Arthur and Questions CodeForces - 518E (贪心模拟)
大意: 给定序列$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|$最小即可.
#include <iostream>
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#include <sstream>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
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;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
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;}
//head #ifdef ONLINE_JUDGE
const int N = 1e6+10;
#else
const int N = 111;
#endif int n, k, a[N]; int main() {
scanf("%d%d", &n, &k);
REP(i,1,n) {
string s;
cin>>s;
if (s[0]=='?') a[i]=INF;
else {
stringstream ss(s);
ss>>a[i];
}
}
REP(i,1,k) {
for (int j=i; j<=n; j+=k) if (a[j]!=INF) {
if (a[j]<=0) {
int now = a[j];
for (int ii=j-k; ii>=i; ii-=k) {
if (a[ii]==INF) a[ii]=--now;
else break;
}
}
if (a[j]>=0) {
int now = a[j];
for (int ii=j+k; ii<=n; ii+=k) {
if (a[ii]==INF) a[ii]=++now;
else break;
}
}
}
int s = i;
while (a[s]!=INF&&s<=n) s+=k;
if (s>n) continue;
int t = s;
while (a[t]==INF&&t<=n) t+=k;
t -= k;
int d = (s-t)/k/2;
if (s-k>=i&&a[s-k]>=d) d=a[s-k]+1;
if (t+k<=n&&a[t+k]<=d+(t-s)/k) d=a[t+k]-1-(t-s)/k;
for (int j=s; j<=t; ++d,j+=k) a[j]=d;
}
REP(i,1,n) if (a[i]==INF) return puts("Incorrect sequence"),0;
REP(i,1,k) {
for (int j=i+k; j<=n; j+=k) if (a[j]<=a[j-k]) return puts("Incorrect sequence"),0;
}
REP(i,1,n) printf("%d ", a[i]);hr;
}
Arthur and Questions CodeForces - 518E (贪心模拟)的更多相关文章
- CodeForces - 730A 贪心+模拟
贪心策略: 1.只有一个最大值,选着第二大的一起参加比赛减分. 2.有奇数个最大值,选择三个进行比赛. 3.偶数个最大值,选择两个进行比赛. 为什么不把最大值全部选择? 因为最多只能选五个,有可能选择 ...
- Population Size CodeForces - 416D (贪心,模拟)
大意: 给定$n$元素序列$a$, 求将$a$划分为连续的等差数列, 且划分数尽量小. $a$中的$-1$表示可以替换为任意正整数, 等差数列中必须也都是正整数. 贪心策略就是从前到后尽量添进一个等差 ...
- Codeforces 1042C (贪心+模拟)
题面 传送门 分析 思路简单,但代码较复杂的贪心 分类讨论: 有0 负数有奇数个:将绝对值最小(实际最大)的负数和0全部乘到一起,最后删掉0 负数有偶数个:将0全部乘到一起,最后删掉0 没有0 负数有 ...
- Sums of Digits CodeForces - 509C (贪心,模拟)
大意: 一个未知严格递增数组$a$, 给定每个数的数位和, 求$a[n]$最小的数组$a$ #include <iostream> #include <algorithm> # ...
- Arthur and Brackets CodeForces - 508E (贪心,括号匹配)
大意: n个括号, 位置未知, 给出每对括号左右间距, 求输出一个合法括号序列. 最后一个括号间距一定为1, 从右往左遍历, 每次括号划分越小越好. #include <iostream> ...
- Music in Car CodeForces - 746F (贪心,模拟)
大意: n首歌, 第$i$首歌时间$t_i$, 播放完获得贡献$a_i$, 最多播放k分钟, 可以任选一首歌开始按顺序播放, 最多选w首歌半曲播放(花费时间上取整), 求贡献最大值. 挺简单的一个题, ...
- 贪心+模拟 Codeforces Round #288 (Div. 2) C. Anya and Ghosts
题目传送门 /* 贪心 + 模拟:首先,如果蜡烛的燃烧时间小于最少需要点燃的蜡烛数一定是-1(蜡烛是1秒点一支), num[g[i]]记录每个鬼访问时已点燃的蜡烛数,若不够,tmp为还需要的蜡烛数, ...
- CodeForces ---596B--Wilbur and Array(贪心模拟)
Wilbur and Array Time Limit: 2000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Su ...
- Arthur and Table CodeForces - 557C
Arthur and Table CodeForces - 557C 首先,按长度排序. 长度为p的桌腿有a[p]个. 要使得长度为p的桌腿为最长,那么要按照代价从小到大砍掉sum{长度不到p的腿的数 ...
随机推荐
- 佳佳的Fibonacci
#include<cstdio> #include<cstring> #include<iostream> #include<cmath> #inclu ...
- linux创建桌面快捷方式
这里拿postman举例,其他的程序类似 在/usr/sharp/applications新建postman.desktop文件(终端下输入vim /usr/sharm/applications/po ...
- 解决ssh连接问题1
某天服务器A与B,互相ping没问题,telnet 22端口没问题 ssh -v a@10.80.97.241 OpenSSH_6.6.1, OpenSSL 1.0.1k-fips 8 Jan 201 ...
- 《CSS世界》读书笔记(十三)
<!-- <CSS世界>张鑫旭著 --> margin 无效情形解析 (1)display 计算值 inline 的非替换元素的垂直 margin 是无效的.对于内联替换元素, ...
- 2.1JAVA基础复习——JAVA语言的基础组成注释和常量变量
JAVA语言的基础组成有: 1.关键字:被赋予特殊含义的单词. 2.标识符:用来标识的符号. 3.注释:用来注释说明程序的文字. 4.常量和变量:内存存储区域的表示. 5.运算符:程序中用来运算的符号 ...
- 20180706001 - 动态添加 tabPage
// public TabPage Add(Form form, string str_frm_name) { form.Visible = true ...
- json键和值转数组
var jb={"美的":49,"三星":35,"海信":25,"格力":16,"方太":14}; ...
- 利用phpspider爬取网站数据
本文实例原址:PHPspider爬虫10分钟快速教程 在我们的工作中可能会涉及到要到其它网站去进行数据爬取的情况,我们这里使用phpspider这个插件来进行功能实现. 1.首先,我们需要php环境, ...
- hadoop过程中遇到的错误与解决方法
本文整理了在hadoop学习过程中遇到的各种问题. windows下开发环境搭建 大部分情况下,我们都是在windows下开发,hadoop则一般部署于linux服务器(无论是CDH还是原生hadoo ...
- ajax的三次封装简单概况
原生ajax: readyState 准备状态 status 页面状态 ...