传送门

分析:其实没什么好分析的。统计一下负数个数。如果负数个数是偶数的话,就要尽量增加负数或者减少负数。是奇数的话就努力增大每个数的绝对值。用一个优先队列搞一下就行了。 我感觉这道题的细节极为多,非常复杂,其实是自己智障了。。

我看了一下学长菊苣的代码,好精巧。。。注释部分是他的代码

/*****************************************************/
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define offcin ios::sync_with_stdio(false)
#define sigma_size 26
#define lson l,m,v<<1
#define rson m+1,r,v<<1|1
#define slch v<<1
#define srch v<<1|1
#define sgetmid int m = (l+r)>>1
#define LL long long
#define ull unsigned long long
#define mem(x,v) memset(x,v,sizeof(x))
#define lowbit(x) (x&-x)
#define bits(a) __builtin_popcount(a)
#define mk make_pair
#define pb push_back
#define fi first
#define se second const int INF = 0x3f3f3f3f;
const LL INFF = 1e18;
const double pi = acos(-1.0);
const double inf = 1e18;
const double eps = 1e-9;
const LL mod = 1e9+7;
const int maxmat = 10;
const ull BASE = 31; /*****************************************************/ const int maxn = 2e5 + 5;
struct Node {
LL pos, val;
}node[maxn];
struct kk {
LL pos, val;
bool operator <(const kk &rhs) const {
return abs(val) > abs(rhs.val);
}
};
priority_queue<kk> q;
int N, k, x;
bool cmp1(const Node &a, const Node &b) {return a.val < b.val; }
bool cmp2(const Node &a, const Node &b) {return a.pos < b.pos; }
int main(int argc, char const *argv[]) {
cin>>N>>k>>x;
int neg = 0;
int flag = 1;
for (int i = 0; i < N; i ++) {
cin>>node[i].val;
node[i].pos = i;
if (node[i].val < 0) neg ++;
}
sort(node, node + N, cmp1);
if (neg && (neg & 1) == 0) {
int lb = 0, ub = N - 1, res = -1;
while (lb <= ub) {
int mid = (lb + ub) >> 1;
if (node[mid].val < 0) {
res = mid;
lb = mid + 1;
}
else ub = mid - 1;
}
bool flag1 = false, flag2 = false;
if ((LL)k * x + node[res].val >= 0) flag1 = true;
if ((LL)-k * x + node[res + 1].val < 0) flag2 = true;
if (res == N - 1) {
if ((LL)k * x + node[res].val >= 0) {
LL temp1 = ((-node[res].val - 1) / x + 1);
k -= temp1;
node[res].val += temp1 * x;
}
else flag = -1;
}
else if (flag1 && flag2) {
LL temp1 = ((-node[res].val - 1) / x + 1);
LL temp2 = (node[res + 1].val / x + 1);
// cout<<temp1<<" "<<temp2<<endl;
if (temp1 == temp2) {
if (abs(node[res].val) > abs(node[res + 1].val)) {
k -= temp2;
node[res + 1].val -= temp2 * x;
}
else {
k -= temp1;
node[res].val += temp1 * x;
}
}
else if (temp1 > temp2) {
k -= temp2;
node[res + 1].val -= temp2 * x;
}
else {
k -= temp1;
node[res].val += temp1 * x;
}
}
else if (flag1) {
int temp1 = ((-node[res].val - 1) / x + 1);
k -= temp1;
node[res].val += temp1 * x;
}
else if (flag2) {
int temp2 = (node[res + 1].val / x + 1);
k -= temp2;
node[res + 1].val -= temp2 * x;
}
else flag = -1;
}
else if (neg == 0) {
if ((LL)-x * k + node[0].val < 0) {
LL temp = (node[0].val / x + 1);
k -= temp;
node[0].val -= temp * x;
}
else flag = -1;
}
for (int i = 0; i < N; i ++) q.push((kk){node[i].pos, node[i].val});
while (k > 0) {
kk temp = q.top(); q.pop();
if (temp.val < 0) temp.val -= flag * x;
else temp.val += flag * x;
k --;
q.push((kk){temp.pos, temp.val});
}
int p = 0;
while (!q.empty()) {
kk temp = q.top(); q.pop();
node[p ++] = (Node){temp.pos, temp.val};
}
sort(node, node + N, cmp2);
for (int i = 0; i < N; i ++) cout<<node[i].val<<" ";
return 0;
}
// LL a[MAX]; // struct Edge{
// LL a,b;
// int id;
// bool operator < (const Edge &e)const{
// return a>e.a;
// }
// }; // LL labs(LL a){
// if(a<0) return -a;
// return a;
// }
// int main(){
// //freopen("in.txt","r",stdin);
// int n,k,x;
// while(cin>>n>>k>>x){
// int fu=0;
// priority_queue<Edge> q;
// for(int i=0;i<n;i++){
// scanf("%I64d",&a[i]);
// if(a[i]<0) fu++;
// q.push((Edge){labs(a[i]),a[i],i});
// }
// while(k--){
// Edge tmp=q.top();q.pop();
// if(fu%2){
// tmp.a+=x;
// if(tmp.b<0) tmp.b-=x;
// else tmp.b+=x;
// a[tmp.id]=tmp.b;
// }
// else{
// if(tmp.b<0){
// tmp.b+=x;
// if(tmp.b>=0) fu--;
// }
// else{
// tmp.b-=x;
// if(tmp.b<0) fu++;
// }
// tmp.a=labs(tmp.b);
// a[tmp.id]=tmp.b;
// }
// q.push(tmp);
// }
// for(int i=0;i<n;i++){
// printf("%I64d ",a[i]);
// }
// cout<<endl;
// }
// return 0;
// }

