CodeForces 518E Arthur and Questions(贪心 + 思维)题解
题意:给你a1~an,k,要求a1 + ... + ak < a2 + .... + ak+1 < a3 + ... + ak+2 <...,然后这里的ai有可能是?,要求你填?的数字,并且使a1~an的绝对值之和最小,不可能输出Incorrect sequence
思路:由上式要求我们可以得到a1 < ak+1 < ak+k+1 < ....且a2 < ak+2 < ak+k+2 < ....且...,所以可以转化为这样的要求。但是要绝对值最小怎么办,我们每次找连续的一连串?,尽可能让中间的位置为0,这样绝对值最小。所以我们先按中间赋值0这样去操作,然后再根据左右边界对整个区间进行修正,全加或全减一个数使得符合要求。
代码:
#include<cmath>
#include<set>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5 + ;
const ull seed = ;
const int INF = 0x3f3f3f3f;
const int MOD = ;
int n, k, none[maxn];
ll a[maxn];
int id(int j, int k, int i){return j * k + i;}
void solve(int ii, int l, int r){
int m = (l + r) / ;
int num = ;
for(int i = m; i <= r; i++)
a[id(i, k, ii)] = num++;
num = ;
for(int i = m; i >= l; i--)
a[id(i, k, ii)] = num--;
int dis;
if(l != ){
dis = a[id(l - , k, ii)] - a[id(l, k, ii)];
if(dis >= ){
dis++;
for(int i = l; i <= r; i++)
a[id(i, k, ii)] += dis;
}
}
if(id(r + , k, ii) <= n){
dis = a[id(r, k, ii)] - a[id(r + , k, ii)];
if(dis >= ){
dis++;
for(int i = l; i <= r; i++)
a[id(i, k, ii)] -= dis;
}
}
}
int main(){
char o[];
scanf("%d%d", &n, &k);
for(int i = ; i <= n; i++){
scanf("%s", o);
if(o[] == '?'){
none[i] = ;
}
else{
sscanf(o, "%lld", &a[i]);
}
}
for(int i = ; i <= k; i++){
int l = , r = , ok = ;
for(int j = ; j * k + i <= n; j++){
if(none[id(j, k, i)] && (j == || !none[id(j - , k, i)])){
l = j;
ok = ;
}
if(none[id(j, k, i)]){
r = j;
}
else{
if(ok){
solve(i, l, r);
ok = ;
}
}
}
if(ok) solve(i, l, r);
} ll tot = , pre;
for(int i = ; i <= k; i++){
tot += a[i];
}
pre = tot;
for(int i = k + ; i <= n; i++){
tot = tot - a[i - k] + a[i];
if(tot <= pre){
printf("Incorrect sequence\n");
return ;
}
pre = tot;
} for(int i = ; i <= n; i++){
if(i != ) printf(" ");
printf("%d", a[i]);
}
printf("\n");
return ;
}
CodeForces 518E Arthur and Questions(贪心 + 思维)题解的更多相关文章
- CodeForces 508E Arthur and Brackets 贪心
题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...
- codeforces 233 C. Cycles(贪心+思维)
题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪 ...
- Codeforces 449C Jzzhu and Apples 贪心 (看题解)
Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas
题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...
- Mike and distribution CodeForces - 798D (贪心+思维)
题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...
- Educational Codeforces Round 63 (Rated for Div. 2) 题解
Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...
- Educational Codeforces Round 65 (Rated for Div. 2)题解
Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...
- Educational Codeforces Round 64 (Rated for Div. 2)题解
Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- web项目访问地址前添加小图片
修改HTML 1.head标签添加 <link rel="icon" type="image/x-icon" href="images/icon ...
- js判断PC端还是移动端
function goPAGE() { if ((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobi ...
- c++试题2
一.写出下列程序的运行结果(40 分) 1.for(i=1;i<5;i++); cout << “OK” << endl; 程序执行后的输出结果是: OK ___ ...
- lucene学习教程
1Lucene的介绍 ①Lucene是什么: 是一个开放源代码的全文检索引擎工具包,但它不是一个完整的全文检索引擎,而是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎,部分文本分析引擎 ②Lu ...
- English Time And Date
What's the Time in English? Explanation There are two common ways of telling the time. Formal but ea ...
- Docker入门3------手动编辑自定义镜像
手动编辑自定义镜像 查看本地现有镜像: 基于centos创建一个,会自动下载centos最新原始镜像 docker run -it --name=web centos /bin/bash 然后在容器内 ...
- vue-cli脚手架
cnpm i vue-cli -g //npm 安装报错,原因不明,可能是我改过东西的原因,但是cnpm可以安装 命令行进入要新建的vue的目录执行 C:\Users\76912\Videos\v ...
- final、finally、finalize区别
final final关键字可以用来修饰类,方法以及成员变量,当用在不同的场景下时具有不同的意义. 修饰类 如果修饰类,则代表这个类不可继承 修饰方法 如果修饰方法,则代表这个方法不可覆写:同时,允许 ...
- 修改jupyter notebook的默认打开地址
1)在Anaconda Prompt终端中输入下面命令,查看你的notebook配置文件在哪里: jupyter notebook --generate-config 2)通过搜索关键词:c.Note ...
- android 前台服务不显示通知
原因可以在哪里写了执行完成后就自动结束的吧 导致前台服务没有出现 如我 @Override public int onStartCommand(Intent intent, int flags, in ...