codeforces#510 Div2

pre过了三题 后来A题被hack了 B题终测挂了 两题其实都是有一个小细节没有处理好 中间C还因为cinT了一次

唉本来打的还不错的 还是太菜了 继续加油吧

 

A-Benches

有n张椅子 原来第i张上有ai个人 现在来了m个人

求最大值的最大和最小的可能

 1 #include <iostream>
 2 #include<algorithm>
 3 #include<stdio.h>
 4 #include<set>
 5 #include<cmath>
 6 #include<cstring>
 7 #include<map>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 
12 using namespace std;
13 
14 int n, m;
15 const int maxn = 105;
16 int a[maxn];
17 
18 int main()
19 {
20     while (cin >> n >> m) {
21         int maxpeo = -1;
22         for (int i = 0; i < n; i++) {
23             cin >> a[i];
24             maxpeo = max(maxpeo, a[i]);
25         }
26 
27         int maxk = maxpeo + m;
28         int sub = 0;
29         for (int i = 0; i < n; i++) {
30             sub += maxpeo - a[i];
31         }
32         int mink;
33         if(sub >= m){
34             mink = maxpeo;
35         }
36         else if ((m - sub) % n == 0) {
37             mink = (m - sub) / n + maxpeo;
38         }
39         else {
40             mink = (m - sub) / n + 1 + maxpeo;
41         }
42 
43         cout<<mink<<" "<<maxk<<endl;
44     }
45 }
View Code

 

B-Vitamins

有n瓶果汁,每瓶有一个价格 和 可能含有的维生素【最多只有ABC三种维生素】

现在想要用最少的钱集齐ABC

本来感觉是dp 写不出 所以先去写了C 后来发现其实贪心就能过

分类讨论一下 一种是 直接选三个维生素都有的里面价格最小的

一种是 选两个维生素加缺的里面价格最小的

一种是 选单个维生素价格最小的之和

存好含三种的果汁 两种的果汁 含A的果汁【不包括三种都有】含B的含C的

第一种可能只有一种情况

第二种可能 枚举 跑一遍

第三种可能也只有一种情况

比一下最小值

需要注意有可能有的数量是0

  1 #include <iostream>
  2 #include<algorithm>
  3 #include<stdio.h>
  4 #include<set>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<map>
  8 #include<vector>
  9 #include<queue>
 10 #include<stack>
 11 
 12 #define inf 0x3f3f3f3f
 13 
 14 using namespace std;
 15 
 16 const int maxn = 1005;
 17 int n;
 18 struct node {
 19     int c;
 20     char vitamin[5];
 21 }juice[maxn];
 22 node a[maxn], b[maxn], c[maxn], three[maxn], two[maxn];
 23 
 24 bool cmp(node a, node b)
 25 {
 26     return a.c < b.c;
 27 }
 28 
 29 int main()
 30 {
 31     while (scanf("%d", &n) != EOF) {
 32         int cnta = 0, cntb = 0, cntc = 0, cntth = 0, cnttwo = 0;
 33         for (int i = 0; i < n; i++) {
 34             scanf("%d %s", &juice[i].c, juice[i].vitamin);
 35             if (strlen(juice[i].vitamin) == 3) {
 36                 three[cntth++] = juice[i];
 37             }
 38             else if (strlen(juice[i].vitamin) == 2) {
 39                 two[cnttwo++] = juice[i];
 40                 for (int j = 0; j < 2; j++) {
 41                     if (juice[i].vitamin[j] == 'A') {
 42                         a[cnta++] = juice[i];
 43                     }
 44                     else if (juice[i].vitamin[j] == 'B') {
 45                         b[cntb++] = juice[i];
 46                     }
 47                     else {
 48                         c[cntc++] = juice[i];
 49                     }
 50                 }
 51             }
 52             else {
 53                 if (juice[i].vitamin[0] == 'A') {
 54                     a[cnta++] = juice[i];
 55                 }
 56                 else if (juice[i].vitamin[0] == 'B') {
 57                     b[cntb++] = juice[i];
 58                 }
 59                 else {
 60                     c[cntc++] = juice[i];
 61                 }
 62             }
 63         }
 64 
 65         if (cntth == 0 && (cnta == 0 || cntb == 0 || cntc == 0)) {
 66             printf("-1\n");
 67             continue;
 68         }
 69 
 70         sort(three, three + cntth, cmp);
 71         sort(two, two + cnttwo, cmp);
 72         sort(a, a + cnta, cmp);
 73         sort(b, b + cntb, cmp);
 74         sort(c, c + cntc, cmp);
 75 
 76         int ans;
 77         if(cntth == 0){
 78             ans = inf;
 79         }
 80         else{
 81             ans = three[0].c;
 82         }
 83         for (int i = 0; i < cnttwo; i++) {
 84             if (strcmp(two[i].vitamin, "AB") == 0 || strcmp(two[i].vitamin, "BA") == 0) {
 85                 ans = min(ans, two[i].c + c[0].c);
 86             }
 87             else if (strcmp(two[i].vitamin, "AC") == 0 || strcmp(two[i].vitamin, "CA") == 0) {
 88                 ans = min(ans, two[i].c + b[0].c);
 89             }
 90             else {
 91                 ans = min(ans, two[i].c + a[0].c);
 92             }
 93         }
 94         if(cnta != 0 && cntb != 0 && cntc != 0){
 95             ans = min(ans, a[0].c + b[0].c + c[0].c);
 96         }
 97 
 98 
 99         printf("%d\n", ans);
100     }
101 }
View Code

 

