大意: 给定序列$a$, 某些位置为'?', 求给'?'赋值使得序列$(a_1+a_2+...+a_k,a_2+a_3+...+a_{k+1},...)$严格递增, 且$\sum |a_i| $最小.

化简一下可以得到$a_1<a_{k+1}<a_{2k+1}<...,a_2<a_{k+2}<a_{2k+2}<...$, 所以每一部分都是独立的, 所以单独考虑k个部分, 贪心使得$\sum|a_i|$最小即可.

  1. #include <iostream>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstdio>
  5. #include <math.h>
  6. #include <set>
  7. #include <map>
  8. #include <queue>
  9. #include <string>
  10. #include <string.h>
  11. #include <bitset>
  12. #include <sstream>
  13. #define REP(i,a,n) for(int i=a;i<=n;++i)
  14. #define PER(i,a,n) for(int i=n;i>=a;--i)
  15. #define hr putchar(10)
  16. #define pb push_back
  17. #define lc (o<<1)
  18. #define rc (lc|1)
  19. #define mid ((l+r)>>1)
  20. #define ls lc,l,mid
  21. #define rs rc,mid+1,r
  22. #define x first
  23. #define y second
  24. #define io std::ios::sync_with_stdio(false)
  25. #define endl '\n'
  26. #define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
  27. using namespace std;
  28. typedef long long ll;
  29. typedef pair<int,int> pii;
  30. const int P = 1e9+7, INF = 0x3f3f3f3f;
  31. ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
  32. ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
  33. ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
  34. inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
  35. //head
  36.  
  37. #ifdef ONLINE_JUDGE
  38. const int N = 1e6+10;
  39. #else
  40. const int N = 111;
  41. #endif
  42.  
  43. int n, k, a[N];
  44.  
  45. int main() {
  46. scanf("%d%d", &n, &k);
  47. REP(i,1,n) {
  48. string s;
  49. cin>>s;
  50. if (s[0]=='?') a[i]=INF;
  51. else {
  52. stringstream ss(s);
  53. ss>>a[i];
  54. }
  55. }
  56. REP(i,1,k) {
  57. for (int j=i; j<=n; j+=k) if (a[j]!=INF) {
  58. if (a[j]<=0) {
  59. int now = a[j];
  60. for (int ii=j-k; ii>=i; ii-=k) {
  61. if (a[ii]==INF) a[ii]=--now;
  62. else break;
  63. }
  64. }
  65. if (a[j]>=0) {
  66. int now = a[j];
  67. for (int ii=j+k; ii<=n; ii+=k) {
  68. if (a[ii]==INF) a[ii]=++now;
  69. else break;
  70. }
  71. }
  72. }
  73. int s = i;
  74. while (a[s]!=INF&&s<=n) s+=k;
  75. if (s>n) continue;
  76. int t = s;
  77. while (a[t]==INF&&t<=n) t+=k;
  78. t -= k;
  79. int d = (s-t)/k/2;
  80. if (s-k>=i&&a[s-k]>=d) d=a[s-k]+1;
  81. if (t+k<=n&&a[t+k]<=d+(t-s)/k) d=a[t+k]-1-(t-s)/k;
  82. for (int j=s; j<=t; ++d,j+=k) a[j]=d;
  83. }
  84. REP(i,1,n) if (a[i]==INF) return puts("Incorrect sequence"),0;
  85. REP(i,1,k) {
  86. for (int j=i+k; j<=n; j+=k) if (a[j]<=a[j-k]) return puts("Incorrect sequence"),0;
  87. }
  88. REP(i,1,n) printf("%d ", a[i]);hr;
  89. }

Arthur and Questions CodeForces - 518E (贪心模拟)的更多相关文章

  1. CodeForces - 730A 贪心+模拟

    贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...

  2. Population Size CodeForces - 416D (贪心,模拟)

    大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...

  3. Codeforces 1042C (贪心+模拟)

    题面 传送门 分析 思路简单,但代码较复杂的贪心 分类讨论: 有0 负数有奇数个:将绝对值最小(实际最大)的负数和0全部乘到一起,最后删掉0 负数有偶数个:将0全部乘到一起,最后删掉0 没有0 负数有 ...

  4. Sums of Digits CodeForces - 509C (贪心,模拟)

    大意: 一个未知严格递增数组$a$, 给定每个数的数位和, 求$a[n]$最小的数组$a$ #include <iostream> #include <algorithm> # ...

  5. Arthur and Brackets CodeForces - 508E (贪心,括号匹配)

    大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...

  6. Music in Car CodeForces - 746F (贪心,模拟)

    大意: n首歌, 第$i$首歌时间$t_i$, 播放完获得贡献$a_i$, 最多播放k分钟, 可以任选一首歌开始按顺序播放, 最多选w首歌半曲播放(花费时间上取整), 求贡献最大值. 挺简单的一个题, ...

  7. 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts

    题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...

  8. CodeForces ---596B--Wilbur and Array(贪心模拟)

    Wilbur and Array Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Su ...

  9. Arthur and Table CodeForces - 557C

    Arthur and Table CodeForces - 557C 首先,按长度排序. 长度为p的桌腿有a[p]个. 要使得长度为p的桌腿为最长,那么要按照代价从小到大砍掉sum{长度不到p的腿的数 ...

随机推荐

  1. Linq To Object 函数介绍

    static void Main(string[] args) { #region Aggregate 把集合中的元素按照表达式依次执行 { IEnumerable<int> list = ...

  2. 关于git的认证方式

    之前对github的使用,形成了两种观点.就是有两种url的模式,一种是http或https的,另一种是git专属的.然后git专属的url方式可以配置公钥认证,http(s)的则需要输入密码. 近期 ...

  3. Oracle exp和expdp对数据进行备份

    以下给出两个示例,详细内容需要查阅手册: exp system OWNER=ZLTX FILE=ZLTX20190123.DMP expdp system DUMPFILE=ZLTX20190123. ...

  4. MVC 使用缓存

    public AController() { ViewBag[); } private List<BlogsClass> GetClass(int parentId) { List< ...

  5. ajax请求网络api

    不啰嗦,直接上代码: 1.在浏览器输入网址:http://api.asilu.com/weather/?callback=getname&city=深圳 你会看到如下结果:他返回的是json数 ...

  6. javascript数组方法

    [声明一个数组]var a=[1,2,3]; [定义数组的长度]var a=new Array(2); [连接数组]数组1.concat(数组2,数组3) [将数组转换为字符串,可自定连接符]arr. ...

  7. spring boot常见问题

    1.什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的spring引用程序 main方法运行 嵌入的T ...

  8. IP通信第四周作业

    一.选择交换机的主要技能指标是什么? a.背板带宽.二/三层交换吞吐率. b.VLAN类型和数量. c.交换机端口数量及类型. d.支持网络管理的协议和方法.需要交换机提供更加方便和集中式的管理. e ...

  9. 【js】高阶函数是个什么?

    所谓高阶函数,就是函数中可以传入另一个函数作为参数的函数. 简单一张图,方便理解全文. function 高阶函数(函数){} 这是一个高阶函数,f是传入的函数作为参数. 其实高阶函数用的很多.其实平 ...

  10. 如何对tcp流认证并加密

    一个场景:目前越来越多的业务需要远程读写Redis,而Redis 本身不提供 SSL/TLS 的支持,在需要安全访问的环境下. 这时候就需要额外的手段进行加密认证,这里有两种手段:spiped 和 n ...