JZOJ5274. 数组

来源:互联网 发布:fifa online315数据库 编辑:程序博客网 时间:2024/06/03 21:41

这里写图片描述

分析

先考虑只有正数的情况,
假设有三个正数,a,b,c,
a是最小的,可以得到
(a1)bc<a(b1)c
那么:
(a+1)bc=abc+bc,a(b+1)c=abc+ac
因为ac<bc,所以(a+1)bc>a(b+1)c
也就是说,我们现在的贪心策略就是:
每次找最小的数,然后加上x。

如果一开始给出的ai 的积是正数,
那我们就找出一个最接近0的,改变它的符合。

code

#include<queue>#include<cstdio>#include<iostream>#include<algorithm>#include <cstring>#include <string.h>#include <cmath>#include <math.h>#define ll long long#define N 100003#define db double#define P putchar#define G getchar#define mo 1000000007using namespace std;char ch;void read(ll &n){    n=0;    ch=G();    while((ch<'0' || ch>'9') && ch!='-')ch=G();    ll w=1;    if(ch=='-')w=-1,ch=G();    while('0'<=ch && ch<='9')n=n*10+ch-'0',ch=G();    n*=w;}struct node{    ll x;}t;priority_queue <node> q;bool operator <(node a,node b){    return a.x>b.x;}ll n,k,x,y,s;ll ans;int main(){    ans=1;s=0;    read(n),read(k),read(x);    for(int i=1;i<=n;i++)    {        read(y);        if(y<0)s++;        t.x=abs(y);        q.push(t);    }    if(s%2==0)    {        t=q.top();        y=t.x/x;        if(t.x%x)y++;        if(y>k)y=k;else s++;        k-=y;        t.x=abs(t.x-x*y);        q.pop();        q.push(t);    }    while(k)    {        t=q.top();        q.pop();        t.x+=x;        q.push(t);        k--;    }    while(!q.empty())t=q.top(),q.pop(),ans=ans*(t.x%mo)%mo;    if(s%2)ans=(mo-ans)%mo;    printf("%lld\n",ans);}
原创粉丝点击