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
相关文章
相关标签/搜索