codeforces 杂题训练

开个新坑吧...上一个坑是dp,补的巨累QwQ,这次先开30题再慢慢干到50吧...

难度估计在  普及~提高  主要是 cf 中的 D之类的题...自认为难度差不多普及~提高的都会拉进来...

废话不多说,开坑!!!

2/30

1.CF988D

这是一场div3的0.0结果难度逼近div2。

题意: 给 n 个数 从中选出一个子集,要求这个子集里面任意两个数之差都为 2的非负整数幂。输出最大子集。

这题看了题解QAQ,看到了结论,最大个数不超过 3 个,于是自己证明了一下。

设有 四个数 A B C D 为子集,只要证明不存在这样的子集,就说明是不超过3个的。

假设 A>B>C>D (注意子集不可能有两个数相等,因为2x≠0) 则这个子集有这样的性质。

②-①得 

      

      

      

      

           因为  为偶数 (除 y-x=0之外)     为偶数 (除 z-x=0之外)  而 一个偶数+1 必为奇数,由此 y-x=0 或 z-x=0 

      由于   都 大于0 而  则y-x≠0 否则   为0 不符合。

      由此得出   x=z  x+1=y然后引入 D 

      

      

      只看 A B D同理可得 x=p 只看 B C D 可得 z+1=p  

      于是 x=z+1 而之前求到了 x=z 于是矛盾,所以不可能存在超过个数为3的子集。

所以捏,枚举一下 x 

再枚举一下 B

只要枚举b的时候拿二分判断一下 A C 是否存在即可。

效率 

 1 #define ll long long
 2 #include<cstdio>
 3 #include<algorithm>
 4 
 5 using namespace std;
 6 const int maxn=2*1e5+50;
 7 ll a[maxn];
 8 int n;
 9 inline int read(){
10   char c=getchar();int x=0,f=1;
11   while (c<'0'||c>'9') {
12      if (c='-') f=-1;
13      c=getchar();
14   }
15   while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
16   return x*f;
17 }
18 int find(int l,int r,ll x){
19    while (l<=r){
20      int m=(l+r)>>1;
21      if (a[m]<x) l=m+1;else r=m-1;
22    }
23    if (l<=n&&a[l]==x) return l;else return 0;
24 }
25 int main(){
26    n=read();
27    int ans=1,ansi=1,ansx=0,ansy=0;
28    ll z=0;
29    for (int i=1;i<=n;i++) a[i]=read();
30    sort(a+1,a+n+1);
31    for (int k=0;k<=32;k++){
32       if (k==0) z=1;else z*=2;
33       for (int i=1;i<=n;i++){
34         int x=find(1,i-1,a[i]-z),y=find(i+1,n,a[i]+z),tot=1;
35         if (x) tot++;
36         if (y) tot++;
37         if (tot==3) {
38            printf("3\n%lld %lld %lld",a[x],a[i],a[y]);
39            return 0;
40         }else{
41            if (tot>ans)ans=tot,ansi=i,ansx=x,ansy=y;
42         }
43       }
44    }
45    printf("%d\n",ans);
46    if (ansx) printf("%lld ",a[ansx]);
47    printf("%lld ",a[ansi]);
48    if (ansy) printf("%lld",a[ansy]);
49    return 0;
50 }
CF988D

2.CF987D

题意: 给一个无向图,边权为1,每个点有一个权值(<=k),从一个点出发到达其他点就可以拿到这个到达点的权值,问每一个点拿到s个不同的权值需要跑多远。

QwQ这题还是看题解了...

由于 k 较小,就枚举 k 然后把声音权值是k的拉进队列里然后跑bfs就可以得到一个数组 dist[i][x] 表示 第i个点 拿权值为x 需要的最短路。

然后对dist[i]排序取前s个就好了0.0

 1 #define ll long long
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1e5+50;
 6 int dist[maxn][105],v[maxn],first[maxn],a[maxn],q[maxn],tot=0,n;
 7 struct enode{
 8     int next,y;
 9 }e[maxn*2];
10 inline int read(){
11     char c=getchar();int x=0;
12     while (c<'0'||c>'9') c=getchar();
13     while (c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
14     return x;
15 }
16 void adde(int x,int y){
17     e[tot].next=first[x];
18     e[tot].y=y;
19     first[x]=tot++;
20 }
21 void bfs(int x){
22     int head=1,tail=0;
23     for (int i=1;i<=n;i++) {
24         dist[i][x]=maxn;
25         v[i]=0;
26         if (a[i]==x) q[++tail]=i,dist[i][x]=0;
27     }
28     while (head<=tail){
29         int now=q[head];
30         for (int i=first[now];i>=0;i=e[i].next){
31             int y=e[i].y;
32             if (dist[now][x]+1<dist[y][x]) {
33                 dist[y][x]=dist[now][x]+1;
34                 if (!v[y]) v[y]=1,q[++tail]=y;
35             }
36         }
37         head++;
38     }
39 }
40 int main(){
41     n=read();
42     int m=read(),k=read(),s=read();
43     for (int i=1;i<=n;i++) a[i]=read(),first[i]=-1;
44     for (int i=1;i<=m;i++) {
45         int x=read(),y=read();
46         adde(x,y);
47         adde(y,x);
48     }
49     for (int w=1;w<=k;w++) bfs(w);
50     for (int i=1;i<=n;i++){
51         sort(dist[i]+1,dist[i]+k+1);
52         ll ans=0;
53         for (int j=1;j<=s;j++) ans+=dist[i][j];
54         printf("%lld ",ans);
55     }
56     return 0;
57 }
CF987D

猜你喜欢

转载自www.cnblogs.com/Bunnycxk/p/9160173.html