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的腿的数 ...
随机推荐
- Linq To Object 函数介绍
static void Main(string[] args) { #region Aggregate 把集合中的元素按照表达式依次执行 { IEnumerable<int> list = ...
- 关于git的认证方式
之前对github的使用,形成了两种观点.就是有两种url的模式,一种是http或https的,另一种是git专属的.然后git专属的url方式可以配置公钥认证,http(s)的则需要输入密码. 近期 ...
- Oracle exp和expdp对数据进行备份
以下给出两个示例,详细内容需要查阅手册: exp system OWNER=ZLTX FILE=ZLTX20190123.DMP expdp system DUMPFILE=ZLTX20190123. ...
- MVC 使用缓存
public AController() { ViewBag[); } private List<BlogsClass> GetClass(int parentId) { List< ...
- ajax请求网络api
不啰嗦,直接上代码: 1.在浏览器输入网址:http://api.asilu.com/weather/?callback=getname&city=深圳 你会看到如下结果:他返回的是json数 ...
- javascript数组方法
[声明一个数组]var a=[1,2,3]; [定义数组的长度]var a=new Array(2); [连接数组]数组1.concat(数组2,数组3) [将数组转换为字符串,可自定连接符]arr. ...
- spring boot常见问题
1.什么是springboot 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件) 创建独立的spring引用程序 main方法运行 嵌入的T ...
- IP通信第四周作业
一.选择交换机的主要技能指标是什么? a.背板带宽.二/三层交换吞吐率. b.VLAN类型和数量. c.交换机端口数量及类型. d.支持网络管理的协议和方法.需要交换机提供更加方便和集中式的管理. e ...
- 【js】高阶函数是个什么?
所谓高阶函数,就是函数中可以传入另一个函数作为参数的函数. 简单一张图,方便理解全文. function 高阶函数(函数){} 这是一个高阶函数,f是传入的函数作为参数. 其实高阶函数用的很多.其实平 ...
- 如何对tcp流认证并加密
一个场景:目前越来越多的业务需要远程读写Redis,而Redis 本身不提供 SSL/TLS 的支持,在需要安全访问的环境下. 这时候就需要额外的手段进行加密认证,这里有两种手段:spiped 和 n ...