Codeforces Round #652 (Div. 2) A~C

题目链接

A FashionabLee

看能不能被4整除。

#include 
using namespace std;
#define ll long long
int main(){   
    int t;    
    ll n;    
    cin >> t;    
    while(t--)    
    {        
        cin >> n;        
        if(n%4==0)            
            cout << "YES" <<endl;        
        else            
            cout << "NO" <<endl;    
    }    
    return 0;
}

B AccurateLee

刚开始用stack做中间步骤出问题了,后来用的规律。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define ll long long
int main()
{
    int t,n;
    string a;
    cin >> t;
    while(t--)
    {
        cin >> n >> a;
        int sum=0;
        for(int i=0;i<n;i++)
            if(a[i]=='1') 
                sum++;
        if(sum==0||sum==n){ cout<<a<<endl;continue; }
        int flag1=0,flag2=n;
        for(int i=0;i<n;i++)
            if(a[i]=='1')
            {
                flag1=i;
                break;
            }
        for(int i=n-1;i>=0;i--)
            if(a[i]=='0')
            {
                flag2=i;
                break;
            }
        for(int i=0;i<flag1;i++)
            cout << 0;
        if(flag1!=flag2+1) 
            cout << 0;
        for(int i=flag2+1;i<n;i++)
            cout << 1;
        cout<<endl;
    }
    return 0;
}

C

用两个数指向数组前后,然后逐位进行操作即可。

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int N = 2e5 + 10;
#define ll long long
ll arr[N],ans[N];
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        ll n,k;
        cin >> n >> k;
        ll l = 1,r = n,sum = 0,q;
        for(int i = 1; i <= n; i++)
            cin >> arr[i];
        for(int i = 1; i <= k; i++)
            cin >> ans[i];
        sort(ans+1,ans+k+1);
        sort(arr+1,arr+n+1);
        for(q = 1; q <= k; q++)
        {
            if(ans[q] != 1) break;
            sum += (arr[r]*2);
            r--;
        }
        if(q == k+1)
            cout << sum << endl;
        else
        {
            sort(ans+q,ans+1+k,cmp);
            while(q<=k)
            {
                ans[q] -= 2;
                sum += (arr[r]+arr[l]);
                r--,l++;
                while(ans[q]--)
                    l++;
                q++;
            }
            cout << sum << endl;
        }
    }
}

你可能感兴趣的:(Codeforces Round #652 (Div. 2) A~C)