Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)

这场比赛没有打,后来补了一下,第五题数位dp好不容易才搞出来(我太菜啊)。

比赛传送门:http://codeforces.com/contest/1073

A. Diverse Substring

题意:给你个字符串,让你找一个子串满足任意一个字符的个数不超过其他字符的总和,输出yes或no表示否存在,如果存在输出任意一个。

这题只要找两个不同的相邻字符,因为两个字符各一个都不超过其他字符的总和,如果字符串只由一个字符组成或长度等于一才会不存在。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define rep(x, l, r) for(int x = (int)l; x <= (int)r; x++)
  6. #define repd(x, r, l) for(int x = (int)r; x >= (int)l; x--)
  7. #define clr(x,y) memset(x, y, sizeof(x))
  8. #define mp(x, y) make_pair(x, y)
  9. #define all(x) begin(x), end(x)
  10. #define MAXN 1005
  11. #define fi first
  12. #define se second;
  13. #define Size(x) ((int)size(x))
  14. using namespace std;
  15. typedef long long LL;
  16. typedef vector<int> vi;
  17. typedef pair<int, int> pii;
  18. const int INF = << ;
  19. const int p = ;
  20. //head by DYH
  21.  
  22. char st[MAXN];
  23.  
  24. int main(){
  25. int n;
  26. scanf("%d", &n);
  27. scanf("%s", st);
  28. int len = strlen(st);
  29. char ch = st[];
  30. rep(i, , len - ){
  31. if(st[i] != ch){
  32. puts("YES");
  33. printf("%c%c\n", ch, st[i]);
  34. return ;
  35. }
  36. }
  37. puts("NO");
  38. return ;
  39. }

Problem-A

B.Vasya and Books

题意:有n本书在一个栈中,依次为a1, a2, …, an,现在有n个操作,对于每个操作i,将从栈顶到书本bi的所有书全部放入包中,如果已经在包中,就不进行操作。问你每次操作需要放几本书。

这题可以记录下对于每本书i在栈中的位置posi,然后再记录上一次操作后放入包的书本数used,假如posb < used, 那么这次需要放入used - posbi 本书,否则就为0。

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define rep(x, l, r) for(int x = (int)l; x <= (int)r; x++)
  6. #define repd(x, r, l) for(int x = (int)r; x >= (int)l; x--)
  7. #define clr(x,y) memset(x, y, sizeof(x))
  8. #define mp(x, y) make_pair(x, y)
  9. #define all(x) begin(x), end(x)
  10. #define MAXN 200005
  11. #define fi first
  12. #define se second;
  13. #define Size(x) ((int)size(x))
  14. using namespace std;
  15. typedef long long LL;
  16. typedef vector<int> vi;
  17. typedef pair<int, int> pii;
  18. const int INF = << ;
  19. const int p = ;
  20. //head by DYH
  21.  
  22. int id[MAXN];
  23.  
  24. int main(){
  25. int n;
  26. scanf("%d", &n);
  27. rep(i, , n){
  28. int x;
  29. scanf("%d", &x);
  30. id[x] = i;
  31. }
  32. int used = ;
  33. rep(i, , n){
  34. int x;
  35. scanf("%d", &x);
  36. if(id[x] > used){
  37. printf("%d ", id[x] - used);
  38. used = id[x];
  39. }
  40. else printf("0 ");
  41. }
  42. puts("");
  43. return ;
  44. }

Problem-B

C.Vasya and Robot

题意:有一个机器人,有四种移动的操作。

  • U — move from (x,y)(x,y) to (x,y+1)(x,y+1);
  • D — move from (x,y)(x,y) to (x,y−1)(x,y−1);
  • L — move from (x,y)(x,y) to (x−1,y)(x−1,y);
  • R — move from (x,y)(x,y) to (x+1,y)(x+1,y).

现在有一个由操作指令组成的字符串,让你修改一些操作,使得机器人从(0, 0)走到(x, y),并且maxID - minID + 1最小,即修改最小的长度的子串。如果怎么更改都无法到达,输出-1。

这题用二分法和尺取法都可以做,也很好证明,就是在判断的时候有点麻烦。

对于每一个[l, r]的区间,最简单的方法就是现将这段区间全部删去,求出此时机器人到达的点(x2, y2)。如果进行r - l + 1次操作就能到达(x, y),即abs(x2 - x) + abs(y2 - y) <= r - l + 1,前提是同奇偶。

能否到达也无需另外判断,直接把答案的初值赋为-1即可。(然而我是一开始就判断的)

代码如下:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <algorithm>
  4. #include <cstring>
  5. #define rep(x, l, r) for(int x = (int)l; x <= (int)r; x++)
  6. #define repd(x, r, l) for(int x = (int)r; x >= (int)l; x--)
  7. #define clr(x,y) memset(x, y, sizeof(x))
  8. #define mp(x, y) make_pair(x, y)
  9. #define all(x) begin(x), end(x)
  10. #define MAXN 200005
  11. #define fi first
  12. #define se second
  13. #define Size(x) ((int)size(x))
  14. using namespace std;
  15. typedef long long LL;
  16. typedef vector<int> vi;
  17. typedef pair<int, int> pii;
  18. const int INF = << ;
  19. const int p = ;
  20. //head by DYH
  21.  
  22. pii t, sum[MAXN];
  23. int len;
  24. char st[MAXN];
  25.  
  26. bool judge(int l, int r){
  27. return abs(t.fi - (sum[len].fi - sum[r].fi + sum[l - ].fi)) + abs(t.se - (sum[len].se - sum[r].se + sum[l - ].se)) <= r - l + ;
  28. }
  29.  
  30. int main(){
  31. scanf("%d", &len);
  32. scanf("%s", st);
  33. rep(i, , len){
  34. if(st[i - ] == 'U') sum[i].fi = sum[i - ].fi, sum[i].se = sum[i - ].se + ;
  35. if(st[i - ] == 'D') sum[i].fi = sum[i - ].fi, sum[i].se = sum[i - ].se - ;
  36. if(st[i - ] == 'L') sum[i].fi = sum[i - ].fi - , sum[i].se = sum[i - ].se;
  37. if(st[i - ] == 'R') sum[i].fi = sum[i - ].fi + , sum[i].se = sum[i - ].se;
  38. }
  39. int x, y;
  40. scanf("%d%d", &x, &y);
  41. if(abs(x) + abs(y) > len || abs(abs(x) + abs(y) - len) & ){
  42. puts("-1");
  43. return ;
  44. }
  45. t = mp(x, y);
  46. int l = , r = , ans = INF;
  47. while(r <= len){
  48. while(judge(l, r)){
  49. ans = min(ans, r - l + );
  50. l++;
  51. }
  52. r++;
  53. }
  54. printf("%d\n", ans);
  55. return ;
  56. }