Codeforces Round #374 (Div. 2) D. Maxim and Array的更多相关文章

  1. Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心

    D. Maxim and Array 题目连接: http://codeforces.com/contest/721/problem/D Description Recently Maxim has ...

  2. Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心

    题目链接:http://codeforces.com/problemset/problem/721/D D. Maxim and Array time limit per test 2 seconds ...

  3. Codeforces Round #374 (Div. 2) D. Maxim and Array 线段树+贪心

    D. Maxim and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #374 (div.2)遗憾题合集

    C.Journey 读错题目了...不是无向图,结果建错图了(喵第4样例是变成无向就会有环的那种图) 并且这题因为要求路径点尽可能多 其实可以规约为限定路径长的拓扑排序,不一定要用最短路做 #prag ...

  5. Codeforces Round #374 (Div. 2) A B C D 水 模拟 dp+dfs 优先队列

    A. One-dimensional Japanese Crossword time limit per test 1 second memory limit per test 256 megabyt ...

  6. 拓扑序+dp Codeforces Round #374 (Div. 2) C

    http://codeforces.com/contest/721/problem/C 题目大意:给你有向路,每条路都有一个权值t,你从1走到n,最多花费不能超过T,问在T时间内最多能访问多少城市? ...

  7. Codeforces Round #374 (Div. 2) C. Journey DP

    C. Journey 题目连接: http://codeforces.com/contest/721/problem/C Description Recently Irina arrived to o ...

  8. Codeforces Round #374 (Div. 2) B. Passwords 贪心

    B. Passwords 题目连接: http://codeforces.com/contest/721/problem/B Description Vanya is managed to enter ...

  9. Codeforces Round #374 (Div. 2) A. One-dimensional Japanese Crosswor 水题

    A. One-dimensional Japanese Crossword 题目连接: http://codeforces.com/contest/721/problem/A Description ...

随机推荐

  1. mysql 5.6 online ddl

    innodb存储引擎实现online ddl的原理是在执行创建或删除操作的同时,将DML操作日志写入到一个缓存中,待完成索引创建后再重做应用到表上,以此达到数据的一致性,这个缓存大小由参数innodb ...

  2. ListView的基础入门

    1.先在XML中定义一个ListView视图 2.获得ListView,在Mainactivity中声明 3.创建一个类继承适配器BaseAdapter,实现四个方法 public class MyL ...

  3. CSS2中基本属性的介绍

    这是继上一篇的选择器的总结,对css2基本属性的小结!

  4. CSS3 :target伪类实现Tab切换效果

    用:target伪类实现Tab切换效果真的非常简单!简单到什么程度呢?它只需要下面这些代码. style.css: .song-info { position: absolute; backgroun ...

  5. 类加载机制(深入理解JAVA虚拟机学习笔记)

    1.类加载机制的定义 将class文件加载到内存,然后对class文件中的数据进行校验.解析和初始化,转换成可以被虚拟机直接使用的JAVA类型,这就是虚拟机的类加载机制.(在JAVA中,类的加载.连接 ...

  6. HTML相关问题

    1.XHTML和HTML有什么区别HTML是一种基本的WEB网页设计语言,XHTML是一个基于XML的置标语言最主要的不同:XHTML 元素必须被正确地嵌套.XHTML 元素必须被关闭.标签名必须用小 ...

  7. 关于 OJ1575的参考题解

    #include <stdio.h>int main(){ int a,b; scanf("%d",&a); b=0; while(a) { b+=a%10; ...

  8. ASP.NET中cookie与Fiter实现简单登陆,AllowAnonymous匿名登陆

    向服务器发送cookie 在登陆的时候,我们可以可以通过下列代码,向服务器发送cookie,其中包括自己的账号信息(不涉及加密),用以后面判断访问者. HttpCookie cookie = new ...

  9. php : 配置

    一. php: undefined function mysql_connect()  mac 上操作 一.有可能是因为版本不同而引起的 PHP5中使用mysql_connect()函数进行连接.但P ...

  10. 在Ubuntu下使用 csapp.h 和 csapp.c

    它山之石可以攻玉. 对于<深入理解计算机系统>这本神人写就的神书, 我等凡人就不评论什么啦. 这本书的 第二,三 部分, 真的真的对我理解操作系统有很大的帮助. (当然, 如果你不看第一部 ...