题意:给你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(贪心 + 思维)题解的更多相关文章

  1. CodeForces 508E Arthur and Brackets 贪心

    题目: E. Arthur and Brackets time limit per test 2 seconds memory limit per test 128 megabytes input s ...

  2. codeforces 233 C. Cycles(贪心+思维)

    题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪 ...

  3. Codeforces 449C Jzzhu and Apples 贪心 (看题解)

    Jzzhu and Apples 从大的质因子开始贪心, 如果有偶数个则直接组合, 如果是奇数个留下那个质数的两倍, 其余两两组合. #include<bits/stdc++.h> #de ...

  4. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  5. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  6. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  7. Educational Codeforces Round 63 (Rated for Div. 2) 题解

    Educational Codeforces Round 63 (Rated for Div. 2)题解 题目链接 A. Reverse a Substring 给出一个字符串,现在可以对这个字符串进 ...

  8. Educational Codeforces Round 65 (Rated for Div. 2)题解

    Educational Codeforces Round 65 (Rated for Div. 2)题解 题目链接 A. Telephone Number 水题,代码如下: Code #include ...

  9. Educational Codeforces Round 64 (Rated for Div. 2)题解

    Educational Codeforces Round 64 (Rated for Div. 2)题解 题目链接 A. Inscribed Figures 水题,但是坑了很多人.需要注意以下就是正方 ...

随机推荐

  1. gym 101657 D

    理论1A.  //没删debug的文件读入.. 傻逼题. 先求出来每条边两侧的三角形,然后枚举边,根据叉积判断三角形位置,建图,拓扑排序. #include <bits/stdc++.h> ...

  2. 【微信小程序——开发步骤1】

    知识点:   view,image,text编写文本框架 使用弹性盒子动态布局 使用rpx调试分辨率 在wxml中查看默认样式属性 步骤: 1.以如图页面实例说明如何写出微信文本内容 先对页面写出整体 ...

  3. 逆向工程之修改关键CALL返回值_破解视频转换专家

    1)注册软件随便输入注册名注册码 2)进入软件根目录,发送到PEID查壳 3)发现无壳 4)发送到OD 4.1)右键菜单选择智能搜索 4.2)找到关键信息点注册 4.3)找到关键信息点双击进入汇编,向 ...

  4. PAT甲级1068 Find More Coins【01背包】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805402305150976 题意: n个硬币,每一个有一个特有的价 ...

  5. PostgreSQL 自动输入密码(转)

    原文:https://www.cnblogs.com/litmmp/p/5122534.html 在 Shell 命令行中,使用 postgresql-client 连接 PostgreSQL 数据库 ...

  6. cestos7安装zookeeper

    zookeeper安装包下载地址http://archive.apache.org/dist/zookeeper/ 一.单机 在/usr目录下 curl -L-O http://archive.apa ...

  7. Windows平台搭建Kafka

    1. 安装JDK 1.1 安装文件:http://www.oracle.com/technetwork/java/javase/downloads/jre8-downloads-2133155.htm ...

  8. unity UGUI UI跟随

    实现2dUI跟随游戏中角色的移动(应用于玩家名称,血条,称号) using UnityEngine; public class UI_Follow : MonoBehaviour { public C ...

  9. 关闭jtag保留swd

    串行线JTAG 配置 (Serial wire JTAG configuration)  位26:24  这些位只能由软件写( 读这些位,将返回未定义的数值) ,用于配置SWJ和跟踪复用功能的I/O ...

  10. Java web开发环境搭配

    1.安装并配置JDK环境(1)安装过程省略(建议安装在自己指定的统一目录下,方便后期查找). (2)配置环境变量 JAVA_HOME:  C:\Java\jdk\jdk1.7.0_45 (jdk安装目 ...