Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)

Codeforces Round #681 (Div. 2, based on VK Cup 2019-2020 - Final)

传送门(点击传送

A. Kids Seating

题意:
  有 4n 个小孩,标号从 1 到 4n ,任意两个孩子(设两个孩子的标号为 a 和 b) ,若 gcd(a,b)=1 则两个孩子会吵架,若 a 是 b 的倍数或 b 是 a 的倍数则孩子会吵架。现在要选出 n 个孩子且两两不吵架,请输出选择的孩子的标号。
  
思路:
  4n 个孩子选 n 个,标号足够多,从4n开始往小选且选标号为偶数的孩子,选 n 个即可。这样选出来的最小标号的孩子的编号也为 4 n − ( n − 1 ) × 2 = 2 n + 2 4n-(n-1) \times 2 = 2n+2 4n(n1)×2=2n+2,选出的标号均符合题意。
  
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t,n;
    cin>>t;
    while(t--){
    
    
        cin>>n;
        for(int i=4*n,j=0;j<n;j++){
    
    
            cout<<i-2*j<<" ";
        }
        cout<<endl;
    }
    return 0;
}

B. Saving the City

题意:
  t 组数据,每组数据给出一个01串,第 i 位置为 0 则表示该位置没有炸弹,为 1 则表示该位置有炸弹。若第 i 位置炸弹爆炸则 i-1 ,i+1 位置的炸弹也会爆炸,现在引爆一次需要花费 a ,在任意位置 x 放置炸弹需要花费 b ,现在需要你引爆所有给出的炸弹,最少花费是多少。
  
思路:
  先搜索出炸弹带,定义炸弹带为连续紧凑的炸弹,把炸弹带的起始位置和结束位置记录下来。如果没有炸弹,我们答案为0,如果有炸弹,那我们至少需要引爆一次,假设引爆第一组,然后判断相邻的炸弹带是单独引爆划算还是将两个炸弹带之间的空隙都放置炸弹划算,取花费小的方案,依次类推,算完所有炸弹带即可。
  
