Educational Codeforces Round 39 

D. Timetable

令\(dp[i][j]\)表示前\(i\)天逃课了\(j\)节课的情况下,在学校的最少时间

转移就是枚举第\(i\)天逃了\(x\)节课,然后取当天逃\(x\)节课情况下在学校的最小值即可

view code
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("Ofast,no-stack-protector")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define INF 0x3f3f3f3f
  6. #define endl "\n"
  7. #define LL long long int
  8. #define vi vector<int>
  9. #define vl vector<LL>
  10. #define all(V) V.begin(),V.end()
  11. #define sci(x) scanf("%d",&x)
  12. #define scl(x) scanf("%lld",&x)
  13. #define scs(s) scanf("%s",s)
  14. #define pii pair<int,int>
  15. #define pll pair<LL,LL>
  16. #ifndef ONLINE_JUDGE
  17. #define cout cerr
  18. #endif
  19. #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
  20. #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
  21. #define debug(x) cerr << #x << " = " << x << endl
  22. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  23. template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
  24. template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
  25. const int MAXN = 2e5+7;
  26. int n, m, k;
  27. char s[MAXN];
  28. void solve(){
  29. sci(n); sci(m); sci(k);
  30. vi f(k+1,n*m);
  31. f[k] = 0;
  32. for(int d = 1; d <= n; d++){
  33. vi next_f(k+1,n*m);
  34. scs(s+1);
  35. vi A; for(int i = 1; i <= m; i++) if(s[i]=='1') A << i;
  36. vi cost(A.size()+1,n*m);
  37. for(int i = 0; i <= A.size(); i++){
  38. if(i==A.size()) cost[i] = 0;
  39. else for(int j = 0; j <= i; j++) cmin(cost[i],A[A.size() - 1 - i + j] - A[j] + 1);
  40. }
  41. for(int pre = 0; pre <= k; pre++) for(int cur = 0; cur <= min(pre,(int)A.size()); cur++) cmin(next_f[pre-cur],f[pre]+cost[cur]);
  42. f.swap(next_f);
  43. }
  44. cout << *min_element(all(f)) << endl;
  45. }
  46. int main(){
  47. #ifndef ONLINE_JUDGE
  48. freopen("Local.in","r",stdin);
  49. freopen("ans.out","w",stdout);
  50. #endif
  51. solve();
  52. return 0;
  53. }

E.  Largest Beautiful Number

考虑从后往前枚举每个位置,判断在这个位置之前全部相同,这个位置比原来小的情况下是否存在合法解即可

特判长度为奇数的情况和小于等于\(1xxxxxx1\)的情况

view code
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("Ofast,no-stack-protector")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define INF 0x3f3f3f3f
  6. #define endl "\n"
  7. #define LL long long int
  8. #define vi vector<int>
  9. #define vl vector<LL>
  10. #define all(V) V.begin(),V.end()
  11. #define sci(x) scanf("%d",&x)
  12. #define scl(x) scanf("%lld",&x)
  13. #define scs(s) scanf("%s",s)
  14. #define pii pair<int,int>
  15. #define pll pair<LL,LL>
  16. #ifndef ONLINE_JUDGE
  17. #define cout cerr
  18. #endif
  19. #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
  20. #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
  21. #define debug(x) cerr << #x << " = " << x << endl
  22. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  23. template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
  24. template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
  25. const int MAXN = 2e5+7;
  26. int n;
  27. string s;
  28. void solve(){
  29. cin >> s;
  30. function<bool(void)>check = [&](){
  31. string str(s.size(),'0');
  32. str.front() = str.back() = '1';
  33. return str >= s;
  34. };
  35. if(s.size()&1 or check()){
  36. for(int i = 1; i <= (s.size()&1 ? s.size() - 1 : s.size() - 2); i++) cout << 9;
  37. cout << endl; return;
  38. }
  39. vi cnt(10,0);
  40. for(int i = 0; i < s.size(); i++) cnt[s[i]-'0'] ^= 1;
  41. for(int i = s.size() - 1; ~i; i--){
  42. cnt[s[i]-'0'] ^= 1;
  43. for(char c = s[i] - 1; c >= '0'; c--){
  44. cnt[c-'0'] ^= 1;
  45. int tot = accumulate(all(cnt),0);
  46. if(tot>s.size()-i-1){
  47. cnt[c-'0'] ^= 1;
  48. continue;
  49. }
  50. string str(s);
  51. s[i] = c;
  52. int cur = i;
  53. for(int j = 0; j < s.size()-i-1-tot; j++) s[++cur] = '9';
  54. for(int j = 9; ~j; j--) if(cnt[j]) s[++cur] = j + '0';
  55. cout << s << endl;
  56. return;
  57. }
  58. }
  59. }
  60. int main(){
  61. #ifndef ONLINE_JUDGE
  62. freopen("Local.in","r",stdin);
  63. freopen("ans.out","w",stdout);
  64. #endif
  65. int tt; for(cin >> tt; tt--; solve());
  66. return 0;
  67. }

