CF每日一练(2.9)

CF-1013

A. Piles With Stones

比较两个序列的和,因为只能拿走或者不拿,所以总数不能变大。

B. And

  • 答案只有 -1,0,1,2几种可能,所以对于每一种答案都暴力扫一次是可以的
  • 或者对于每个 \(a_i\) ,将\(a_i\) 标记加一,如果\(a_i \neq a_i\& x\) ,将\(a_i\&x\) 用另一个数组标记加一。然后整体扫一次就可以了
#include <bits/stdc++.h>
using namespace std;
int n,x;
int a[100010],b[100010];
int main(){
    cin>>n>>x;
    for(int i=1;i<=n;i++){
        int y;
        scanf("%d",&y);
        a[y]++;
        if((x&y)!=y)
            b[x&y]++;
    }
    int res = -1;
    for(int i=0;i<=100000;i++)
    {
        if(a[i]>=2)res = 0;
        else if(res!=0&&a[i]==1&&b[i]>=1)res = 1;
        else if(res!=1&&b[i]>=2)res = 2;
    }
    cout<<res<<endl;
    return 0;
}

C. Photo of The Sky

我们关心的只是 \(x_{max} - x_{min}\)\(y_{max} - y_{min}\)

现在的只是整个坐标的合集。先整体排个序。

\[ a_1,a_2 \cdots a_{2\times n-1},a_{2 \times n}\]

  • 如果序列中最大值和最小值在同一个集合,那么枚举另一个集合的最大元素或者最小元素,得到另一个集合的最小的 \(max - min\)
  • 如果序列中最大值和最小值不在同一个集合,那么只有将 \(a_1 \cdots a_n\) 分到一个集合,\(a_{n+1} \cdots a_{2\times n}\) 分到一个集合时最优
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n;
ll a[200010];
int main(){
    scanf("%d",&n);
    for(int i=0;i<2*n;i++)
        scanf("%lld",&a[i]);
    sort(a,a+2*n);
    ll mi = 1ll<<60;
    //第一种情况,枚举另一个集合的最小值a[i]
    for(int i=1;i<n;i++)
        mi = min(mi,a[i+n-1]-a[i]));
    mi = mi*(a[2*n-1]-a[0]);//结算,获得面积
    mi = min(mi,(a[n-1]-a[0])*(a[2*n-1]-a[n]));//与第二种情况作比较
    cout<<mi<<endl;
    return 0;
}

D. Chemical table

tag: 并查集,联通块

题目操作:若有\((r_1,c_1),(r_1,c_2),(r_2,c_1)\) ,那么自动生成\((r_2,c_2)\)

抛开二维平面,寻找坐标点之间的关系,可以发现一条规律:如果\(r_1\)\(c_1,r_2\)有关系,\(r_2\)\(c_2\)有关系,则\(r_2\)\(c_2\)会有关系。如果把他们看成点与点之间的关系,可以画出一个图,这个图是联通的。而任意两个不联通的点只需要再添加一个点就可以使得他们联通。所以我们只需要求出联通块个数就可以知道答案了。

#include <bits/stdc++.h>
using namespace std;
int n,m,q;
int f[400010];
//并查集
int find(int x){
    return x==f[x]? x : f[x] = find(f[x]);
}
int main(){
    cin>>n>>m>>q;
    for(int i=1;i<=n+m;i++)f[i] = i;
    for(int i=0;i<q;i++){
        int x,y;
        cin>>x>>y;
        y+=n;
        x = find(x);y=find(y);
        f[x] = y;
    }
    //先随便找一个联通块
    int root = find(1);
    int res = 0;
    for(int i=2;i<=n+m;i++){
        int x = find(i);
        //如果发现另一个联通块,则先使得他们联通,然后res++
        if(x!=root){
            f[x] = root;res++;
        }
    }
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/chd-acm/p/10357932.html