Educational Codeforces Round 20 

A. Maximal Binary Matrix

直接从上到下从左到右填,注意只剩一个要填的位置的情况

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 111;
  7. bool vis[MAXN][MAXN];
  8. void solve(){
  9. int n, k; cin >> n >> k;
  10. if(n*n<k) cout << -1 << endl;
  11. else{
  12. for(int i = 1; i <= n and k; i++) for(int j = 1; j <= n and k; j++){
  13. if(vis[i][j]) continue;
  14. if(k==1){
  15. if(i!=j) continue;
  16. vis[i][j] = true;
  17. k--; break;
  18. }
  19. vis[i][j] = vis[j][i] = true;
  20. if(i==j) k -= 1;
  21. else k -= 2;
  22. if(!k) break;
  23. }
  24. for(int i = 1; i <= n; i++){
  25. for(int j = 1; j <= n; j++) cout << (vis[i][j] ? 1 : 0) << ' ';
  26. cout << endl;
  27. }
  28. }
  29. }
  30. int main(){
  31. #ifndef ONLINE_JUDGE
  32. freopen("Local.in","r",stdin);
  33. freopen("ans.out","w",stdout);
  34. #endif
  35. solve();
  36. return 0;
  37. }

B. Distances to Zero

把所有\(0\)的位置记下来然后二分找最近位置即可

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 2e5+7;
  7. void solve(){
  8. ____();
  9. int n;
  10. cin >> n;
  11. vector<int> vec(n);
  12. for(int &x : vec) cin >> x;
  13. vector<int> zeropos;
  14. for(int i = 0; i < n; i++) if(!vec[i]) zeropos.push_back(i);
  15. for(int i = 0; i < n; i++){
  16. auto p = lower_bound(zeropos.begin(),zeropos.end(),i);
  17. int ret = MAXN;
  18. if(p!=zeropos.end()) ret = *p - i;
  19. if(p!=zeropos.begin()){
  20. p--;
  21. ret = min(ret,i-*p);
  22. }
  23. cout << ret << ' ';
  24. }cout << endl;
  25. }
  26. int main(){
  27. #ifndef ONLINE_JUDGE
  28. freopen("Local.in","r",stdin);
  29. freopen("ans.out","w",stdout);
  30. #endif
  31. solve();
  32. return 0;
  33. }

C. Maximal GCD

考虑枚举\(g=gcd(A)\),由于要递增,所以填的方式为\(g,2g,3g,\cdots kg+l\)

显然要满足\(g\mid \sum A_i\),找最大的满足条件的\(g\)即可

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. typedef long long int LL;
  7. void solve(){
  8. LL n, k;
  9. cin >> n >> k;
  10. if(k>1e7 or (k+1)*k/2>n) cout << -1 << endl;
  11. else{
  12. vector<LL> f;
  13. for(LL i = 1; i * i <= n; i++){
  14. if(n%i) continue;
  15. f.push_back(i);
  16. if(i!=n/i) f.push_back(n/i);
  17. }
  18. sort(f.begin(),f.end());
  19. LL tot = (k + 1) * k / 2;
  20. LL g = 1;
  21. for(auto d : f){
  22. if(tot * d > n) break;
  23. g = max(g,d);
  24. }
  25. vector<LL> ret;
  26. for(int i = 0; i < k; i++){
  27. ret.push_back((i+1)*g);
  28. n -= (i+1)*g;
  29. }
  30. ret.back() += n;
  31. for(auto x : ret) cout << x << ' ';
  32. cout << endl;
  33. }
  34. }
  35. int main(){
  36. #ifndef ONLINE_JUDGE
  37. freopen("Local.in","r",stdin);
  38. freopen("ans.out","w",stdout);
  39. #endif
  40. solve();
  41. return 0;
  42. }

D.Magazine Ad

