• Educational Codeforces Round 21 A-D


    A. Lucky Year

    水题

    #include 
    
    using namespace std;
    const int N = 2e5 + 5;
    typedef long long ll;
    typedef pair<ll, ll> pll;
    typedef array<ll, 3> p3;
    int mod = 1e9+7;
    const int maxv = 4e6 + 5;
    // #define endl "\n"
    
    
    
    void solve()
    {
        ll n;
        cin>>n;
        ll tmp=n;
        int cnt=0;
        while(tmp){
            tmp/=10;
            cnt++;
        }
        ll d=pow(10,cnt-1);
        ll ans=(n+d-1)/d*d;
        if(ans==n){
            cout<<d<<endl;
        }
        else cout<<ans-n<<endl;
    
    }
    
    int main()
    {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    B. Average Sleep Time

    思路:前缀和

    #include 
    
    using namespace std;
    const int N = 2e5 + 5;
    typedef long long ll;
    typedef pair<ll, ll> pll;
    typedef array<ll, 3> p3;
    int mod = 1e9+7;
    const int maxv = 4e6 + 5;
    // #define endl "\n"
    
    
    
    void solve()
    {
        int n,k;
        cin>>n>>k;
        vector<ll> a(n+5),s(n+5);
        for(int i=1;i<=n;i++) cin>>a[i];
        for(int i=1;i<=n;i++) s[i]=s[i-1]+a[i];
        ll sum=0;
        for(int i=1;i<=n-k+1;i++){
            sum+=s[i+k-1]-s[i-1];
        }
        //cout<
        long double ave=sum*1.0/(n-k+1);
        printf("%.6Lf",ave);
    }
    
    int main()
    {
        // ios::sync_with_stdio(0);
        // cin.tie(0);
        // cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    C. Tea Party

    思路:贪心。我们先把每个杯子倒满一半,然后把贪心的去把剩余倒入容量最大的杯子即可。
    被数据范围诈骗了,不敢写。

    #include 
    
    using namespace std;
    const int N = 2e5 + 5;
    typedef long long ll;
    typedef pair<ll, ll> pll;
    typedef array<ll, 3> p3;
    int mod = 1e9+7;
    const int maxv = 4e6 + 5;
    // #define endl "\n"
    
    void solve()
    {
        int n,w;
        cin>>n>>w;
        vector<pll> a(n+5);
        int sum=0;
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            a[i]={x,i};
        }
        for(int i=1;i<=n;i++){
            auto &[x,y]=a[i];
            sum+=(x+1)/2;
        }
        if(sum>w){
            cout<<-1<<endl;
            return ;
        }
        sort(a.begin()+1,a.begin()+1+n,[](pll x,pll y){
            return x.first>y.first;
        });
        int cur=w-sum;
        for(int i=1;i<=n;i++){
            auto &[x,y]=a[i];
            int tmp=x;
            x=x-(x+1)/2;
            if(cur>=x){
                cur-=x;
                x=tmp;
                cur=max(cur,0);
            }
            else{
                x=(tmp+1)/2+cur;
                cur=0;
            }
        }
        sort(a.begin()+1,a.begin()+1+n,[](pll x,pll y){
            return x.second<y.second;
        });
        for(int i=1;i<=n;i++){
            cout<<a[i].first<<" ";
        }
        cout<<endl;
    
    }
    
    int main()
    {
         ios::sync_with_stdio(0);
         cin.tie(0);
         cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72

    D. Array Division

    思路:我们考虑一个序列能够被分为两部分,且两部分和相等的条件:两部分的值肯定要相等,即整个序列的总和为偶数。
    我们定义ave为两部分相等的值。
    我们可以对整个序列进行扫描,然后使用两个map来记录目前已经算入总和的数和未算入总和的数,若当前总和sum大于ave,则说明我们已经统计的总和过大,若当前已经算入总和的数中存在sum-ave这个数,那么肯定能讲整个序列分为两部分。
    同理,若sum小于ave,那么则是总和过小,我们只需要进行判断,对于未算入总和的数,是否存在一个数为ave-sum即可。

    #include 
    
    using namespace std;
    const int N = 2e5 + 5;
    typedef long long ll;
    typedef pair<ll, ll> pll;
    typedef array<ll, 3> p3;
    int mod = 1e9+7;
    const int maxv = 4e6 + 5;
    // #define endl "\n"
    
    
    
    void solve()
    {
        int n;
        cin>>n;
        vector<ll> a(n+5);
        ll sum=0;
        map<ll,int> mp1,mp2;
        for(int i=1;i<=n;i++) cin>>a[i],sum+=a[i],mp1[a[i]]++;
        if(sum&1){
            cout<<"NO"<<endl;
            return ;
        }
        //sort(a.begin()+1,a.begin()+1+n);
        ll tmp=sum/2;
        ll res=0;
        for(int i=1;i<=n;i++){
            res+=a[i];
            mp1[a[i]]--;
            mp2[a[i]]++;
            if(res==tmp){
                cout<<"YES"<<endl;
                return ;
            }
            else if(res<=tmp){
                if(mp1[tmp-res]>0){
                    cout<<"YES"<<endl;
                    return ;
                }
            }
            else{
                if(mp2[res-tmp]>0){
                    cout<<"YES"<<endl;
                    return ;
                }
            }
        }
        cout<<"NO"<<endl;
    }
    
    int main()
    {
        // ios::sync_with_stdio(0);
        // cin.tie(0);
        // cout.tie(0);
        int t;
        t=1;
        //cin>>t;
        while(t--){
            solve();
        }
        system("pause");
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    MySQL 性能调优和优化技巧
    洛谷 NOIP 2023 模拟赛 挑战 NPC IV
    HDFS文件系统检查工具fsck
    【C++入门】文件流(fstream)介绍和使用
    asp.net docker-compose添加kafka和redis和zookeeper
    Go 时间轴排序
    客户端日志打印规范
    软考网络工程师每日一练10.17
    C++制作游戏引擎之一 方向键控制地球上下左右乱跑
    数组知识点以及leetcode刷题
  • 原文地址:https://blog.csdn.net/Unlimited_ci/article/details/134467061