zoukankan      html  css  js  c++  java
  • Codeforces Round #587 (Div. 3)

    题目链接:https://codeforces.com/contest/1216


    A:

    题意:给出一个仅有a,b组成的字符串,可执行操作把a换成b,b换成a,问最小操作次数,使得任意前偶数里a,b的数量相等。

    idea:遍历一遍就好了,不满足条件的就换。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4  
     5 using namespace std;
     6  
     7 int n, ans;
     8 string a;
     9  
    10 int main()
    11 {
    12     cin >> n >> a;
    13     int len = n;
    14     if (n % 2 != 0)  len -= 1;
    15     
    16     for (int i = 0; i < len; i ++ )
    17     {
    18         if (a[i] == 'a')
    19         {
    20             if (a[i + 1] == 'b')  i ++ ;
    21             else
    22             {
    23                 a[i + 1] = 'b';
    24                 ans ++ ;
    25                 i ++ ;
    26             }
    27         }
    28         else
    29         {
    30             if (a[i + 1] == 'a')  i ++ ;
    31             else 
    32             {
    33                 a[i + 1] = 'a';
    34                 ans ++ ;
    35                 i ++ ;
    36             }
    37         }
    38     }
    39     
    40     cout << ans << endl;
    41     cout << a;
    42     return 0;
    43     
    44 }
    View Code

    B:

    题意:射击n个目标,每次会有不同的消耗,问按什么顺序射击可以消耗最少。

    idea:贪心,排序后,从最大的开始依次射击。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4  
     5 using namespace std;
     6 const int MAXN = 1000 + 10;
     7 int n, ans, a[MAXN], b[MAXN];
     8  
     9 bool cmp(int x, int y)
    10 {
    11     return x > y;
    12 }
    13  
    14 int main()
    15 {
    16     cin >> n;
    17     for (int i = 0; i < n; i ++ )
    18     {
    19         cin >> a[i];
    20         b[i] = a[i];
    21     }
    22     
    23     sort(a, a + n, cmp);
    24     
    25     for (int i = 0; i < n; i ++ )
    26     {
    27         ans += a[i] * i + 1;
    28     }
    29     
    30     cout << ans << endl;
    31     for (int i = 0; i < n; i ++ )
    32     {
    33         for (int j = 0; j < n; j ++ )
    34         {
    35             if (a[i] == b[j])
    36             {
    37                 cout << j + 1 << " ";
    38                 b[j] = -1;
    39             }
    40         }
    41     }
    42     return 0;
    43 }
    View Code

    D:

    题意:n种剑,每种剑有m把,来了k个人,每人偷走了s把剑,且每个人偷走的都是同一类型的剑,以知偷走后剩余的剑的数量a1,a2 .. an,问最少偷剑的人是多少。

    idea:拿剩余数量中最大的数和其它an相减,计算出相减这些数的最大公约数就是每个人偷走的剑的数量,差值除gcd累加就是人数

     1 include <iostream>
     2 #include <cstdio>
     3 #include <bits/stdc++.h>
     4  
     5 using namespace std;
     6 typedef long long ll;
     7 const int MAXN = 2e5 + 10;
     8 ll n, a[MAXN], b[MAXN], ss;
     9  
    10 int main()
    11 {
    12     scanf("%lld",&n);
    13     for (int i = 0; i < n; i ++ )
    14     {
    15         scanf("%lld",&a[i]);
    16         ss = max(a[i], ss);
    17     }
    18     for (int i = 0; i < n; i ++ )  b[i] = ss - a[i];
    19     
    20     ll c = 0;
    21     for (int i = 0; i < n; i ++ )
    22     {
    23         c = __gcd(c,b[i]);
    24     }
    25     ll ans = 0;
    26     for (int i = 0; i < n; i ++ )
    27     {
    28         ans += b[i] / c;
    29     }
    30     cout << ans << " " << c;
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    MTK android flash配置
    MTK平台缩写
    百度员工离职总结:如何做个好员工
    android 分区layout以及虚拟内存布局-小结
    Android eMMC Booting
    Android gingerbread eMMC booting
    Chrome插件开发
    jsonp解决CORS问题
    【好玩】将js代码转为日式表情
    多行文本溢出显示省略号(...)的方法
  • 原文地址:https://www.cnblogs.com/chuyds/p/11572650.html
Copyright © 2011-2022 走看看