• Codeforces Round #436 (Div. 2)【A、B、C、D、E】


    Codeforces Round #436 (Div. 2)

    敲出一身冷汗。。。感觉自己宛如智障:(

     codeforces 864 A. Fair Game【水】

    题意:已知n为偶数,有n张卡片,每张卡片上都写有一个数,两个人每人选一个数,每人可以拿的卡片必须写有是自己选的数,问能否选择两个数使得两个人每人拿的卡片数一样多并且能拿光卡片。[就是看输入是不是只有两种数字]

    //:第一遍我看成字符串包含有选的数字也能拿,,这样写着居然过了。。水题水题。。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int N=101;
     6 int n, a[N];
     7 int main() {
     8     int i, k, x=0, y=0;
     9     scanf("%d", &n);
    10     for(i = 1; i <= n; ++i){
    11         scanf("%d", &k);a[k]++;
    12         if(!x) x=k; else if(k!=x) y=k;
    13     }
    14     if(a[x]==a[y] && a[x]+a[y]==n){printf("YES
    %d %d
    ", x, y);}
    15     else puts("NO");
    16     return 0;
    17 }
    15ms

     codeforces 864 B. Polycarp and Letters【水】

    题意:给一个只包含大写字母和小写字母的字符串,现在有一个集合,它可以包含字符串中的不同小写字母的位置,但是要求其任意两个位置之间在字符串中不能有大写字母。求集合最大的大小。

    题解:线性暴力扫过去,求每段大写字母之间包含的最多的不同小写字母数量。

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<string>
     5 #include<algorithm>
     6 #include<set>
     7 using namespace std;
     8 const int N=201;
     9 int n;
    10 string s;
    11 set<char>st;
    12 int main() {
    13     int i, j, k, m = 0;
    14     scanf("%d", &n);
    15     cin >> s;   s += 'Z';
    16     for(i = 0; i <= n; ++i) {
    17         if(s[i] >= 'A' && s[i] <= 'Z') st.clear();
    18         else {st.insert(s[i]); m = max(m, (int)st.size());}
    19     }
    20     printf("%d
    ", m);
    21     return 0;
    22 }
    15ms

     codeforces 864 C. Bus【模拟】

    题意:有个x轴,汽车从0出发到x=a,再返回到0,一直做这样的往返运动,现在规定0->a或a->0都算一次旅行,并且一开始汽车油箱有b升油,每走一个单位要消耗一升油,现在知道x=f位置处有个加油站,每次经过可以选择加油或不加,加的话可以加满到b升,要求完成k次旅行,求最少的加油次数。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 int main() {
     6     int a, b, f, k, i, j;
     7     scanf("%d%d%d%d", &a, &b, &f, &k);
     8     int num = 0, ed = b;
     9     if(k>2&&b<2*f || k>1&&b<2*(a-f) || b<f || b<a-f) {puts("-1"); return 0;}
    10     for(i = 1; i <= k; ++i) {
    11         if(i == k && ed >= a) break;
    12         if(i%2) {
    13             if(ed<2*a-f) {num++; ed = b-(a-f);}
    14             else ed -= a;
    15         }
    16         else {
    17             if(ed<a+f) {num++; ed = b-f;}
    18             else ed -= a;
    19         }
    20     }
    21     printf("%d
    ", num);
    22     return 0;
    23 }
    15ms

     codeforces 864 D. Make a Permutation!【贪心】

    题意:每次可以任意改变数组中的一个数,要求把数组变成1~n,求改变数的最小数量和最后的字典序最小的数组。

    题解:因为要字典序最小,顺序访问没出现的数,将其与重复了的数进行判断,小的话直接替换,否则若重复的数已经被标记,则也进行替换。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int N = 2e5+1;
     6 int n, a[N], num[N], vis[N];
     7 int main() {
     8     int i, j, k, cnt = 0, x = 1;
     9     memset(vis, 0, sizeof(vis));
    10     memset(num, 0, sizeof(num));
    11     scanf("%d", &n);
    12     for(i = 0; i < n; ++i) {scanf("%d", &a[i]); num[a[i]]++;}
    13     for(i = 0; i < n; ++i) {
    14         for(; num[x]; ++x);
    15         if(num[a[i]]>1 && (x < a[i] || vis[a[i]])) {
    16             num[a[i]]--; num[a[i]=x]++; ++cnt;
    17         }
    18         else vis[a[i]] = 1;
    19     }
    20     printf("%d
    ", cnt);
    21     for(i = 0; i < n-1; ++i) printf("%d ", a[i]);
    22     printf("%d
    ", a[n-1]);
    23     return 0;
    24 }
    93ms

    codeforces 864 E. Fire【DP】

    题意:已知第i个文件:保存所需的时间为ti,到了时间di则会毁坏,和可得的价值pi。求能保存的文件的最大价值和,以及能保存的文件的编号。

    题解:给d小的赋予高优先级再对文件排序。dp[j]表示在j时间前保存的文件所能得到的最大价值和,并对选择保存的文件进行标记。

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 const int N = 101;
     6 const int M = 2001;
     7 int n;
     8 int dp[M], vis[N][M];
     9 struct node {
    10     int t, d, p, id;
    11     bool operator < (const node&r) const{
    12         return d < r.d;
    13     }
    14 }a[N];
    15 int b[N];
    16 int main() {
    17     int i, j, k, t, ed = 0, cnt = 0;
    18     memset(dp, 0, sizeof(dp)); memset(vis, 0, sizeof(vis));
    19     scanf("%d", &n);
    20     for(i = 1 ; i <= n; ++i) {
    21         scanf("%d%d%d", &a[i].t, &a[i].d, &a[i].p);
    22         a[i].id = i;
    23     }
    24     sort(a+1, a+1+n);
    25     for(i = 1; i <= n; ++i) {
    26         t = a[i].t;
    27         for(j = a[i].d-1; j >= t; --j) {
    28             if(dp[j] < dp[j-t]+a[i].p) {
    29                 dp[j] = dp[j-t] + a[i].p;
    30                 vis[i][j] = 1;
    31             }
    32         }
    33     }
    34     for(i = 1; i < a[n].d; ++i) if(dp[i]>dp[ed]) ed = i;
    35     printf("%d
    ", dp[ed]);
    36     for(i = n; i >= 1; --i) {
    37         if(vis[i][ed]) {b[cnt++] = a[i].id; ed -= a[i].t;}
    38     }
    39     printf("%d
    ", cnt);
    40     for(i = cnt-1; i > 0; --i) printf("%d ", b[i]);
    41     if(cnt) printf("%d
    ", b[0]);
    42     return 0;
    43 }
    15ms
  • 相关阅读:
    四,awk格式化
    printf命令详解
    三,awk变量
    二,awk分隔符
    一,AWK基础
    【leetcode_easy_array】1399. Count Largest Group
    【leetcode_easy_array】1184. Distance Between Bus Stops
    【leetcode_easy_array】1346. Check If N and Its Double Exist
    【leetcode_easy_array】1304. Find N Unique Integers Sum up to Zero
    【leetcode_easy_array】1337. The K Weakest Rows in a Matrix
  • 原文地址:https://www.cnblogs.com/GraceSkyer/p/7591345.html
Copyright © 2020-2023  润新知