Codeforces Round #632 (Div. 2)

比賽連接:https://codeforces.com/contest/1333ios

比賽的時候,寫D的時候用了cin,cout的優化結果仍是被卡了,看來cin,cout得少用。c++

 

A - Little Artem

給你n * m的矩陣,只有B和W,相鄰有其餘不同的點成爲好點,讓好點B的數量多於W。那麼直接讓左上角的一塊爲B其他爲W便可。數組

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <bits/stdc++.h>
 4 using namespace std;  5  
 6 const int N = 1e5 + 10;  7  
 8 int n, m;  9 int T; 10  
11 int main() 12 { 13     cin >> T; 14     while(T --) 15  { 16         scanf("%d%d", &n, &m); 17         for (int i = 1; i <= n; i ++) 18  { 19         
20         for (int j = 1; j <= m; j ++) 21  { 22             if(i == 1 && j == 1) 23  { 24                 cout << "W"; 25  } 26             else
27             cout << "B"; 28  } 29         cout << endl; 30  } 31  } 32 }
View Code

 

B - Kind Anton

給定一個n,長度爲n的a數組和b數組,a[i]∈{1, -1, 0},m爲任意整數,a[i]只能+a[j](j < i),那麼根據題意,從後向前模擬便可。ide

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <bits/stdc++.h>
 4 using namespace std;  5  
 6 const int N = 1e5 + 10;  7  
 8 int n, m;  9 int T; 10 int a[N]; 11 int b[N]; 12 map<int, int>s; 13  
14 int main() 15 { 16     cin >> T; 17     while(T --) 18  { 19  s.clear(); 20         scanf("%d", &n); 21         for (int i = 1; i <= n; i ++) 22             scanf("%d", &a[i]), s[a[i]]++; 23         for (int i = 1; i <= n; i ++) 24             scanf("%d", &b[i]); 25         bool flag = true, flag2 = false; 26         bool t1 = false; 27         bool t2 = false; 28         for (int i = n; i >= 1; i --) 29  { 30             s[a[i]] --; 31             if(a[i] == b[i]) continue; 32             if(a[i] < b[i]) 33  { 34                 if(!s[1]) 35  { 36                     flag = false; 37                     break; 38  } 39  } 40             if(a[i] > b[i]) 41  { 42                 if(!s[-1]) 43  { 44                     flag = false; 45                     break; 46  } 47  } 48  } 49         if(flag) 50  { 51             puts("YES"); 52  } 53         else
54  { 55             puts("NO"); 56  } 57  } 58     
59 }
View Code

 

 

C - Eugene and an array

給定長度爲n的序列,定義序列a爲「好的」,當且僅當,a的子段中不存在sum值爲0。那麼根據題意,若sum[a[i]]在i前面存在,那麼這樣的序列只能最後一次取到sum[a[i]]的下標k之後,數量爲i - k。優化

 

 1 #include<bits/stdc++.h> 
 2 using namespace std;  3 typedef long long ll;  4 const int N = 2e5+10;  5 ll a[N];  6 map<ll, ll>mp;  7 int main()  8 {  9  ll n; 10     scanf("%lld", &n); 11     for(int i = 1; i <= n; i++) 12     scanf("%lld", &a[i]); 13     ll sum = 0, res = 0, last = 0; 14     mp[0] = 1; 15     for(int i = 1; i <= n; i++) 16  { 17         sum += a[i]; 18         if(mp[sum]) last = max(last, mp[sum]); 19         res += i - last; 20         mp[sum] = i+1; 21  
22  } 23     printf("%lld\n", res); 24 }
View Code

 

D - Challenges in school №41

有一個n個箭頭箭頭序列,只能是L(左箭頭)或者R(右箭頭),每次操做能夠選一對相鄰的相對的箭頭變成相背的箭頭。每秒操做至少1次,求可以剛好k秒把整個箭頭序列變成沒有任何相對的箭頭。spa

那麼最後的序列確定是LLLL...LLRRR....R這樣,那麼能夠暴力算出每一輪能移多少而且至少移幾輪。由於n只有3000,最壞狀況下o(n²),知足條件。code

當出現RL的時候就把R的下標保存。當RLL的時候要移倆輪,因此每次有RL存在,i++。blog

