Educational Codeforces Round 21 Problem A - C

Problem A Lucky Year

 

题目传送门[here]

  题目大意是说,只有一个数字非零的数是幸运的,给出一个数,求下一个幸运的数是多少。

  这个幸运的数不是最高位的数字都是零,于是只跟最高位有关,只保留最高位,然后加一求差就是答案。

Code

 1 /**
 2  * Codeforces
 3  * Problem#808A
 4  * Accepted
 5  * Time:15ms
 6  * Memory:0k
 7  */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<ctime>
11 #include<cctype>
12 #include<cstring>
13 #include<cstdlib>
14 #include<fstream>
15 #include<sstream>
16 #include<algorithm>
17 #include<map>
18 #include<set>
19 #include<stack>
20 #include<queue>
21 #include<vector>
22 #include<stack>
23 using namespace std;
24 typedef bool boolean;
25 #define inf 0xfffffff
26 #define smin(a, b) a = min(a, b)
27 #define smax(a, b) a = max(a, b)
28 #define max3(a, b, c) max(a, max(b, c))
29 #define min3(a, b, c) min(a, min(b, c))
30 template<typename T>
31 inline boolean readInteger(T& u){
32     char x;
33     int aFlag = 1;
34     while(!isdigit((x = getchar())) && x != '-' && x != -1);
35     if(x == -1) {
36         ungetc(x, stdin);
37         return false;
38     }
39     if(x == '-'){
40         x = getchar();
41         aFlag = -1;
42     }
43     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
44     ungetc(x, stdin);
45     u *= aFlag;
46     return true;
47 }
48 
49 int power[10];
50 
51 int n;
52 int bits = 0;
53 
54 inline void init() {
55     power[0] = 1;
56     for(int i = 1; i <= 10; i++)
57         power[i] = power[i - 1] * 10;
58 }
59 
60 inline void solve() {
61     readInteger(n);
62     while(power[bits] <= n)    bits++;
63     int high = n - n % power[bits - 1];
64     int next = high + power[bits - 1];
65     printf("%d\n", next - n);
66 }
67 
68 int main() {
69     init();
70     solve();
71     return 0;
72 }
Problem A

Problem B Average Sleep Time

题目传送门[here]

  题目大意是说,给出n个数ai和k,再得到n - k + 1个新数,第i个新数为,再求这几个新数的平均数。

  有两种方法,第一种是前缀和暴力乱搞。第二种是特殊处理两段的数被求和的次数,中间的都是k,然后就可以求和了,然后就可以求平均数。

  我呢,用的第一种方法,因为懒。

Code

 1 /**
 2  * Codeforces
 3  * Problem#808B
 4  * Accepted
 5  * Time:30ms
 6  * Memory:2400k
 7  */
 8 #include<iostream>
 9 #include<cstdio>
10 #include<ctime>
11 #include<cctype>
12 #include<cstring>
13 #include<cstdlib>
14 #include<fstream>
15 #include<sstream>
16 #include<algorithm>
17 #include<map>
18 #include<set>
19 #include<stack>
20 #include<queue>
21 #include<vector>
22 #include<stack>
23 using namespace std;
24 typedef bool boolean;
25 #define inf 0xfffffff
26 #define smin(a, b) a = min(a, b)
27 #define smax(a, b) a = max(a, b)
28 #define max3(a, b, c) max(a, max(b, c))
29 #define min3(a, b, c) min(a, min(b, c))
30 template<typename T>
31 inline boolean readInteger(T& u){
32     char x;
33     int aFlag = 1;
34     while(!isdigit((x = getchar())) && x != '-' && x != -1);
35     if(x == -1) {
36         ungetc(x, stdin);
37         return false;
38     }
39     if(x == '-'){
40         x = getchar();
41         aFlag = -1;
42     }
43     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
44     ungetc(x, stdin);
45     u *= aFlag;
46     return true;
47 }
48 
49 #define LL long long
50 
51 int n, k;
52 LL *sum;
53 int* a;
54 double w;
55 
56 inline void init() {
57     readInteger(n);
58     readInteger(k);
59     w = n - k + 1;
60     sum = new LL[(const int)(n + 1)];
61     a = new int[(const int)(n + 1)];
62     sum[0] = 0;
63     for(int i = 1; i <= n; i++) {
64         readInteger(a[i]);
65         sum[i] = sum[i - 1] + a[i];
66     }
67 }
68 
69 LL s = 0;
70 double avg = 0.0;
71 
72 inline void solve() {
73     for(int i = k; i <= n; i++) {
74         s += sum[i] - sum[i - k];
75     }
76     avg = s / w;
77     printf("%.9lf", avg);
78 }
79 
80 int main() {
81     init();
82     solve();
83     return 0;
84 }
Problem B

