Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
来源:互联网 发布:htcg5软件 编辑:程序博客网 时间:2024/05/21 10:56
题目链接:这里
题意:给你n个数,你可以操作k次,每次使得一个数增加x或者减小x,你要使得最后所有数的乘积最小,问你最后这个序列长什么样子。
解法:贪心,根据符号的不同,每次贪心的使得一个绝对值最小的数减去x或者加上x就好了,这个贪心比较显然。假设当前乘积为ANS,那么你改变a[i]的大小的话,那么对答案的影响为ANS/A[i]/*X。然后找到影响最大的就好了。
//CF 721D#include <bits/stdc++.h>using namespace std;const int maxn = 2e5+7;long long a[maxn];int n, k, x;set <pair <long long , long long> > S;int main(){ scanf("%d%d%d", &n, &k, &x); int sign = 0; for(int i = 1; i <= n; i++){ scanf("%lld", &a[i]); if(a[i] < 0) sign ^= 1; S.insert(make_pair(abs(a[i]), i)); } for(int i = 1; i <= k; i++){ int pos = S.begin()->second; S.erase(S.begin()); if(a[pos] < 0) sign ^= 1; if(sign) a[pos] += x; else a[pos] -= x; if(a[pos] < 0) sign ^= 1; S.insert(make_pair(abs(a[pos]), pos)); } for(int i = 1; i <= n; i++){ printf("%lld ", a[i]); } printf("\n");}
0 0
- Codeforces Round #374 (Div. 2)D. Maxim and Array(贪心)
- Codeforces Round 374 (Div 2)D Maxim and Array 【贪心】
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Codeforces Round #374 (Div. 2) D - Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心+ 最小堆
- Codeforces Round #374 (Div. 2) D. Maxim and Array —— 贪心
- 【Codeforces Round 374 (Div 2)D】【贪心】 Maxim and Array n个数做K次±X使得乘积尽可能小
- Codeforces 721D Maxim and Array[贪心]
- codeforces 721D Maxim and Array(贪心)
- codeforces 721D. Maxim and Array(贪心)
- CodeForces 721D. Maxim and Array(贪心,思维题)
- Codeforces712 D. Maxim and Array (贪心)
- 721D Maxim and Array codeforces(模拟)
- 【24.17%】【codeforces 721D】Maxim and Array
- Codeforces Round #331 (Div. 2) B. Wilbur and Array (贪心)
- JAVA_多线程
- C/C++标识符的规定
- 紧急救援
- 搭建以太坊私有链和部署智能合约开发环境
- React学习之进阶性能优化(十五)
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Android 数据清除工具类
- HDU 1143Tri Tiling&&POJ2663
- CyclicBarrier 高并发的使用
- PhoneView
- 网络协议逆向分析
- dpdk内存管理之内存分配器(堆分配)
- jdbc连接数据库,使用通配符
- middle-box