Codeforces Round #640 (Div. 4)

A. Sum of Round Numbers

题目链接:https://codeforces.ml/contest/1352/problem/A

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <stack>
    #include <queue>
    #define rush() int T;cin>>T;while(T--)
    #define go(a) while(cin>>a)
    #define ms(a,b) memset(a,b,sizeof a)
    #define E 1e-8
    using namespace std;
    typedef long long ll;
    const int idata=1e6+5;
     
    ll n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    stack<int>stk;
    priority_queue<ll,vector<ll>,less<ll> >q;
    map<ll,int>mp;
    ll a[idata];
     
    int main()
    {
        cin.tie(0);
        iostream::sync_with_stdio(false);
        rush()
        {
            cin>>n;
            num=0,cnt=0;
            while(n){
                a[++num]=n%10;
                n/=10;
            }
            for(i=num;i>=1;i--){
                if(a[i]) cnt++;
            }
            cout<<cnt<<endl;
            for(i=num;i>=1;i--){
                if(a[i]){
                    if(i==5) cout<<a[i]*1e4<<" ";
                    if(i==4) cout<<a[i]*1e3<<" ";
                    if(i==3) cout<<a[i]*1e2<<" ";
                    if(i==2) cout<<a[i]*10<<" ";
                    if(i==1) cout<<a[i]*1<<" ";
                }
            }
            cout<<endl;
        }
        return 0;
    }

 B. Same Parity Summands(构造)


题目链接:https://codeforces.ml/contest/1352/problem/B

    #include <iostream>
    #include <cstdio>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <map>
    #include <stack>
    #include <queue>
    #define rush() int T;cin>>T;while(T--)
    #define go(a) while(cin>>a)
    #define ms(a,b) memset(a,b,sizeof a)
    #define E 1e-8
    using namespace std;
    typedef long long ll;
    const int idata=1e6+5;
     
    ll n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    stack<int>stk;
    priority_queue<ll,vector<ll>,less<ll> >q;
    map<ll,int>mp;
    ll a[idata];
     
    int main()
    {
        cin.tie(0);
        iostream::sync_with_stdio(false);
        rush()
        {
            ll n;
            bool flag=0;
            cin>>n>>k;
            k--;
            if((n-k) & 1) flag=1;
            if(flag && n-k>0){
                cout<<"yes"<<endl;
                cout<<n-k<<" ";
                for(i=1;i<=k;i++){
                    cout<<"1 ";
                }
                cout<<endl;
                continue;
            }
            if(n-2*k<=0 || ((n-2*k) & 1)){
                cout<<"no"<<endl;
                continue;
            }
            else{
                cout<<"yes"<<endl;
                cout<<n-k*2<<" ";
                for(i=1;i<=k;i++){
                    cout<<"2 ";
                }
                cout<<endl;
                continue;
            }
        }
        return 0;
    }

 C. K-th Not Divisible by n

题目链接:https://codeforces.ml/contest/1352/problem/C

题目大意:给你两个数字n,k,输出第k个数(从1开始记数),数字的排列是,是n的倍数的情况下,这个数字不算在k个数里面。

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=1e3+5;

    int n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    int a[idata];

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        ll n,k,m,ans;
        cin>>n>>k;
        //按 1->无穷 每n个数为一组,共m组
        m=(k/(n-1));
        if(k%(n-1)) ans=m*n+k%(n-1);
        else ans=m*n;
        //为什么要-1,可以模拟一下n=3,k=6的情况
        if(ans % n==0) ans--;
        cout<<ans<<endl;
    }
    return 0;
}

 D. Alice, Bob and Candies (模拟)

题目链接:https://codeforces.ml/contest/1352/problem/D

题目大意:给出n个数,a从左边拿,b从右边拿,所拿数的值必须刚刚大于上一次对方上一次那的数,如果不够,就把剩下的全拿了

比赛的时候模拟l1小时,就差一点点,一种情况没有考虑进去

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=1e3+5;

