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 水题,但是坑了很多人.需要注意以下就是正方 ...
随机推荐
- 创建dblink
创建dblinkcreate database link user1_Link_app--dblink名称----connect to ehl_app ----远程数据库名称----identifie ...
- [Educational Round 3][Codeforces 609E. Minimum spanning tree for each edge]
这题本来是想放在educational round 3的题解里的,但觉得很有意思就单独拿出来写了 题目链接:609E - Minimum spanning tree for each edge 题目大 ...
- windows 安装 Apache、php、mysql及其配置(转载)
此文包括的注意内容:软件版本及下载地址Apache2.4的配置和安装php7.0的配置mysql5.5的安装常见问题及解决方法1.软件版本Windows server 2008 r2+ 64位Apac ...
- CString数组和CStringArray
CStringArray是编译器定义的类型!可以进行一些(如:访问.增.删.改)等操作. 集中单个字符串的操作使用Cstring,集中一批字符串的管理使用CstringArray. 一个是动态,CSt ...
- 转 python测试框架最全资源汇总
转自: http://www.testclass.net/list/python_list_1/ xUnit frameworks(单元测试框架) frameworks 框架 unittest - p ...
- Dubbo 分布式服务框架入门
要想了解 Dubbo 是什么,我们不防先了解它有什么用.使用场景:比如我想开发一个网上商城项目,这个网上商城呢,比较复杂,分为 pc 端 web 管理后台,微信端销售公众号,那么我们分成四个项目,pc ...
- JavaScript基础知识(字符串的方法)
字符串的方法 1.字符串: 在js中被单引号或双引号包起来的内容都是字符串: var t = "true"; console.log(typeof t);// "stri ...
- Linux命令 printf
定长: $ printf '%s\t%s\t%s\t%s\t%s\t\n' $(cat printf.txt) # %s 表示一个不固定长度的字符串:printf不是管道命令,因此需要通过类似cat的 ...
- Java ee第七周作业
一.什么是JSON?JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度. JSON就是一串字符串 只不过元素会使用特定的 ...
- Linux 从源码编译安装 OpenSSH
https://blog.csdn.net/bytxl/article/details/46639073 Linux 从源码编译安装 OpenSSH以及各问题解决 2015年06月25日 17:37: ...