题意:给你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. 哈密顿绕行世界问题 (dfs)

    一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市.  Input前20行的第i行有3个数,表示与第i个城市相邻的3个城市.第20行 ...

  2. express.js graphql express-graphql

    文档 创建应用 const l = console.log; var express = require("express"); var graphqlHTTP = require ...

  3. common lisp的几个基本概念

    S-表达式 quote nil 与 () cons car cdr 真假 predicate 谓词与 t 与 nil null 函数 与 not 函数 if then else and 与 or de ...

  4. 查看 java 中的编译的字节码文件

    javap -c Atomicity ---------- javap -c 产生的字节码文件---------- Compiled from "Atomicity.java" p ...

  5. 18.jwt加密

    jwt 官网https://jwt.io/ jwt:json web token jwt-simple: https://www.npmjs.com/package/jwt-simple jsonwe ...

  6. /编写一个函数,要求从给定的向量A中删除元素值在x到y之间的所有元素(向量要求各个元素之间不能有间断), 函数原型为int del(int A ,int n , int x , int y),其中n为输入向量的维数,返回值为删除元素后的维数

    /** * @author:(LiberHome) * @date:Created in 2019/2/28 19:39 * @description: * @version:$ */ /* 编写一个 ...

  7. git迁移

    git迁移 项目开发的不同阶段可能要使用不同的git仓库,有时需要迁移. git有很好的方法,只需要几个命令 目标: 我们需要把代码从 http://a.com/projectA.git 迁移到 ht ...

  8. Docker入门基础(一)

    Docker入门基础 Linux只存在文件目录,不存在“盘”的概念 Dockers优点:方便部署环境.资源占用少(微服务) Docker的三大概念 镜像:类似虚拟机的镜像.用俗话说就是安装文件.容器: ...

  9. UI自动化框架——构建思维

    目的:从Excel中获取列的值,传输到页面 技巧:尽可能的提高方法的重用率 Java包: 1.java.core包 3个类:1)日志(LogEventListener)扩展web driver自带的事 ...

  10. 新建Maven项目时dtd约束出错

    新建或者导入Maven项目时出错:org.apache.maven.archiver.MavenArchiver.getManifesteclipse新建maven项目时,pom.xml文件第一行报错 ...