2021/12/09程式培力刷題記錄

刷題證明 

ACodeForces 1593AElections

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    int x, y, z;
    cin >> t;
    while (t--)
    {
        cin >> x >> y >> z;
        int big=max(max(x, y), z);
        if (x > max(y, z))
        {
            cout << "0 ";
        }
        else if(x<=max(y,z))
        {
            cout << abs(big - x) +1<< " ";
        }
        if (y > max(x, z))
        {
            cout << "0 ";
        }
        else if(y<=max(x,z))
        {
            cout << abs(big - y)+1 << " ";
        }
        if (z > max(x, y))
        {
            cout << "0 " << endl;
        }
        else if(z<=max(x,y))
        {
            cout << abs(big- z)+1 << " " << endl;
        }
    }
    return 0;
}
BCodeForces 1550AFind The Array
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n = 0, ans = 0;
        cin >> n;
        int counter = 1;
        while (n > 0)
        {
            n = n - counter;
            ans++;
            counter += 2;
        }
        cout << ans << endl;

    }
    return 0;
}
CCodeForces 1480AYet Another String Game

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        char game[60];
        char check[30];
        cin >> game;
        for (int i = 0; i < strlen(game); i += 2)
        {
            if (game[i] == 'a')
            {
                game[i] = 'b';
            }
            else
            {
                game[i] = 'a';
            }
        
        }
        for (int i = 1; i <strlen(game); i += 2)
        {
            if (game[i] == 'z')
            {
                game[i] = 'y';
            }
            else
            {
                game[i] = 'z';
            }
        }
        printf("%s\n", game);

    }
    return 0;
}
DCodeForces 1430BBarrels
#include<bits/stdc++.h>
using namespace std;
#define int long long 
const int maxx=2e6;
int b[maxx];
signed main()
{
    int t, n, k;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 0; i < n; i++)
        {
            cin >> b[i];
        }
        sort(b, b + n);
        for (int i = n - 2; i >= n -k-1; i--)
        {
            b[n-1] += b[i];
        }
        cout << b[n-1] << endl;
    }
}
ECodeForces 1501BNapoleon Cake

 

#include<bits/stdc++.h>
using namespace std;
int ans[200100], a[200100];
int main()
{
    int t, n;
    cin >> t;
    while (t--)
    {
        cin >> n;

        for (int i = 1; i <= n; i++)
        {
            ans[i] = 0;
            a[i] = 0;
        }
        for (int i = 1; i <= n; i++)
        {
            int x;
            cin >> x;
            ans[max(1, i - x+ 1)]++;
            ans[i + 1]--;

        }
        for (int i = 1; i <= n; i++)
        {
            a[i] = a[i - 1] + ans[i];
            printf("%d ", a[i]> 0);
        }
        cout << endl;

    }
FCodeForces 1427AAvoiding Zero
#include<bits/stdc++.h>
using namespace std;
bool cmp(const int& x, const int& y)
{
    return x > y;
}
int main()
{
    int t;
    int arr[60];
    int sum;
    cin >> t;
    while (t--)
    {

        int n;
        cin >> n;
        sum = 0;
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
            sum += arr[i];
        }
        if (sum > 0)
        {
            printf("YES\n");
            sort(arr, arr + n, cmp);
        }
        else if (sum == 0)
        {
            printf("NO\n");
            continue;
        }
        else
        {
            printf("YES\n");
            sort(arr, arr + n);
        }
        for (int i = 0; i < n; i++)
        {
            cout << arr[i]<<" ";
        }
        cout << endl;
        for (int i = 0; i <= n; i++)
        {
            arr[i] = 0;
        }




    }
}
GCodeForces 16BBurglar and Matches

 

#include<bits/stdc++.h>
using namespace std;
long long arr[30][30];
int main()
{
    int m;
    long long n[1][1];
    cin >> n[0][0];
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            cin >> arr[i][j];
        }
    }
    for (int i = m - 1; i > 0; i--)
    {
        for (int j = 0; j <=i-1; j++)
        {
            if (arr[j][1] > arr[j + 1][1])
            {
                swap(arr[j][1], arr[j + 1][1]);
                swap(arr[j][0], arr[j + 1][0]);
            }
        }
    }
    
    if (n[0][0]<= arr[m - 1][0])
    {
        cout << n[0][0] * arr[m - 1][1] << endl;
        return 0;
    }
    else if (n[0][0] > arr[m - 1][0])
    {
        int sum = 0;
        int ans = 0;
        int cnt = 0;
        for (int i = m - 1; i >=0; --i)
        {
            if (sum > n[0][0])
            {
                ans -= (sum - n[0][0]) * arr[i+1][1];
                cout << ans << endl;
                return 0;
            }
           if (sum == n[0][0])
            {
                cout << ans << endl;
                return 0;
            }
            sum += arr[i][0];
            ans += arr[i][0] * arr[i][1];
            cnt = i;
        }
        if (sum > n[0][0])
        {
            ans -= (sum - n[0][0]) * arr[cnt][1];
            cout << ans << endl;
            return 0;
        }
        if (sum < n[0][0])
        {
            cout << ans << endl;
            return 0;
        }
        if (sum == n[0][0])
        {
            cout << ans << endl;
            return 0;
        }
        
        
    }
    return 0;



}
HCodeForces 352AJeff and Digits
#include<bits/stdc++.h>
using namespace std;
int  main()
{
    int n;
    cin >> n;
    int x = 0;
    int zero = 0;
    int five = 0;
    for (int i = 0; i < n; i++)
    {
        cin >> x;
        if (x == 0)
        {
            zero++;
        }
        else if (x == 5)
        {
            five++;
        }
    }
    if (zero == 0)
    {
        printf("-1");
        return 0;
    }
    if (zero > 0&&five/9>=1)
    {
        for (int i = 0; i < five / 9; i++)
        {
            printf("555555555");
        }
        for (int i = 0; i < zero; i++)
        {
            printf("0");
        }
        return 0;
    }
    else if(zero>0&&five/9==0)
    {
        printf("0");
        return 0;
    }
    else
    {
        printf("-1");
    }
    return 0;
}
ICodeForces 1605BReverse Sort

 

#include<bits/stdc++.h>
using namespace std;
int cnt1, cnt0;
int pos1[1010], pos0[1010];
int main()
{
    int t, n;
    string s;
    cin >> t;
    while (t--)
    {
        cnt1 = 0;
        cnt0 = 0;
        cin >> n;
        cin >> s;
        for (int i = 0; i <n; i++)
        {
            if (s[i] == '1')
            {
                cnt1++;
                pos1[cnt1] = i+1;
            }
        }
        for (int i = n-1; i >=0; i--)
        {

            if (cnt1 == cnt0)
            {
                break;
            }
            if (s[i] == '0')
            {
                cnt0++;
                pos0[cnt0] = i+1;
            }
            else if (s[i] == '1')
            {
                cnt1--;
            }

        }
        if (cnt1 >= 1)
        {
            cout << "1" << endl;
            cout << cnt1 + cnt0 << " ";
            for (int i = 1; i <= cnt1; i++)
            {
                cout << pos1[i] << " ";
            }
            for (int i = cnt0; i > 0; i--)
            {
                cout << pos0[i] << " ";
            }
            cout << endl;
        }
        else
        {
            cout << "0" << endl;
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值