Codeforces712 D. Maxim and Array (贪心)
来源:互联网 发布:淘宝新开店铺采集 编辑:程序博客网 时间:2024/05/18 14:28
题目连接:http://codeforces.com/contest/721/problem/D
题目大意:给出一个序列,一个数x,一次操作可以让序列里的数+x或这-x,最多操作k次,要求最后序列的数乘积最小。
解题思路:贪心。每次对绝对值最小的数进行操作,然后对其分类讨论。
/* ***********************************************┆ ┏┓ ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃ ┃ ┆┆┃ ━ ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃ ┃ ┆┆┃ ┻ ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆ ┃ 勒 ┃ ┆ ┆ ┃ 戈 ┗━━━┓ ┆┆ ┃ 壁 ┣┓┆┆ ┃ 的草泥马 ┏┛┆┆ ┗┓┓┏━┳┓┏┛ ┆┆ ┃┫┫ ┃┫┫ ┆┆ ┗┻┛ ┗┻┛ ┆************************************************ */#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <bitset>using namespace std;#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)#define per(i,a,b) for (int i=(b),_ed=(a);i>=_ed;i--)#define pb push_back#define mp make_pairconst int inf_int = 2e9;const long long inf_ll = 2e18;#define inf_add 0x3f3f3f3f#define mod 1000000007#define LL long long#define ULL unsigned long long#define MS0(X) memset((X), 0, sizeof((X)))#define SelfType intSelfType Gcd(SelfType p,SelfType q){return q==0?p:Gcd(q,p%q);}SelfType Pow(SelfType p,SelfType q){SelfType ans=1;while(q){if(q&1)ans=ans*p;p=p*p;q>>=1;}return ans;}#define Sd(X) int (X); scanf("%d", &X)#define Sdd(X, Y) int X, Y; scanf("%d%d", &X, &Y)#define Sddd(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())#define all(a) a.begin(), a.end()#define mem(x,v) memset(x,v,sizeof(x))typedef pair<int, int> pii;typedef pair<long long, long long> pll;typedef vector<int> vi;typedef vector<long long> vll;inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,rx=getchar();return ra*fh;}//#pragma comment(linker, "/STACK:102400000,102400000")LL Abs(LL x){return x < 0 ? -x : x;}struct node{ LL val; int id; bool operator < (const node &a)const { return Abs(val) > Abs(a.val); }};LL a[200005];int main(){//freopen("in.txt","r",stdin);//freopen("out.txt","w",stdout);ios::sync_with_stdio(0);cin.tie(0);int n,k,x;n = read(), k = read(), x = read();int sign = 1;priority_queue<node>q;for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); if(a[i]<0)sign = -sign; q.push(node{a[i],i}); }while(k--) { node y = q.top(); q.pop(); if(a[y.id]<0) { if(sign<0) a[y.id] -= x; else a[y.id] += x; if(a[y.id]>=0) sign = -sign; } else { if(sign<0) a[y.id] += x; else a[y.id] -= x; if(a[y.id]<0) sign = -sign; } q.push(node{a[y.id],y.id}); }for(int i=1;i<=n;i++) { printf("%I64d",a[i]); if(i!=n)printf(" "); else printf("\n"); }return 0;}
0 0
- Codeforces712 D. Maxim and Array (贪心)
- Codeforces 721D Maxim and Array[贪心]
- codeforces 721D Maxim and Array(贪心)
- codeforces 721D. Maxim and Array(贪心)
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- Codeforces Round 374 (Div 2)D Maxim and Array 【贪心】
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- codeforces721D Maxim and Array(贪心)
- cf 721 D Maxim and Array
- 721D Maxim and Array codeforces(模拟)
- 【24.17%】【codeforces 721D】Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心+ 最小堆
- Codeforces 721D Maxim and Array【贪心+优先队列+分类讨论】
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
- codeforces 721D D. Maxim and Array (STL||优先队列)
- CF374 Maxim and Array
- STL常用的拷贝替换算术集合算法
- 最常用的 Eclipse 快捷键总结
- Qt openGL基础教程
- 算法_分治法求最大子数组
- MVC框架之composer加载(一)
- Codeforces712 D. Maxim and Array (贪心)
- spring加载外置配置文件
- Github团队协作教程
- java 求最长回文子串
- Sublime Text 3 3126 注册码
- 关于SQL漏洞--不用statement的原因
- NLP:stanfordNLP
- POJ 1273 && 3469 【最大流】
- 敏捷开发学习总结(1):传统序列式软件开发方法的缺点,以及迭代开发方法的选择