把所有串按空格和横杠分开,得到一些短的串,每个串的权值为长度,问题转化为把\(n\)个数分成最多\(k\)组,求权值最大的组的最小值,二分即可

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 1e6+7;
  7. int k;
  8. void solve(){
  9. ____();
  10. string s;
  11. cin >> k;
  12. cin.get(); getline(cin,s);
  13. s.push_back('-');
  14. // binary search
  15. vector<int> pos;
  16. pos.push_back(-1);
  17. for(int i = 0; i < s.length(); i++) if(s[i]=='-' or s[i]==' ') pos.push_back(i);
  18. vector<int> seg;
  19. for(int i = 1; i < pos.size(); i++) seg.push_back(pos[i] - pos[i-1]);
  20. seg.back()--;
  21. auto check = [&](int m){
  22. int cnt = 0, tot = 0;
  23. for(int i = 0; i < seg.size(); i++){
  24. if(seg[i]>m) return false;
  25. if(tot + seg[i] <= m) tot += seg[i];
  26. else{
  27. cnt++;
  28. tot = seg[i];
  29. }
  30. }
  31. cnt++;
  32. return cnt <= k;
  33. };
  34. int l = 1, r = s.length();
  35. while(l<=r){
  36. int mid = (l + r) >> 1;
  37. if(check(mid)) r = mid - 1;
  38. else l = mid + 1;
  39. }
  40. cout << l << endl;
  41. }
  42. int main(){
  43. #ifndef ONLINE_JUDGE
  44. freopen("Local.in","r",stdin);
  45. freopen("ans.out","w",stdout);
  46. #endif
  47. solve();
  48. return 0;
  49. }

E. Roma and Poker

考虑\(DP\),\(dp[i][j]\)表示到第\(i\)个位置赢和输的差值为\(j\)的情况下的上一个状态

转移的时候注意不能从差值的绝对值为\(k\)的状态转移过来

最后倒推回去输出答案就好了

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 1e3+7;
  7. const int D = 1e3+3;
  8. const int INF = 0x3f3f3f3f;
  9. int f[MAXN][MAXN<<1];
  10. int n, k;
  11. char s[MAXN];
  12. void solve(){
  13. ____();
  14. cin >> n >> k >> s + 1;
  15. memset(f,0x3f,sizeof(f));
  16. f[0][D] = -1;
  17. for(int i = 1; i <= n; i++){
  18. if(s[i]=='L' or s[i]=='?'){ // lose
  19. for(int j = D - k + 1; j <= D + k - 1; j++){
  20. if(f[i-1][j]==INF) continue;
  21. f[i][j-1] = j;
  22. }
  23. }
  24. if(s[i]=='W' or s[i]=='?'){ // win
  25. for(int j = D - k + 1; j <= D + k - 1; j++){
  26. if(f[i-1][j]==INF) continue;
  27. f[i][j+1] = j;
  28. }
  29. }
  30. if(s[i]=='D' or s[i]=='?'){
  31. for(int j = D - k + 1; j <= D + k - 1; j++){
  32. if(f[i-1][j]==INF) continue;
  33. f[i][j] = j;
  34. }
  35. }
  36. }
  37. if(f[n][D+k]==INF and f[n][D-k]==INF) cout << "NO" << endl;
  38. else{
  39. int u;
  40. string ret;
  41. if(f[n][D+k]!=INF) u = D + k;
  42. else u = D - k;
  43. for(int i = n; i >= 1; i--){
  44. if(u - f[i][u] == 0) ret.push_back('D');
  45. else if(u - f[i][u] == 1) ret.push_back('W');
  46. else ret.push_back('L');
  47. u = f[i][u];
  48. }
  49. reverse(ret.begin(),ret.end());
  50. cout << ret << endl;
  51. }
  52. }
  53. int main(){
  54. #ifndef ONLINE_JUDGE
  55. freopen("Local.in","r",stdin);
  56. freopen("ans.out","w",stdout);
  57. #endif
  58. solve();
  59. return 0;
  60. }

