• Codeforces Round #835 (Div. 4)



    一、A - Medium Number

    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N];
    void solve()
    {
        cin >> a[1] >> a[2] >> a[3];
        sort(a + 1,a + 1 + 3);
        cout << a[2] << endl;
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
       
        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

    二、B - Atilla’s Favorite Problem

    -思路: 排个序找最大值输出即可

    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N];
    void solve()
    {
       int n; cin >> n;
       string s; cin >> s;
       sort(s.begin(),s.end());
       cout << s[n - 1] - 'a' + 1 << endl;
        
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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

    三、C - Advantage

    • 思路: 记录数组中的最大值与次大值,然后枚举数组中的每一个元素,如果A[i] 不是最大值,就减去最大值,如果是最大值就减去次大值
    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N],b[N];
    void solve()
    {
       int n; cin >> n;
       
       for(int i = 1;i <= n;i ++ ) 
       {
           cin >> a[i];
           b[i] = a[i];
       }
       
       sort(a + 1,a + 1 + n);
       int mx = a[n];
       int mx2 = a[n - 1];
       
       for(int i = 1;i <= n;i ++ )
       {
           if(b[i] != mx)
           cout << b[i] - mx << ' ';
           else
           cout << b[i] - mx2 << ' ';
       }
       
       cout << endl;
       
        
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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

    四、D - Challenging Valleys

    • 思路: 数组只有3种情况 ,一直非递减,一直非递增,先递减再递增,那就判断如果当前遍历到的是递减的情况,就看一下前面是否出现递增,如果出现就不行 其他情况都可以
    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N],b[N];
    void solve()
    {
       int n; cin >> n;
       bool f1 = false,f2 = false,success = true;
       for(int i = 1;i <= n;i ++ )  cin >> a[i];
       if(n == 1) cout << "YES" << endl;
       else
       {
             for(int i = 2;i <= n;i ++ ) // 当且出现一次
       {
           if(a[i] == a[i - 1]) continue;
          
           if(a[i] < a[i - 1])
           {
               if(!f2)
               f2 = true;
               
               if(f1)
               {success = false;
               break;
               }
           }
        
           if(a[i] > a[i - 1]) f1 = true; // 上升
       }
       
      if(success) cout << "YES" << endl;
      else cout << "NO" << endl;
       }
     
        
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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

    五、E - Binary Inversions

    • 思路: 只能改变一次,如果是在位置 i 的1变成0对结果的影响是, i 到 n 之间 0 的个数cnt0,和 1 到 i 之间1的个数cnt1,就是cnt1 - cnt0;如果位置 i 是 0 然后变成1的话,同理,所以我们就用两个前缀和数组记录一下1的个数与0的个数,枚举每一个位置取最大值就好
    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N],s0[N],s1[N];
    
    void solve()
    {
       int n; cin >> n;
       for(int i = 1;i <= n;i ++ ) cin >> a[i];
       
       for(int i = 1;i <= n;i ++ )
       {
           if(a[i] == 1)
           {
               s1[i] = s1[i - 1] + 1;
               s0[i] = s0[i - 1];
           }
           else
           {
               s1[i] = s1[i - 1];
               s0[i] = s0[i - 1] + 1;
           }
           
       }
        int ans = 0;
        for(int i = 1;i <= n;i ++ )
        {
            if(a[i] == 1)
            {
                ans += s0[n] - s0[i];
            }
        }
        int res = ans;
        for(int i = 1;i <= n;i ++ )
        {
            if(a[i] == 0) // 把这个翻转成1
            {
                int k = s0[n] - s0[i];
                int w = s1[i - 1];
                
                ans = max(ans,res + k - w);
              
            }
            else
            {
                int k = s0[n] - s0[i]; // 后面的0
                int w = s1[i - 1];
                ans = max(ans,res - k + w);
                
            }
        }
        
        cout << ans << endl;
        
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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

    六、F - Quests

    • 思路: 首先贪心思想,开始我们一定要选权值大的,所以我们给数组排一个降序
      1 无解的情况 : d * a[1] < c : d天每天选择最大的还是不行,就无解
      2 无穷大的情况: a[1] + a[2] + … a[d] >= c,前d天每天只选择不同的权值的和大于等于c
      3 其它情况就是直接二分k值, 就是相当于周期是(k + 1),所以只用下标枚举从1到k + 1即可,然后计算a[i]被选择了几次,求和(a[i] * w1 + a[i +1] * w2 + a[i + 1] * w3…a[k + 1] * wk + 1)然后与c比较 w就是a[i]被选择几次
    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 2e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int a[N];
    int c,d,n;
    bool check(int mid)
    {
        int sum = 0;
        
        for(int i = 1;i <= min(mid + 1,n);i ++ )
        {
    
            sum += a[i] * ((d + mid + 1 - i) / (mid + 1));
            
        }
        
       
        if(sum >= c)
        {
             return true;
        }
        
        return false;
    }
    bool cmp(int x,int y)
    {
        return x > y;
    }
    void solve()
    {
       cin >> n >> c >> d;
       int s = 0;
       for(int i = 1;i <= n;i ++ )
       {
            cin >> a[i];
       }
       
       sort(a + 1,a + 1 + n,cmp);
       for(int i = 1;i <= n;i ++ )
       if(i <= d)
       s += a[i];
       
       int sum = 0;
       sum = a[1] * d;
       if(sum < c) cout << "Impossible" << endl;
       else if(s >= c) cout << "Infinity" << endl;
       else
       {
           int l = 0,r = 1e9;
           
           while(l <= r)
           {
               int mid = (l + r) / 2;
               
               if(check(mid)) l = mid + 1;
               else r = mid - 1;
           }
           
          cout << r << endl;
       }
       
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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
    • 73
    • 74
    • 75
    • 76
    • 77

    七、G - SlavicG’s Favorite Problem

    • 思路: dfs从a点开始对每一个点 p 求一个从a开始到p的前缀异或和,然后记录这个值但是不遍历b点,然后再从b点来一次dfs,如果之前出现过就代表可以成功
    • 代码:
    #include 
    #define ios ios::sync_with_stdio(0),cin.tie(0)
    #define fi first
    #define se second
    #define pb push_back
    #define PII pair<int,int>
    #define int long long
    using namespace std;
    
    const int N = 1e5 + 100,M = N * 2,INF = 0x3f3f3f3f,mod = 998244353;
    int h[N], e[M], w[M], ne[M], idx;
    int x[N],y[N];
    map<int,int> mp;
    int n,a1,b1;
    bool f;
    
    void add(int a, int b, int c)
    {
        e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
    }
    
    void dfs(int u,int fa,int k)
    {
        mp[k] = 1;
        for(int i = h[u];i != -1; i = ne[i])
        {
            int j = e[i];
            if(j == fa) continue;
            if(j == b1) continue;
            
            dfs(j,u,k ^ w[i]);
        }
    }
    
    void dfs2(int u,int fa,int k)
    {
      
        for(int i = h[u];i != -1; i = ne[i])
        {
            int j = e[i];
            if(j == fa) continue;
            dfs2(j,u,k ^ w[i]);
            if(f) return;
            if(mp[k ^ w[i]]) 
            {
                f = true;
                return;
            }
        }
    
    }
    
    void solve()
    {
        cin >> n >> a1 >> b1;
        memset(h, -1, sizeof h);
        mp.clear();
        idx = 0;
        for(int i = 1;i <= n - 1;i ++ )
        {
             int u,v,q; cin >> u >> v >> q;
             add(u,v,q);
             add(v,u,q);
        }
        
        dfs(a1,-1,0);
         f = false;
        dfs2(b1,-1,0);
        
       
        if(f) cout << "YES" << endl;
        else cout << "NO" << endl;
        
       
    }
    
    signed main()
    {
        ios; 
        int T;cin >> T; while(T -- ) solve();
        
        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
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
  • 相关阅读:
    Prometheus+Ansible+Consul实现服务发现
    Blazor 发布WebAssembly使用Brotli 压缩提升初次加载速度
    零代码—无代码到底有什么影响,谁会胜出?
    bootstrap.xml 和applicaiton.properties和applicaiton.yml的区别和联系
    1151:素数个数
    【博客547】keepalived实现vip的原理剖析
    typescript之神奇的infer - 推断任何你想知道的类型
    i18n在VUE3中使用插槽动态传入组件
    JavaScript详解(一)
    vue3 defineProps defineEmits defineExpose
  • 原文地址:https://blog.csdn.net/weixin_52255583/article/details/127983510