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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- 6.cookie、session,localStorage、sessionStorage
必须在服务器下运行 cookie/session 存东西 cookie 时间 过期时间 4k 服务器把一部分数据保存在客户端(浏览器) session 回话 时间 服务器存取用户信息 5M local ...
- 计算属性和监听,computed,watch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- windows下eclipse实现操作虚拟机ubantu中的hdfs hbase
1.首先打开虚拟机,查看虚拟机的ip地址 2.修改C:\Windows\System32\drivers\etc下的主机名与ip的映射文件 3.配置Map/reduce 配置成功后可以查看hdfs文件 ...
- 剑指offer——python【第31题】整数1出现的次数
题目描述 求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了. ...
- EasyUI Tree节点拖动到指定容器
效果图:将tree节点拖动到指定的DIV中,结果显示节点的id和text 代码: <!DOCTYPE html> <html> <head> <meta ch ...
- call()的个人理解
先看两道道面试题 面试题1: var number = 50; var obj = { number: 60, getNum: function() { var number = 70; return ...
- 电力电子MATLAB
1.电力电子仿真时,要加一个powergui 2.变压器Multi-Winding Transformer 其中额定电压比就是匝数比,并且变压器上的电压不能超过额定电压 上图这一项表示变压器的容量和频 ...
- 11.0-uC/OS-III就绪列表(优先级)
准备运行的任务被放置于就绪列表中.就绪列表包括2个部分:位映像组包含了优先级信息,一个表包含了所有指向就绪任务的指针. 1.优先级 图6-1到6-3显示了优先级的位映像组.它的宽度取决于CPU_DAT ...
- 5、Flutter 实现 ViewPager、bottomNavigationBar 界面切换
1.前言 首先我们想一下,如果在 Android 中实现 布局切换,通常的思路是: 做一个 viewpager 一组 Fragment 每个 Fragment 绑定一个 xml 最后填充至 viewp ...
- bugfree3.0.1-导入excel测试用例
大多数项目里只用BugFree做缺陷管理工具,其实还可以通过该工具导入测试用例,记录测试结果,最后获得统计结果. 难点 1.导入文件要求XML格式: 2.一般我们的测试用例都是用excle文件存取,很 ...