Problem-C

D:Berland Fair

题意:有n个摊位,在第i个摊位可以花费ai买到一个糖果。现在有一个人有m块钱,从摊位1出发,每到一个摊位如果当前的钱数大于ai,他就会买一个糖果,然后前往第i + 1个摊位(如果i == n,就到第1个摊位)。问你他会买多少颗糖果。(他会一直买直到在任何摊位都买不了糖果)

这题看上去很麻烦(也许是我太弱了,大佬勿喷),其实只要判断

noip结束,要搞文化课了……没空啊。

Educational Codeforces Round 53 (Rated for Div. 2) (前五题题解)的更多相关文章

  1. Codeforces Round #519 by Botan Investments(前五题题解)

    开个新号打打codeforces(以前那号玩废了),结果就遇到了这么难一套.touristD题用了map,被卡掉了(其实是对cf的评测机过分自信),G题没过, 700多行代码,码力惊人.关键是这次to ...

  2. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  3. Educational Codeforces Round 53 (Rated for Div. 2)

    http://codeforces.com/contest/1073 A. Diverse Substring #include <bits/stdc++.h> using namespa ...

  4. [codeforces][Educational Codeforces Round 53 (Rated for Div. 2)D. Berland Fair]

    http://codeforces.com/problemset/problem/1073/D 题目大意:有n个物品(n<2e5)围成一个圈,你有t(t<1e18)元,每次经过物品i,如果 ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum

    https://codeforces.com/contest/1073/problem/E 题意 求出l到r之间的符合要求的数之和,结果取模998244353 要求:组成数的数位所用的数字种类不超过k ...

  6. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot 【二分 + 尺取】

    任意门:http://codeforces.com/contest/1073/problem/C C. Vasya and Robot time limit per test 1 second mem ...

  7. Educational Codeforces Round 53 (Rated for Div. 2)G. Yet Another LCP Problem

    题意:给串s,每次询问k个数a,l个数b,问a和b作为后缀的lcp的综合 题解:和bzoj3879类似,反向sam日神仙...lcp就是fail树上的lca.把点抠出来建虚树,然后在上面dp即可.(感 ...

  8. Educational Codeforces Round 53 (Rated for Div. 2) D. Berland Fair

    题意:一个人  有T块钱 有一圈商店 分别出售 不同价格的东西  每次经过商店只能买一个  并且如果钱够就必须买 这个人一定是从1号店开始的!(比赛的时候读错了题,以为随意起点...)问可以买多少个 ...

  9. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot

    题意:给出一段操作序列 和目的地 问修改(只可以更改 不可以删除或添加)该序列使得最后到达终点时  所进行的修改代价最小是多少 其中代价的定义是  终点序号-起点序号-1 思路:因为代价是终点序号减去 ...

随机推荐

  1. 不插字段,直接利用OracleSpatial计算

    select to_char(regexp_replace(sdo_util.to_gmlgeometry(sdo_geom.sdo_difference( SDO_GEOMETRY ( 2003, ...

  2. 洛谷 1372 又是毕业季I

    题目背景 “叮铃铃铃”,随着高考最后一科结考铃声的敲响,三年青春时光顿时凝固于此刻.毕业的欣喜怎敌那离别的不舍,憧憬着未来仍毋忘逝去的歌.1000多个日夜的欢笑和泪水,全凝聚在毕业晚会上,相信,这一定 ...

  3. 3d爱心代码

    <!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. Android Studio(七):项目从Eclipse到Android Studio迁移

    Android Studio相关博客: Android Studio(一):介绍.安装.配置 Android Studio(二):快捷键设置.插件安装 Android Studio(三):设置Andr ...

  5. CSS的固定定位

    将元素放置在浏览器窗口的固定位置,拖拽窗口时元素位置不变. 类似语法: div{ position:fixed; top:0px; left:0px; right:0px; bottom:0px; }

  6. 【原生JS】动态分页样式效果

    效果图如下: html: <body> <div> <table id="btnbox"> <tbody> <tr>&l ...

  7. vue-router在新窗口打开页面

    1. <router-link>标签实现新窗口打开: <router-link target="_blank" :to="{path:'/app/dat ...

  8. pycharm解析器找不到

    pycharm每个项目都需要设定解释器位置吧,把自己解释器位置添加到项目设置就好了 先装python,再装pycharm,不然找不到解析器的,解析器其实就是python.exe.

  9. Scrapy项目注意事项

  10. P1108 分解质因数

    题目描述 给你一个正整数 \(n\) ,请给 \(n\) 分解质因数,并按照样例输出的格式输出对应的结果. 输入格式 输入包含一个正整数 \(n (2 \le n \le 10^9)\) . 输出格式 ...