代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t,a,b,ans=0,fr,se;
    string s;
    cin>>t;
    while(t--){
    
    
        cin>>a>>b;
        cin>>s;
        vector<pair<int,int> >vec;
        for(int i=0;i<s.size();i++){
    
    
            if(s[i]=='1'){
    
    
                fr=i;
                while(i+1<s.size() && s[i+1]=='1'){
    
    
                    i++;
                }
                se=i;
                vec.push_back(make_pair(fr,se));
            }
        }
        if(vec.size()>0) ans=a;
        else ans=0;
        for(int i=1;i<vec.size();i++){
    
    
            if((vec[i].first-vec[i-1].second-1)*b<a){
    
    
                ans+=(vec[i].first-vec[i-1].second-1)*b;
            }else{
    
    
                ans+=a;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

C. The Delivery Dilemma

题意:
  t 组数据,每组数据有 n 个菜,对于每个菜都有两个值,对于第 i 个菜, a i a_{i} ai表示订餐后 a i a_{i} ai分钟能送到, b i b_{i} bi表示我自己去购买这个菜则需要花费 b i b_{i} bi分钟,现在问最少需要多少分钟我们能准备好所有菜(最一开始已订完所有我们需要订的菜,等待到来即可,剩下的就是去购买我们准备自己买的菜)。
  
思路:
  二分搜索,最小为 1 1 1,最大为 1 e 9 1e^{9} 1e9,判断当前时间是否能够完成。判断的时候可以先对数据进行预处理,按照 a [ i ] a[i] a[i]从小到大的顺序排序,对于我们当前时间 x ,所有 a [ i ] ≤ x a[i]≤x a[i]x 的菜可以直接从餐厅订购,然后求剩下所有 a [ i ] > x a[i] > x a[i]>x的菜对应的 b [ i ] b[i] b[i]之和,如果和小于等于 x,则当前时间能够准备所有菜。求 b [ i ] b[i] b[i]和可以通过预处理前缀和来完成O(1)查询。
  
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node{
    
    
    int a,b;
}num[200005];
ll sum[200005];
int pre[200005];
int t,n;

bool judge(int x){
    
    
    int loc=upper_bound(pre+1,pre+1+n,x)-pre;
    if(sum[n]-sum[loc-1]<=1LL*x){
    
    
        return true;
    }else{
    
    
        return false;
    }
}
int main()
{
    
    
    scanf("%d",&t);
    while(t--){
    
    
        sum[0]=0;
        scanf("%d",&n);
        for(int i=1;i<=n;i++){
    
    
            scanf("%d",&num[i].a);
            pre[i]=num[i].a;
        }
        for(int i=1;i<=n;i++){
    
    
            scanf("%d",&num[i].b);
        }
        sort(pre+1,pre+1+n);
        sort(num+1,num+1+n,[](const node &x,const node &y){
    
    return x.a<y.a;});
        for(int i=1;i<=n;i++){
    
    
            sum[i]=sum[i-1]+1LL*num[i].b;
        }
        int l=1,r=1e9;
        while(l<r){
    
    
            int mid=(l+r)>>1;
            if(judge(mid)){
    
    
                r=mid;
            }else{
    
    
                l=mid+1;
            }
        }
        cout<<l<<endl;
    }
    return 0;
}

D. Extreme Subtraction

题意:
  有一个序列,你可以进行任意次操作,每次操作有如下两种选择:①前 k 个元素每个元素减一,②后 k 个元素每个元素减一,k 自选,问能否通过操作使序列全部变为0。
  
思路:
  从第二个位置开始,对于每个位置 i ,比较 n u m [ i ] num[i] num[i] n u m [ i − 1 ] num[i-1] num[i1]的大小,设 c h a = a b s ( n u m [ i ] − n u m [ i − 1 ] ) cha=abs(num[i]-num[i-1]) cha=abs(num[i]num[i1]),如果 n u m [ i ] num[i] num[i]大,说明 n u m [ i ] num[i] num[i]需要比 n u m [ i − 1 ] num[i-1] num[i1]从后面多进行 c h a cha cha次操作②,如果 n u m [ i − 1 ] num[i-1] num[i1]大,说明 n u m [ i − 1 ] num[i-1] num[i1]需要比 n u m [ i ] num[i] num[i]从前面多进行 c h a cha cha次操作①,用线段树进行维护。最后进行完如上操作每个位置的元素一定相等,那么只需要检查任意位置上的元素是否大于等于0即可。
  
代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MAXN=30005;
ll num[MAXN];
struct Tree {
    
    
        ll l,r;
        ll sum;
        ll lazy;
} tree[MAXN<<2];
void push_up(ll node) {
    
    
    tree[node].sum=tree[node<<1].sum+tree[node<<1|1].sum;
}
void push_down(ll node, ll length) {
    
    
    if(tree[node].lazy) {
    
    
        tree[node<<1].lazy+=tree[node].lazy;
        tree[node<<1|1].lazy+=tree[node].lazy;
        tree[node<<1].sum+=(length-(length>>1))*tree[node].lazy;
        tree[node<<1|1].sum+=(length>>1)*tree[node].lazy;
        tree[node].lazy = 0;
    }
}
void build(ll l,ll r, ll node) {
    
    
    tree[node].lazy = 0;
    tree[node].l=l;
    tree[node].r=r;
    if(l==r) {
    
    
        tree[node].sum=num[l];
        return;
    }
    ll mid=(l+r)>>1;
    build(l,mid,node<<1);
    build(mid+1,r,node<<1|1);
    push_up(node);
}
void update(ll left,ll right,ll key, ll node) {
    
    
    if(tree[node].l>=left && tree[node].r<=right) {
    
    
        tree[node].sum+=(tree[node].r-tree[node].l+1)*key;
        tree[node].lazy+=key;
        return;
    }
    push_down(node,tree[node].r-tree[node].l+1);
    ll mid=(tree[node].r+tree[node].l)>>1;
    if(left<=mid) update(left,right,key,node<<1);
    if(right>mid) update(left,right,key,node<<1|1);
    push_up(node);
}
ll query(ll left,ll right,ll node) {
    
    
    if(tree[node].l>=left && tree[node].r<=right) {
    
    
        return tree[node].sum;
    }
    push_down(node,tree[node].r-tree[node].l+1);
    ll mid=(tree[node].r+tree[node].l)>>1;
    ll ans=0;
    if(left<=mid) ans+=query(left,right,node<<1);
    if(right>mid) ans+=query(left,right,node<<1|1);
    return ans;
}
int main()
{
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    int t,n;
    cin>>t;
    while(t--){
    
    
        cin>>n;
        for(int i=1;i<=n;i++){
    
    
            cin>>num[i];
        }
        build(1,n,1);
        for(int i=2;i<=n;i++){
    
    
            ll cha=query(i,i,1)-query(i-1,i-1,1);
            if(cha>0){
    
    
                update(i,n,-1*cha,1);
            }else if(cha<0){
    
    
                update(1,i-1,cha,1);
            }
        }
        ll check=query(1,1,1);
        if(check>=0) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/blaction/article/details/109458839
今日推荐