F. Coprime Subsequences

\(f(n)\)表示组合的\(gcd=n\)的方案数

\(F(n)\)表示组合的\(n\mid gcd\)的方案数

那么\(F(n) = \sum_{n\mid d}f(d)\)

根据莫比乌斯反演,\(f(n) = \sum_{n\mid d}\mu(\frac dn)F(d)\)

我们要求的答案就是\(f(1)\)

容易发现如果有\(x\)个数的因子有\(d\)那么\(F(d) = 2^x-1\)

那么我们就能很容易算出\(f(1)\)了

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MOD = 1e9+7;
  7. const int MAXN = 1e5+7;
  8. int n, prime[MAXN], pri_cnt, mu[MAXN], cnt[MAXN], pw[MAXN];
  9. bool npm[MAXN];
  10. vector<int> ds[MAXN];
  11. void sieve(){
  12. mu[1] = 1;
  13. for(int i = 2; i < MAXN; i++){
  14. if(!npm[i]) prime[++pri_cnt] = i, mu[i] = -1;
  15. for(int j = 1; i * prime[j] < MAXN; j++){
  16. npm[i*prime[j]] = true;
  17. if(i%prime[j]==0){
  18. mu[i*prime[j]] = 0;
  19. break;
  20. }
  21. mu[i*prime[j]] = -mu[i];
  22. }
  23. }
  24. for(int i = 1; i < MAXN; i++) for(int j = i; j < MAXN; j += i) ds[j].push_back(i);
  25. pw[0] = 1;
  26. for(int i = 1; i < MAXN; i++) pw[i] = pw[i-1] * 2 % MOD;
  27. }
  28. void solve(){
  29. ____();
  30. sieve();
  31. cin >> n;
  32. for(int i = 1; i <= n; i++){
  33. int x; cin >> x;
  34. for(int d : ds[x]) cnt[d]++;
  35. }
  36. int ret = 0;
  37. for(int i = 1; i < MAXN; i++) ret = (ret + mu[i] * 1ll * (pw[cnt[i]] - 1)) % MOD;
  38. cout << (ret + MOD) % MOD << endl;
  39. }
  40. int main(){
  41. #ifndef ONLINE_JUDGE
  42. freopen("Local.in","r",stdin);
  43. freopen("ans.out","w",stdout);
  44. #endif
  45. solve();
  46. return 0;
  47. }

G. Periodic RMQ Problem

由于范围比较大,考虑离散化坐标,为了能找到最小值,需要在离散化坐标相邻的两点中间额外加入一个区间最小值的点,这个点的值可以用\(ST\)表来找,加的点的值根据两点实际坐标的距离和位置稍微分类一下就好了,然后就很好搞了