ll n,m,t,_;
int i,j,k;
int cnt,num,ans;
int minn,maxx;
int a[idata];

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        cin>>n;
        for(i=1;i<=n;i++){
            cin>>a[i];
        }
        int x=a[1],y=0;
        if(n==1){
            cout<<"1 "<<x<<" "<<y<<endl;
            continue;
        }

        //判断b拿完,a是否还能拿,(即剩下的数比a要小)
        int l=2,r=n;
        int sum=0;
        for(i=l;i<=r;i++){
            sum+=a[i];
        }
        if(sum<=a[1]){
            y+=sum;
            cout<<"2 "<<x<<" "<<y<<endl;
            continue;
        }

        //b把目标数值拿掉
        int cnt=2,last=a[1],temp=0;
        for(i=n;i>1;i--){
            temp+=a[i];
            if(temp>last){
                last=temp;
                r=i-1;
                break;
            }
        }
        x=a[1],y=temp;
        if(l>r){
            cout<<cnt<<" "<<x<<" "<<y<<endl;
            continue;
        }

        //判断a拿完,b是否还能拿,比赛的时候就因为这个........
        sum=0;
        for(i=l;i<=r;i++){
            sum+=a[i];
        }
        if(sum<=last){
            x+=sum;
            cout<<"3 "<<x<<" "<<y<<endl;
            continue;
        }



        while(true){
            cnt++;
            temp=0;
            for(i=l;i<=r;i++){
                temp+=a[i];
                if(temp>last){
                    last=temp;
                    l=i+1;
                    x+=temp;
                    //cout<<temp<<endl;
                    break;
                }
            }
            if(l>r) break;
            sum=0;
            for(i=l;i<=r;i++){
                sum+=a[i];
            }
            if(sum<=last){
                y+=sum;
                cnt++;
                break;
            }

            cnt++;
            temp=0;
            for(i=r;i>=l;i--){
                temp+=a[i];
                if(temp>last){
                    last=temp;
                    r=i-1;
                    y+=temp;
                    //cout<<temp<<endl;
                    break;
                }
            }
            if(r<l) break;
            sum=0;
            for(i=l;i<=r;i++){
                sum+=a[i];
            }
            if(sum<=last){
                x+=sum;
                cnt++;
                break;
            }
        }
        cout<<cnt<<" "<<x<<" "<<y<<endl;
    }
    return 0;
}

 E. Special Elements(前缀和)


题目链接:https://codeforces.ml/contest/1352/problem/E

consecutive elements         连续元素

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=8000+5;

    int n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    int a[idata];

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        cin>>n; int mp[idata]={0};
        for(i=1;i<=n;i++){
            cin>>a[i];
            mp[a[i]]++;
            a[i]=a[i]+a[i-1];
        }
        int ans=0;
        for(int l=1;l<n;l++){
            for(int r=l+1;r<=n;r++){
                int tag=a[r]-a[l-1];//l---r之间的元素和
                if(tag<=n){
                    if(mp[tag]){
                        ans+=mp[tag];
                        mp[tag]=0;
                    }
                }
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

 F. Binary String Reconstruction


题目链接:https://codeforces.ml/contest/1352/problem/F

题目思路:先考虑n0,n2的输出,无非是输出(n0+1)个0,(n2+1)个1,如果他们非0的话,之后细分n1的输出就可以了

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=8000+5;

    int n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    int a[idata];
    string s;

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        int x,y,z;
        cin>>x>>y>>z;
        if(x)
        for(i=1;i<=x+1;i++){
            cout<<0;
        }
        if(z)
        for(i=1;i<=z+1;i++){
            cout<<1;
        }
        int n;
        if(x && z) n=0,y--;//x,z交界处贡献了一个1
        else if(x && !z) n=1;
        else if(!x && !z) y++,n=0;
        else if(!x && z) n=0;
        for(i=1;i<=y;i++){
            cout<<n;
            n^=1;
        }
        cout<<endl;
    }
    return 0;
}

G. Special Permutation(构造)


题目链接:https://codeforces.ml/contest/1352/problem/G

题目大意:要满足相邻两数之差绝对值大于等于2,小于等于4的条件,且这n个数全为1---n中的某一个数(不能有重复)

#include <bits/stdc++.h>
#define rush() int T;cin>>T;while(T--)
#define go(a) while(cin>>a)
#define ms(a,b) memset(a,b,sizeof a)
#define E 1e-8
using namespace std;
typedef long long ll;
const int idata=8000+5;

    int n,m,t,_;
    int i,j,k;
    int cnt,num,ans;
    int minn,maxx;
    int a[idata];

int main()
{
    cin.tie(0);
    iostream::sync_with_stdio(false);
    rush()
    {
        cin>>n;
        if(n<4){
            cout<<-1<<endl;
            continue;
        }
        if(n & 1) m=n;
        else m=n-1;
        for(i=m;i>=1;i-=2){
            cout<<i<<" ";
        }
        cout<<"4 2 ";
        for(i=6;i<=n;i+=2){
            cout<<i<<" ";
        }
        cout<<endl;
    }
    return 0;
}

原创文章 410 获赞 16 访问量 3万+

猜你喜欢

转载自blog.csdn.net/C_Dreamy/article/details/106061144
今日推荐