Problem C Tea Party

题目传送门[here]

  语文不好,题目大意就不给了(显然是太懒了),去看原题吧,看不懂扔给谷歌机翻。

  首先呢给每人达到最低要求的茶叶量,这时判一下是否合法。(然后跳过一个if吧)。

  接着很容易想到一个符合题意的贪心,给茶杯最大的人加尽可能多的茶叶(能加满就加满),如果还有剩的,就给茶杯第二大的人加....

  这个显然是合法,不会出现让顾客不满意的情况。

Code

  1 /**
  2  * Codeforces
  3  * Problem#808C
  4  * Accepted
  5  * Time:15ms
  6  * Memory:0k
  7  */
  8 #include<iostream>
  9 #include<cstdio>
 10 #include<ctime>
 11 #include<cctype>
 12 #include<cstring>
 13 #include<cstdlib>
 14 #include<fstream>
 15 #include<sstream>
 16 #include<algorithm>
 17 #include<map>
 18 #include<set>
 19 #include<stack>
 20 #include<queue>
 21 #include<vector>
 22 #include<stack>
 23 using namespace std;
 24 typedef bool boolean;
 25 #define inf 0xfffffff
 26 #define smin(a, b) a = min(a, b)
 27 #define smax(a, b) a = max(a, b)
 28 #define max3(a, b, c) max(a, max(b, c))
 29 #define min3(a, b, c) min(a, min(b, c))
 30 template<typename T>
 31 inline boolean readInteger(T& u){
 32     char x;
 33     int aFlag = 1;
 34     while(!isdigit((x = getchar())) && x != '-' && x != -1);
 35     if(x == -1) {
 36         ungetc(x, stdin);
 37         return false;
 38     }
 39     if(x == '-'){
 40         x = getchar();
 41         aFlag = -1;
 42     }
 43     for(u = x - '0'; isdigit((x = getchar())); u = (u << 1) + (u << 3) + x - '0');
 44     ungetc(x, stdin);
 45     u *= aFlag;
 46     return true;
 47 }
 48 
 49 typedef class Teacup {
 50     public:
 51         int val;
 52         int pos;
 53         
 54         Teacup(int val = 0, int pos = 0):val(val), pos(pos) {        }
 55         
 56         boolean operator < (Teacup b) const {
 57             if(val != b.val)    return val > b.val;
 58             return pos < b.pos;
 59         }
 60 }Teacup;
 61 
 62 int n;
 63 int w;
 64 int *a;
 65 int s = 0;
 66 int *b;
 67 Teacup* tc;
 68 
 69 inline void init() {
 70     readInteger(n);
 71     readInteger(w);
 72     a = new int[(const int)(n + 1)];
 73     b = new int[(const int)(n + 1)];
 74     for(int i = 1; i <= n; i++) {
 75         readInteger(a[i]);
 76         b[i] = (a[i] + 1) / 2;
 77         s += b[i];
 78     }
 79 }
 80 
 81 inline void solve() {
 82     if(s > w) {
 83         puts("-1");
 84         return;
 85     }
 86     w -= s;
 87     tc = new Teacup[(const int)(n + 1)];
 88     for(int i = 1; i <= n; i++)
 89         tc[i] = Teacup(a[i], i);
 90     sort(tc + 1, tc + n + 1);
 91     int i = 0;
 92     while(w) {
 93         i++;
 94         int delta = min(w, tc[i].val - b[tc[i].pos]);
 95         b[tc[i].pos] += delta;
 96         w -= delta;
 97     }
 98     for(int i = 1; i <= n; i++) {
 99         printf("%d ", b[i]);
100     }
101 }
102 
103 int main() {
104     init();
105     solve();
106     return 0;
107 }
Problem C
posted @ 2017-06-03 12:20  阿波罗2003  阅读(156)  评论(0编辑  收藏  举报