F. Fibonacci String Subsequences

\(dp[i][l][r]\)表示\(f(i)\)中子串\(s[l:r]\)以子序列的方式出现的次数,考虑转移

对于一般情况,可以考虑把\(s[l:r]\)分成\(s[l:k]\)和\(s[k+1:r]\),然后分别在\(f(i-1)\)和\(f(i-2)\)中找

其次考虑\(s[l:r]\)全在\(f(i-1)\)或\(f(i-2)\)中,如果\(r=n-1\),那么在\(f(i-1)中找到\)\(s[l:r]\)出现次数之后由于后面可以随便选,乘上\(2^{fib(i-2)}\)即可,否则只算左边一部分,对于\(l=0\)的情况类似处理即可

view code
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("Ofast,no-stack-protector")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define INF 0x3f3f3f3f
  6. #define endl "\n"
  7. #define LL long long int
  8. #define vi vector<int>
  9. #define vl vector<LL>
  10. #define all(V) V.begin(),V.end()
  11. #define sci(x) scanf("%d",&x)
  12. #define scl(x) scanf("%lld",&x)
  13. #define scs(s) scanf("%s",s)
  14. #define pii pair<int,int>
  15. #define pll pair<LL,LL>
  16. #ifndef ONLINE_JUDGE
  17. #define cout cerr
  18. #endif
  19. #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
  20. #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
  21. #define debug(x) cerr << #x << " = " << x << endl
  22. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  23. template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
  24. template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
  25. const int MAXN = 2e2+7;
  26. const int MOD = 1e9+7;
  27. LL ksm(LL a, LL b){
  28. LL ret = 1;
  29. while(b){
  30. if(b&1) ret = ret * a % MOD;
  31. b >>= 1;
  32. a = a * a % MOD;
  33. }
  34. return ret;
  35. }
  36. int n, x, fib[MAXN], f[MAXN][MAXN][MAXN];
  37. string s;
  38. int dp(int x, int l, int r){
  39. int &ret = f[x][l][r];
  40. if(~ret) return ret;
  41. if(x==0) return (ret = ((l==r) and s[l]=='0'));
  42. if(x==1) return (ret = ((l==r) and s[l]=='1'));
  43. ret = 0;
  44. if(r==n-1) ret = (ret + 1ll * dp(x-1,l,r) * ksm(2,fib[x-2])) % MOD;
  45. else ret = (ret + 1ll * dp(x-1,l,r)) % MOD;
  46. if(l==0) ret = (ret + 1ll * dp(x-2,l,r) * ksm(2,fib[x-1])) % MOD;
  47. else ret = (ret + 1ll * dp(x-2,l,r)) % MOD;
  48. for(int i = l; i < r; i++) ret = (ret + 1ll * dp(x-1,l,i) * dp(x-2,i+1,r)) % MOD;
  49. return ret;
  50. }
  51. void solve(){
  52. cin >> n >> x >> s;
  53. fib[0] = fib[1] = 1;
  54. for(int i = 2; i < MAXN; i++) fib[i] = (fib[i-1] + fib[i-2]) % (MOD - 1);
  55. memset(f,255,sizeof(f));
  56. cout << dp(x,0,n-1) << endl;
  57. }
  58. int main(){
  59. #ifndef ONLINE_JUDGE
  60. freopen("Local.in","r",stdin);
  61. freopen("ans.out","w",stdout);
  62. #endif
  63. solve();
  64. return 0;
  65. }

