1. 程式人生 > >Codeforces Round #534 (Div. 2) Solution

Codeforces Round #534 (Div. 2) Solution

ces char close sta () dig urn %d for

A. Splitting into digits

Solved.

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 int n;
 5 
 6 void solve()
 7 {
 8     printf("%d\n", n);
 9     for (int i = 1; i <= n; ++i) printf("%d%c", 1, " \n"[i == n]);
10 }
11 
12 int main()
13 {
14     while (scanf("%d", &n) != EOF)
15 solve(); 16 return 0; 17 }
View Code

B. Game with string

Solved.

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define N 100010
 5 char s[N];
 6 
 7 int main()
 8 {
 9     while (scanf("%s", s + 1) != EOF)
10     {
11         stack <char> sta;
12         int cnt = 0
; 13 for (int i = 1, len = strlen(s + 1); i <= len; ++i) 14 { 15 if (!sta.empty() && sta.top() == s[i]) sta.pop(), ++cnt; 16 else sta.push(s[i]); 17 } 18 puts(cnt & 1 ? "Yes" : "No"); 19 } 20 return 0; 21 }
View Code

C. Grid game

Solved.

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 #define N 1010
 5 char s[N];
 6 
 7 int main()
 8 {
 9     while (scanf("%s", s + 1) != EOF)
10     {
11         int x = 0, y = 0;
12         for (int i = 1, len = strlen(s + 1); i <= len; ++i)
13         {
14             if (s[i] == 1)
15             {
16                 printf("%d %d\n", 1, x % 2 ? 3 : 1);   
17                 ++x;
18             }
19             else
20             {
21                 printf("%d %d\n", 3, y % 4 + 1);
22                 ++y;
23             }
24         }
25     }
26     return 0;
27 }
View Code

D. Game with modulo

Solved.

題意:

交互題

猜數,猜一個$a$

每次詢問格式為$(x, y)$

返回結果有兩種

$x \;if (x \; mod\; a) >= (y \;mod\; a)$

$y \;if (x \;mod\; a) < (y \;mod\; a) $

思路:

我們考慮 從小到大倍增,去找到$a的一個單調範圍$

比如說$1, 2, 4, 8, 16, 32 如果某一次詢問中返回了x$

那麽說明$a在詢問的(x, y)範圍中 並且2 \cdot a 不在這個範圍內$

因為是從小到大進行倍增

那麽我們考慮某一次詢問是$(x, 2\cdot x)$

$a在其中$

$如果 a > x, 那麽必然有x >= 2 \cdot x - a$

$如果a = x 那麽必然有 x \;mod\;a = 2 \cdot x \;mod\; a$

那麽這個區間就具有一個單調性,可以進行二分

技術分享圖片
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 char op[110];
 5 
 6 bool check(int x, int y)
 7 {
 8     printf("? %d %d\n", x, y);
 9     fflush(stdout); 
10     scanf("%s", op);
11     return op[0] == y;  
12 }
13 
14 void solve(int l, int r)
15 {
16     int base = 1;
17     for (int i = l; i <= r; i += base, base <<= 1)
18     {
19         printf("? %d %d\n", i, i + base);
20         fflush(stdout);
21         scanf("%s", op);
22         if (op[0] == x)
23         {
24             int l = i + 1, r = i + base - 1, res = -1;
25             while (r - l >= 0)
26             {
27                 int mid = (l + r) >> 1;
28                 if (check(i, mid))
29                 {
30                     l = mid + 1;  
31                     res = mid;
32                 }
33                 else
34                 {
35                     r = mid - 1;
36                 }
37             }    
38             if (res == -1) res = i; 
39             printf("! %d\n", res + 1);
40             fflush(stdout);
41             return;
42         }
43     }
44     
45 }
46 
47 int main()
48 {
49     while (scanf("%s", op) && op[0] != e)
50         solve(0, 1000000000);    
51     return 0;
52 }
View Code

Codeforces Round #534 (Div. 2) Solution