view code
  1. //#pragma GCC optimize("O3")
  2. //#pragma comment(linker, "/STACK:1024000000,1024000000")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  6. const int MAXN = 6e5 + 7;
  7. const int INF = 0x3f3f3f3f;
  8. int n, k, A[MAXN][20];
  9. struct Qs{ int type, l, r, x; }Q[MAXN];
  10. struct SegTree{
  11. #define ls(rt) rt << 1
  12. #define rs(rt) rt << 1 | 1
  13. int minn[MAXN<<2], l[MAXN<<2], r[MAXN<<2], lazy[MAXN<<2];
  14. #define pushup(rt) minn[rt] = min(minn[ls(rt)],minn[rs(rt)])
  15. void build(int L, int R, vector<pair<int,int> > &vcc, int rt = 1){
  16. l[rt] = L, r[rt] = R;
  17. if(L + 1 == R){
  18. minn[rt] = vcc[L].second;
  19. return;
  20. }
  21. int mid = (L + R) >> 1;
  22. build(L,mid,vcc,ls(rt)); build(mid,R,vcc,rs(rt));
  23. pushup(rt);
  24. }
  25. void pushdown(int rt){
  26. if(!lazy[rt]) return;
  27. lazy[ls(rt)] = lazy[rs(rt)] = minn[ls(rt)] = minn[rs(rt)] = lazy[rt];
  28. lazy[rt] = 0;
  29. }
  30. void modify(int L, int R, int x, int rt = 1){
  31. if(L>=r[rt] or l[rt]>=R) return;
  32. if(L<=l[rt] and r[rt]<=R){
  33. lazy[rt] = minn[rt] = x;
  34. return;
  35. }
  36. pushdown(rt);
  37. modify(L,R,x,ls(rt)); modify(L,R,x,rs(rt));
  38. pushup(rt);
  39. }
  40. int query(int L, int R, int rt = 1){
  41. if(L>=r[rt] or l[rt]>=R) return INF;
  42. if(L<=l[rt] and r[rt]<=R) return minn[rt];
  43. pushdown(rt);
  44. return min(query(L,R,ls(rt)),query(L,R,rs(rt)));
  45. }
  46. }ST;
  47. void solve(){
  48. ____();
  49. cin >> n >> k;
  50. for(int i = 0; i < n; i++) cin >> A[i][0], A[i+n][0] = A[i][0];
  51. for(int j = 1; (1 << j) <= (n << 1); j++) for(int i = 0; i + (1 << j) - 1 < (n << 1); i++) A[i][j] = min(A[i][j-1],A[i+(1<<(j-1))][j-1]);
  52. auto query = [&](int l, int r){
  53. int d = (int)log2(r - l + 1);
  54. return min(A[l][d],A[r-(1<<d)+1][d]);
  55. };
  56. int q; cin >> q;
  57. vector<int> vec;
  58. for(int i = 0; i < q; i++){
  59. cin >> Q[i].type >> Q[i].l >> Q[i].r;
  60. Q[i].l--; Q[i].r--;
  61. if(Q[i].type==1) cin >> Q[i].x;
  62. vec.push_back(Q[i].l); vec.push_back(Q[i].r);
  63. }
  64. sort(vec.begin(),vec.end());
  65. vec.erase(unique(vec.begin(),vec.end()),vec.end());
  66. vector<pair<int,int> > vcc;
  67. for(int i = 1, lim = vec.size(); i < lim; i++){
  68. if(vec[i]==vec[i-1]+1) continue;
  69. if(vec[i]-vec[i-1]+1>=n) vcc.push_back({vec[i-1]+1,query(0,n-1)});
  70. else{
  71. int l = vec[i-1] % n, r = vec[i] % n;
  72. if(l>r) r += n;
  73. vcc.push_back({vec[i-1]+1,query(l+1,r-1)});
  74. }
  75. }
  76. for(int i = 0; i < (int) vec.size(); i++) vcc.push_back({vec[i],A[vec[i]%n][0]});
  77. sort(vcc.begin(),vcc.end());
  78. ST.build(0,vcc.size(),vcc);
  79. for(int i = 0; i < q; i++){
  80. int l = Q[i].l, r = Q[i].r;
  81. l = lower_bound(vcc.begin(),vcc.end(),make_pair(l,0)) - vcc.begin();
  82. r = lower_bound(vcc.begin(),vcc.end(),make_pair(r,0)) - vcc.begin();
  83. if(Q[i].type==1) ST.modify(l,r+1,Q[i].x);
  84. else printf("%d\n",ST.query(l,r+1));
  85. }
  86. }
  87. int main(){
  88. #ifndef ONLINE_JUDGE
  89. freopen("Local.in","r",stdin);
  90. freopen("ans.out","w",stdout);
  91. #endif
  92. solve();
  93. return 0;
  94. }