G. Almost Increasing Array

假设没有删一个数的条件的话,就是把原序列\(A\)中的所有\(A[i]\)变成\(A[i]-i\),然后算最长非降子序列

现在考虑枚举删掉的数为\(A[k]\),那么对于\(k\)之前的数\(A[i]\)来说,需要减去\(i\),对于\(k\)之后的数\(A[j]\)来说,需要减去\(j-1\)

那么之需要知道以\(k-1\)为结尾的最长非降子序列的长度和大于\(k\)的部分中起始值大于等于\(A[k-1]\)的最长非降子序列即可

那么可以用线段树来维护

view code
  1. #pragma GCC optimize("O3")
  2. #pragma GCC optimize("Ofast,no-stack-protector")
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define INF 0x3f3f3f3f
  6. #define endl "\n"
  7. #define LL long long int
  8. #define vi vector<int>
  9. #define vl vector<LL>
  10. #define all(V) V.begin(),V.end()
  11. #define sci(x) scanf("%d",&x)
  12. #define scl(x) scanf("%lld",&x)
  13. #define scs(s) scanf("%s",s)
  14. #define pii pair<int,int>
  15. #define pll pair<LL,LL>
  16. #ifndef ONLINE_JUDGE
  17. #define cout cerr
  18. #endif
  19. #define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
  20. #define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
  21. #define debug(x) cerr << #x << " = " << x << endl
  22. function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
  23. template <typename T> vector<T>& operator << (vector<T> &__container, T x){ __container.push_back(x); return __container; }
  24. template <typename T> ostream& operator << (ostream &out, vector<T> &__container){ for(T _ : __container) out << _ << ' '; return out; }
  25. const int MAXN = 4e5+7;
  26. int n, A[MAXN];
  27. struct SegmentTree{
  28. int l[MAXN<<2], r[MAXN<<2], maxx[MAXN<<2];
  29. #define ls(rt) rt << 1
  30. #define rs(rt) rt << 1 | 1
  31. #define pushup(rt) maxx[rt] = max(maxx[ls(rt)],maxx[rs(rt)])
  32. void build(int L, int R, int rt = 1){
  33. l[rt] = L; r[rt] = R;
  34. maxx[rt] = 0;
  35. if(L+1==R) return;
  36. int mid = (L + R) >> 1;
  37. build(L,mid,ls(rt)); build(mid,R,rs(rt));
  38. }
  39. void modify(int pos, int x, int rt = 1){
  40. if(l[rt] + 1 == r[rt]){
  41. maxx[rt] = x;
  42. return;
  43. }
  44. int mid = (l[rt] + r[rt]) >> 1;
  45. if(pos<mid) modify(pos,x,ls(rt));
  46. else modify(pos,x,rs(rt));
  47. pushup(rt);
  48. }
  49. int qmax(int L, int R, int rt = 1){
  50. if(L>=r[rt] or l[rt]>=R) return 0;
  51. if(L<=l[rt] and r[rt]<=R) return maxx[rt];
  52. return max(qmax(L,R,ls(rt)),qmax(L,R,rs(rt)));
  53. }
  54. }ST;
  55. int f[MAXN];
  56. void solve(){
  57. sci(n);
  58. vi vec;
  59. for(int i = 1; i <= n; i++) sci(A[i]), A[i] -= i, vec << A[i] << A[i] + 1;
  60. sort(all(vec)); vec.erase(unique(all(vec)),vec.end());
  61. auto ID = [&](int x){ return lower_bound(all(vec),x) - vec.begin() + 1; };
  62. ST.build(0,vec.size()+1);
  63. for(int i = 1; i <= n; i++){
  64. int x = ID(A[i]);
  65. f[i] = ST.qmax(0,x+1) + 1;
  66. ST.modify(x,f[i]);
  67. }
  68. int ret = f[n-1];
  69. ST.build(0,vec.size()+1);
  70. for(int i = n; i >= 2; i--){
  71. ret = max(ret,f[i-1]+ST.qmax(ID(A[i-1]),vec.size()+1));
  72. int x = ID(A[i]+1);
  73. ST.modify(x,ST.qmax(x,vec.size()+1)+1);
  74. }
  75. cmax(ret,ST.qmax(0,vec.size()+1));
  76. cout << n - 1 - ret << endl;
  77. }
  78. int main(){
  79. #ifndef ONLINE_JUDGE
  80. freopen("Local.in","r",stdin);
  81. freopen("ans.out","w",stdout);
  82. #endif
  83. solve();
  84. return 0;
  85. }