那麼假設算出x輪,一共須要移y次。那麼存在有解的狀況的必定是x <= k && k <= y的。而後去貪心分配輪的次數,實現具體看代碼。排序

 1 #include<bits/stdc++.h> 
 2 using namespace std;  3 typedef long long ll;  4 const int N = 3e3+10;  5 char s[N];  6 int n, k;  7 vector<int>a[N];  8  
 9 int main() 10 { 11     scanf("%d%d", &n, &k); 12     scanf("%s", s + 1); 13     int cnt  = 0; 14     while(1) 15  { 16         cnt ++; 17         bool flag = true; 18         for (int i = 1; i < n; i ++) 19  { 20             if(s[i] == 'R' && s[i + 1] == 'L') 21  { 22                 flag = false; 23  a[cnt].push_back(i); 24                 swap(s[i], s[i + 1]); 25                 i ++; 26  } 27  } 28         if(flag) 29  { 30             break; 31  } 32  } 33     cnt --; 34     if(cnt == 0) 35  { 36         puts("-1"); 37         return 0; 38  } 39     ll res = 0; 40     for (int i = 1; i <= cnt; i ++) 41  { 42         res += a[i].size(); 43  } 44     if(res < k || cnt > k) 45  { 46         puts("-1"); 47         return 0; 48  } 49     ll tmp = k - cnt; 50     for (int i = 1; i <= n; i ++) 51  { 52         int t = a[i].size(); 53         if(tmp >= t - 1) 54  { 55             for (int j = 0; j < t; j ++) 56  { 57                 printf("1 %d\n", a[i][j]); 58  } 59             tmp -= t - 1; 60  } 61         else
62  { 63             if(tmp) 64  { 65                 for (int j = 0; j < tmp; j  ++) 66  { 67                         printf("1 %d\n", a[i][j]); 68  
69  } 70                 printf("%d ", t - tmp); 71                 for (int j = tmp; j < t; j ++) 72  { 73                     printf("%d ", a[i][j]); 74  } 75                 puts(""); 76                 tmp = 0; 77  } 78             else
79  { 80             
81                 
82                 printf("%d ", t); 83                 for (int j = 0; j <t; j ++) 84  { 85                 printf("%d ", a[i][j]); 86  } 87                 puts(""); 88             
89  } 90  } 91  } 92 }
View Code

 

E - Road to 1600(待補)

 

F - Kate and imperfection

 給[1,n]的連續天然數。對[2,n]的每一個k,都枚舉全部大小剛好爲k的子集,而後定義一個值爲f,其遍歷集合中全部的二元組,求出二元組的gcd,而後取這些gcd裏面的最大值,求f的最小值。ci

第一眼看到這個東西的時候一點思路都沒有,後來看了大佬們的思路,發現是真的很神奇,對於每個數(不管質數合數)x,都有一個最小質因子*一個數,那麼咱們就貪心,給n個數排序,確定是讓最小質因子慢慢增大(1,1, 1, 2, 2,...3,),纔會讓f的最大值最小。

那麼根據歐拉篩每一個合子都是被其最小質因子篩的。就能夠在o(n) + o(nlogn)的狀況下獲得答案。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<algorithm>
 6 #include<cmath>
 7 #include<vector>
 8 #include<set>
 9  
10 using namespace std; 11  
12 int read() 13 { 14     char c; 15     while (c = getchar(), c != '-' && (c < '0' || c > '9')); 16     bool flag = (c == '-'); 17     if (flag) 18         c = getchar(); 19     int x = 0; 20     while (c >= '0' && c <= '9') { 21         x = x * 10 + c - '0'; 22         c = getchar(); 23  } 24     return flag ? -x : x; 25 } 26  
27 const int MAXN = 500000; 28  
29 bool flag[MAXN + 1]; 30 int prime[MAXN], x[MAXN + 1]; 31 
32 
33 int main() { 34     int n = read(); 35     int total = 0; 36     x[1] = 1; 37     for (int i = 2; i <= n; i++) 38  { 39         if (!flag[i]) 40  { 41             prime[total++] = i; 42             x[i] = 1; 43  } 44         for (int j = 0; j < total && i * prime[j] <= n; j++) { 45             int k = i * prime[j]; 46             flag[k] = true; 47             x[k] = i; 48             if (!(i % prime[j])) 49                 break; 50  } 51  } 52     sort(x + 1, x + (n + 1)); 53     for (int i = 2; i <= n; i++) 54         printf("%d%c", x[i], (i == n) ? '\n' : ' '); 55     return 0; 56 }
View Code
相關文章
相關標籤/搜索