Educational Codeforces Round 20的更多相关文章

  1. Educational Codeforces Round 20 D. Magazine Ad

    The main city magazine offers its readers an opportunity to publish their ads. The format of the ad ...

  2. Educational Codeforces Round 20 C(math)

    題目鏈接: http://codeforces.com/problemset/problem/803/C 題意: 給出兩個數n, k, 將n拆分成k個數的和,要求這k個數是嚴格遞增的,並且這k個數的g ...

  3. Educational Codeforces Round 20.C

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. Educational Codeforces Round 20 C 数学/贪心/构造

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  5. Educational Codeforces Round 20 C. Maximal GCD

    C. Maximal GCD time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. Educational Codeforces Round 20 B. Distances to Zero

    B. Distances to Zero time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Educational Codeforces Round 20 A. Maximal Binary Matrix

    A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. Educational Codeforces Round 20 E - Roma and Poker(dp)

    传送门 题意 Roma在玩一个游戏,一共玩了n局,赢则bourle+1,输则bourle-1,Roma将会在以下情况中退出 1.他赢了k个bourle 2.他输了k个bourle 现在给出一个字符串 ...

  9. Educational Codeforces Round 20 B

    Description You are given the array of integer numbers a0, a1, ..., an - 1. For each element find th ...

随机推荐

  1. 从零开始的C程序设计大作业——学生成绩管理系统

    前言 学生成绩管理系统可以说是C语言程序设计的结课的必备大作业了.花了些时间,费了些头发肝了下,完成了两个系统,一个是控制台版本的,另一个用easyx图形库进行了优化. 先放出完成后的演示图片占个坑. ...

  2. Head First 设计模式 —— 10. 迭代器 (Iterator) 模式

    思考题 public void printMenu() { PancakeHouseMenu pancakeHouseMenu = new PancakeHouseMenu(); ArrayList ...

  3. [日常填坑系列]CAP食用指南-版本引用问题

    一.前言 最近,由于好久没升级底层框架,一直用着netcore2.2版本,导致有些包没能更新到最新的版本,例如:CAP. 然而,在最近升级到CAP:3.1.2版本的时候,发现有点小坑,只能退回到CAP ...

  4. .netcore 急速接入第三方登录,不看后悔

    新年新气象,趁着新年的喜庆,肝了十来天,终于发了第一版,希望大家喜欢. 如果有不喜欢看文字的童鞋,可以直接看下面的地址体验一下: https://oauthlogin.net/ 前言 此次带来得这个小 ...

  5. 基于Python的接口自动化-读写excel文件

    引言 使用python进行接口测试时常常需要接口用例测试数据.断言接口功能.验证接口响应状态等,如果大量的接口测试用例脚本都将接口测试用例数据写在脚本文件中,这样写出来整个接口测试用例脚本代码将看起来 ...

  6. XSS-labs通关挑战(xss challenge)

    XSS-labs通关挑战(xss challenge) 0x00 xss-labs   最近在看xss,今天也就来做一下xss-labs通过挑战.找了好久的源码,终于被我给找到了,因为在GitHub上 ...

  7. 【Linux】linux rinetd 端口转发部署

    linux下简单好用的工具rinetd,实现端口映射/转发/重定向 Rinetd是为在一个Unix和Linux操作系统中为重定向传输控制协议(TCP)连接的一个工具.Rinetd是单一过程的服务器,它 ...

  8. CTFhub刷题记录

    一 [WesternCTF2018]shrine 没什么好说的,SSTI模版注入类问题,过滤了()但是我们不慌.开始注入,{{29*3}}测试通过. 发现是jinjia2的模版注入.关键点在于没有() ...

  9. 鸿蒙的fetch请求加载聚合数据的前期准备工作-手动配置网络权限

    目录: 1.双击打开"config.json"文件 2.找到配置网络访问权限位置1 3.配置内容1 4.默认访问内容是空的 5.添加配置内容2 6.复制需要配置的网络二级URL 7 ...

  10. 使用modify修改内表

    modify修改内表,有这样一种方式,MODIFY TABLE itab FROM wa [TRANSPORTING ..]. 然后这里的内表itab是有条件的,这个itab必须要有table key ...