Educational Codeforces Round 39的更多相关文章

  1. Educational Codeforces Round 39 (Rated for Div. 2) G

    Educational Codeforces Round 39 (Rated for Div. 2) G 题意: 给一个序列\(a_i(1 <= a_i <= 10^{9}),2 < ...

  2. #分组背包 Educational Codeforces Round 39 (Rated for Div. 2) D. Timetable

    2018-03-11 http://codeforces.com/contest/946/problem/D D. Timetable time limit per test 2 seconds me ...

  3. Educational Codeforces Round 39 (Rated for Div. 2) 946E E. Largest Beautiful Number

    题: OvO http://codeforces.com/contest/946/problem/E CF 946E 解: 记读入串为 s ,答案串为 ans,记读入串长度为 len,下标从 1 开始 ...

  4. Educational Codeforces Round 39 (Rated for Div. 2) B. Weird Subtraction Process[数论/欧几里得算法]

    https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 取模也是一样的,就当多减几次. 在欧几里得最初的 ...

  5. codeforces Educational Codeforces Round 39 (Rated for Div. 2) D

    D. Timetable time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  6. Educational Codeforces Round 39 Editorial B(Euclid算法,连续-=与%=的效率)

    You have two variables a and b. Consider the following sequence of actions performed with these vari ...

  7. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  8. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  9. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

随机推荐

  1. JVM 源码分析(三):深入理解 CAS

    前言 什么是 CAS Java 中的 CAS JVM 中的 CAS 前言 在上一篇文章中,我们完成了源码的编译和调试环境的搭建. 鉴于 CAS 的实现原理比较简单, 然而很多人对它不够了解,所以本篇将 ...

  2. SpringBoot整合Shiro完成认证

    三.SpringBoot整合Shiro思路 首先从客户端发来的所有请求都经过Shiro过滤器,如果用户没有认证的都打回去进行认证,认证成功的,再判断是否具有访问某类资源(公有资源,私有资源)的权限,如 ...

  3. ps的参数解释

    [root@bogon ~]# ps axuUSER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND user启动进程的用户 pid  表示进程标志 ...

  4. kubernets之计算资源

    一  为pod分配cpu,内存以及其他的资源 1.1  创建一个pod,同时为这个pod分配内存以及cpu的资源请求量 apiVersion: v1 kind: Pod metadata: name: ...

  5. git创建分支并关联远程分支

    1.新建本地分支: 如图,再输入你的分支名字,然后选择从哪个远程分支拉代码,如选择master 至此本地分支创建完成. 2.关联远程分支: (1).先输入git branch -vv,看看分支与远程分 ...

  6. STM32驱动LCD原理

    TFTLCD即薄膜晶体管液晶显示器.它与无源TN-LCD.STN-LCD的简单矩阵不同,它在液晶显示屏的每一个像素上都设置有一个薄膜晶体管(TFT),可有效地克服非选通时的串扰,使显示液晶屏的静态特性 ...

  7. BI学习向导文章

    BI的学习笔记: BIWORK的博客:http://www.cnblogs.com/biwork/p/3328879.html 邀月工作室博客 :http://www.cnblogs.com/down ...

  8. Ubuntu16 安装 OpenSSH-Server

    Ubuntu16.04 桌面版默认是没有安装 SSH 服务的,需要手动安装服务: 更新源:sudo apt-get update 安装服务:sudo apt-get install -y openss ...

  9. Springboot中PropertySource注解的使用

    https://blog.csdn.net/qq_30739519/article/list/3 注解 https://blog.csdn.net/qq_30739519/article/detail ...

  10. physical CPU vs logical CPU vs Core vs Thread vs Socket(翻译)

    原文地址: http://www.daniloaz.com/en/differences-between-physical-cpu-vs-logical-cpu-vs-core-vs-thread-v ...