JZOJ 5274. 数组
来源:互联网 发布:matlab 无约束最优化 编辑:程序博客网 时间:2024/06/09 15:16
Description
Input
Output
Sample Input
输入样例1:
3 2 7
5 4 2
输入样例2:
5 3 1
5 4 3 5 5
Sample Output
输出样例1:
999999732
输出样例2:
0
Data Constraint
Solution
显然,乘积最好是负数,这样会更优,那么用一个布尔型变量
Pd 表示当前的负数数量的奇偶性。因为加减操作的对象的值的绝对值一定是当前最小的,这样才会显得更优(贪心)。
于是我们维护一个堆,取出其绝对值最小的那个,再对
Pd 分类讨论:若负数数量为奇数,说明乘积为负数;
若负数数量为偶数,说明乘积为正数;
再根据当前值的正负性来判断加减,之后压回堆中即可,时间复杂度
O(K log N) 。
Code
#include<cstdio>#include<cmath>#include<queue>using namespace std;typedef long long LL;const int N=2e5+1,mo=1e9+7;int n,k,x;int a[N];bool pd;struct data{ int id; data(int x){id=x;} friend bool operator <(data x,data y) { return abs(a[x.id])>abs(a[y.id]); }};priority_queue<data>q;inline int read(){ int X=0,w=1; char ch=0; while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar(); return X*w;}int main(){ n=read(),k=read(),x=read(); for(int i=1;i<=n;i++) { a[i]=read(); if(a[i]<0) pd=!pd; q.push(data(i)); } while(k--) { int t=q.top().id; q.pop(); if(a[t]<0) { if(pd) a[t]-=x; else a[t]+=x; if(a[t]>=0) pd=!pd; }else { if(pd) a[t]+=x; else a[t]-=x; if(a[t]<0) pd=!pd; } q.push(t); } LL ans=1; for(int i=1;i<=n;i++) ans=ans*a[i]%mo; printf("%lld",(ans+mo)%mo); return 0;}
阅读全文
1 0
- JZOJ 5274. 数组
- jzoj 1979 【树状数组】 星星点灯
- jzoj 4690. 【GDOI2017模拟8.12】字符串 后缀数组+RMQ
- jzoj 4683. 【GDOI2017模拟8.11】矩阵 后缀数组+哈希表
- JZOJ 100037【NOIP2017提高A组模拟7.11】后缀数组
- jzoj 4711. 【NOIP2016提高A组模拟8.17】Binary 树状数组+位运算
- jzoj 5016. 【NOI2017模拟3.15】数列 最长不下降序列+树状数组
- JZOJ 3.10 1542——跑步(树状数组+模拟+排序/归并排序)
- JZOJ 4.8 2435——校门外的树【树状数组】
- [jzoj]3542. 【清华集训2014】冒泡排序(树状数组+读入/输出优化+思维)
- [JZOJ 3424] 粉刷匠 && [JZOJ 4254] 集体照
- jzoj 3873. 【NOIP2014八校联考第4场第2试10.20】乐曲创作(music) 构造题+树状数组+线段树
- [JZOJ 1280]最大匹配
- [JZOJ 1281]旅行
- [1282 JZOJ]资源勘探
- [JZOJ 1283]排序统计
- JZOJ NOIP2014模拟 8.12
- JZOJ NOIP2014模拟 8.13
- Java互联网架构学习之page分页
- 天天爱跑步NOIP
- SAP中XML文件与内表的相互转换
- img
- js键值
- JZOJ 5274. 数组
- Java开发中的23种设计模式详解(转)
- 【nyoj 119】 士兵杀敌(三) 【RMQ ST算法】
- Springmvc的转换器
- redis --内部结构--intset
- 第一章 Spring 入门 -- Spring4.3.8参考文档中文版
- java线程为什么弃用stop,suspend方法
- STM8S103之tim4普通定时器
- bzoj 1603: [Usaco2008 Oct]打谷机(拆点并查集)