codeforces-1133 (div3)

A.先全部化成分钟数,取平均数之后化成正常时刻。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
int main(){
int h1,m1,h2,m2;
scanf("%d:%d %d:%d",&h1,&m1,&h2,&m2);
int num = (h2 - h1) * + (m2 - m1);
num /= ; m1 += num;
while(m1 >= ){
h1++;
m1 -= ;
}
printf("%02d:%02d",h1,m1);
return ;
}

A

B.所有数全部对K取余之后就能知道可以匹配他的兄弟是谁了,计数直接算即可。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
int a[maxn];
int num[maxn];
int main(){
Sca2(N,K);
for(int i = ; i <= N ; i ++){
scanf("%d",&a[i]);
a[i] %= K;
num[a[i]]++;
}
int ans = num[] / ;
for(int i = ; i + i <= K; i ++){
if(i + i == K) ans += num[i] / ;
else ans += min(num[i],num[K - i]);
}
Pri(ans * );
return ;
}

B

C.一个类似简化版的尺取即可。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
int a[maxn];
int main(){
Sca(N);
for(int i = ; i <= N ; i ++) Sca(a[i]);
sort(a + ,a + + N);
int s = ,t = ;
int ans = ;
for(int i = ; i <= N ; i ++){
while(t < N && a[t + ] - a[i] <= ) t++;
ans = max(ans,t - i + );
}
Pri(ans);
return ;
}

C

D.由于使每个位置取0的D有一个固定的数,map统计一下相同D的最大个数即可。

这里选择用结构体重载一下运算符处理,double 和 long double 都WA了,所以化成了分数最简比。

要注意如果要用map去重,不但要重载 == ,还有 <,最好可以每一个元素都在 < 里面讨论到,使得map排序结果可以确定

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const long double eps = 1e-;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
LL a[maxn],b[maxn];
struct Node{
LL x,y;
Node(){}
Node(LL x,LL y):x(x),y(y){}
friend bool operator == (Node a,Node b){
return a.x == b.x && a.y == b.y;
}
friend bool operator < (Node a,Node b){
if(a.x == b.x) return a.y < b.y;
return a.x < b.x;
}
};
LL gcd(LL a,LL b){
return !b?a:gcd(b,a % b);
}
map<Node,int>P;
int main(){
Sca(N);
for(int i = ; i <= N ; i ++) scanf("%lld",&a[i]);
for(int i = ; i <= N ; i ++) scanf("%lld",&b[i]);
int ans = ,flag = ;
for(int i = ;i <= N ; i ++){
if(!a[i]){
if(!b[i]) flag++;
continue;
}
Node d;
if(!b[i]){
d = Node(,);
}else{
LL g = gcd(abs(a[i]),abs(b[i]));
int flag = ;
if(a[i] < ) flag *= -;
if(b[i] < ) flag *= -;
d = Node(flag * abs(b[i]) / g,abs(a[i]) / g);
}
P[d]++;
ans = max(ans,P[d]);
}
Pri(ans + flag);
return ;
}

D

E.C题的扩展,dp[i][j]记录有1 ~ j之间选i只队最多可以选多少人,直接递推即可。

5000 * 5000有很大MLE的风险,但是第一层i可以直接省略掉,空间复杂度为O(n)

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
int a[maxn];
int pre[maxn];
int dp[maxn];
int main(){
Sca2(N,K);
for(int i = ; i <= N ; i ++) Sca(a[i]);
sort(a + ,a + + N);
int t = ;
for(int i = ; i <= N; i ++){
while(t <= N && a[t + ] - a[i] <= ){
t++; pre[t] = i;
}
}
for(int i = ; i <= K ; i ++){
for(int j = N; j >= ; j --){
dp[j] = max(dp[j],dp[pre[j] - ] + j - pre[j] + );
}
for(int j = ; j <= N ; j ++) dp[j] = max(dp[j],dp[j - ]);
}
Pri(dp[N]);
return ;
}

E