C-Array Product

给一组数列 做n-1次操作

有两种操作 :第一种将ai和aj相乘结果赋给aj,ai删除

第二种 将ai位置删除【这种操作最多只能一次】

其实也是贪心

首先 所有的0都要合并 然后删去

负数 如果是奇数个的话 删去绝对值最小的那个

如果既有0又有奇数个负数 就把这个负数和0合并再删去

剩下的这些全部照常做第一种操作就好了

  1 #include <iostream>
  2 #include<algorithm>
  3 #include<stdio.h>
  4 #include<set>
  5 #include<cmath>
  6 #include<cstring>
  7 #include<map>
  8 #include<vector>
  9 #include<queue>
 10 #include<stack>
 11 
 12 #define inf 0x3f3f3f3f
 13 
 14 using namespace std;
 15 
 16 int n;
 17 const int maxn = 2e5 + 5;
 18 int a[maxn];
 19 bool vis[maxn];
 20 
 21 int main()
 22 {
 23     //freopen("C:\\Users\\wyb\\Desktop\\tmpcode\\codeforces\\in.txt", "r", stdin);
 24     //freopen("C:\\Users\\wyb\\Desktop\\tmpcode\\codeforces\\out.txt", "w", stdout);
 25     while (scanf("%d", &n) != EOF) {
 26         memset(vis, 1, sizeof(vis));
 27         int cntmin = 0, lastzero = -1, maxminpos, maxmin = -inf, cnt = 0;
 28         for (int i = 1; i <= n; i++) {
 29             scanf("%d", &a[i]);
 30             if (a[i] == 0) {
 31                 if (lastzero == -1) {
 32                     lastzero = i;
 33                 }
 34                 else {
 35                     printf("1 %d %d\n", lastzero, i);
 36                     //cout << "1 " << lastzero << " " << i << endl;
 37                     vis[lastzero] = false;
 38                     lastzero = i;
 39                     cnt++;
 40                 }
 41             }
 42             if (a[i] < 0) {
 43                 cntmin++;
 44                 if (maxmin < a[i]) {
 45                     maxmin = a[i];
 46                     maxminpos = i;
 47                 }
 48             }
 49         }
 50         if (cntmin % 2 && lastzero != -1) {
 51             if(cnt == n - 1){
 52                 continue;
 53             }
 54             cnt++;
 55             printf("1 %d %d\n", maxminpos, lastzero);
 56             //cout << "1 " << maxminpos << " " << lastzero << endl;
 57             if(cnt == n - 1){
 58                 continue;
 59             }
 60             cnt++;
 61             printf("2 %d\n", lastzero);
 62             //cout << "2 " << lastzero << endl;
 63             vis[maxminpos] = vis[lastzero] = false;
 64         }
 65         else if (lastzero != -1) {
 66             if(cnt == n - 1){
 67                 continue;
 68             }
 69             cnt++;
 70             printf("2 %d\n", lastzero);
 71             //cout << "2 " << lastzero << endl;
 72             vis[lastzero] = false;
 73         }
 74         else if(cntmin % 2){
 75             if(cnt == n - 1){
 76                 continue;
 77             }
 78             cnt++;
 79             printf("2 %d\n", maxminpos);
 80             //cout<<"2 "<<maxminpos<<endl;
 81             vis[maxminpos] = false;
 82         }
 83 
 84         int last = -1;
 85         for (int i = 1; i <= n; i++) {
 86             if (vis[i]) {
 87                 if (last == -1) {
 88                     last = i;
 89                 }
 90                 else {
 91                     if(cnt == n - 1){
 92                         break;
 93                     }
 94                     cnt++;
 95                     printf("1 %d %d\n", last, i);
 96                     //cout << "1 " << last << " " << i << endl;
 97                     last = i;
 98                 }
 99             }
100         }
101         //cout<<endl;
102     }
103 }
View Code

 

posted @ 2018-09-17 20:53  wyboooo  阅读(165)  评论(0编辑  收藏  举报