F1.取ind最多的点,将所有和他相邻的点率先加入树边,然后并查集处理加入余下的树边

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
int ind[maxn];
int fa[maxn];
void init(){
for(int i = ; i <= N ; i ++) fa[i] = i;
}
int find(int t){
if(t == fa[t]) return t;
return fa[t] = find(fa[t]);
}
int Union(int a,int b){
a = find(a); b = find(b);
if(a == b) return ;
fa[a] = b;
return ;
}
PII E[maxn];
int main(){
Sca2(N,M); init();
for(int i = ; i <= M ; i ++){
int u,v; Sca2(u,v);
E[i].fi = u; E[i].se = v;
ind[u]++; ind[v]++;
}
int Max = ;
for(int i = ; i <= N ; i ++){
if(ind[Max] < ind[i]) Max = i;
}
int ans = ;
for(int i = ; i <= M ; i ++){
if(E[i].fi == Max || E[i].se == Max){
if(Union(E[i].fi,E[i].se)){
ans++;
printf("%d %d\n",E[i].fi,E[i].se);
}
}
}
for(int i = ; i <= M && ans < N - ; i ++){
if(Union(E[i].fi,E[i].se)){
ans++;
printf("%d %d\n",E[i].fi,E[i].se);
}
}
return ;
}

F1

F2.一开始以为点1出去的桥必须联通,其他任意,后来发现并不。

删除所有1相邻的边之后产生的连通分量,1需要有边和每一个连通分量有边,其余剩下的位置才是任意。

#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
#define For(i, x, y) for(int i=x;i<=y;i++)
#define _For(i, x, y) for(int i=x;i>=y;i--)
#define Mem(f, x) memset(f,x,sizeof(f))
#define Sca(x) scanf("%d", &x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x);
#define Pri(x) printf("%d\n", x)
#define Prl(x) printf("%lld\n",x);
#define CLR(u) for(int i=0;i<=N;i++)u[i].clear();
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define PII pair<int,int>
#define PIL pair<int,long long>
#define PLL pair<long long,long long>
#define pb push_back
#define fi first
#define se second
typedef vector<int> VI;
int read(){int x = ,f = ;char c = getchar();while (c<'' || c>''){if (c == '-') f = -;c = getchar();}
while (c >= ''&&c <= ''){x = x * + c - '';c = getchar();}return x*f;}
const double eps = 1e-;
const int maxn = 2e5 + ;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + ;
int N,M,K;
struct Edge{
int to,next;
bool cut;
}edge[maxn * ];
int head[maxn],tot;
int fa[maxn];
int sum;
void init(){
for(int i = ; i <= N ; i ++) head[i] = -;
tot = ;
}
int find(int x){
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
int Union(int a,int b){
a = find(a); b = find(b);
if(a == b) return ;
fa[a] = b; sum++;
return ;
}
void add(int u,int v){
edge[tot].to = v;
edge[tot].next = head[u];
edge[tot].cut = ;
head[u] = tot++;
}
int ind[maxn];
vector<PII>ans;
int main(){
Sca3(N,M,K); init();
for(int i = ; i <= N ; i ++) fa[i] = i;
for(int i = ; i <= M ; i ++){
int u = read(),v = read();
add(u,v); add(v,u);
if(u != && v != ) Union(u,v);
ind[u]++; ind[v]++;
}
for(int j = head[]; ~j ; j = edge[j].next){
if(Union(,edge[j].to)){
edge[j].cut = edge[j ^ ].cut = ;
}
}
sum = ;
for(int i = ; i <= N ; i ++) fa[i] = i;
for(int i = head[]; ~i; i = edge[i].next){
if(edge[i].cut && Union(,edge[i].to)){
ans.pb(mp(,edge[i].to));
K--;
}
}
for(int i = ; i <= N; i ++){
for(int j = head[i]; ~j; j = edge[j].next){
if((i == || edge[j].to == ) && K <= ) continue;
if(Union(edge[j].to,i)){
if(i == || edge[j].to == ) K--;
ans.pb(mp(i,edge[j].to));
}
}
}
if(sum < N - || K){
puts("NO");
if(M == ){
cout << sum << " " << K << endl;
}
return ;
}
puts("YES");
for(int i = ; i < ans.size(); i ++){
printf("%d %d\n",ans[i].fi,ans[i].se);
}
return ;
}

F2

上一篇:WIA


下一篇:413 Arithmetic Slices