zoukankan      html  css  js  c++  java
  • CF721D Maxim and Array(贪心)

    题意:

    给出K和X,一次操作你可以对序列中的一个元素+X或-X,最多操作K次,询问怎么操作使得序列乘积最小。

    题解:

    /*
     *author: zlc
     *zucc_acm_lab
     *just do it
     */
    #include<bits/stdc++.h> 
    using namespace std;
    typedef long long ll;
    const double pi=acos(-1.0);
    const double eps=1e-6;
    const int mod=1e9+7;
    const int inf=1e9;
    const int maxn=2e5+100;
    inline ll read () {ll x=0;ll f=1;char ch=getchar();while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
    //inline int read () {int x=0;int f=1;char ch=getchar();while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;}
    ll qpow (ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    ll n,k,x;
    ll a[maxn];
    struct qnode {
        ll v;
        bool operator < (const qnode &r) const {return abs(a[v])>abs(a[r.v]);}
        qnode (ll vv) {v=vv;}
    };
    int main () {
        n=read();k=read();x=read();
        for (int i=1;i<=n;i++) a[i]=read();
        int cnt=0;//负数的数量
        for (int i=1;i<=n;i++) if (a[i]<0) cnt++;
        priority_queue<qnode> q;
        for (int i=1;i<=n;i++) q.push(qnode(i));
        int tot=0;
        int f=0;
        while (q.size()&&tot<k) {
            qnode tt=q.top();
            q.pop();
            if (a[tt.v]<0) {
                if (cnt%2==0) {
                    a[tt.v]+=x;
                    if (a[tt.v]>=0) cnt--;     
                }
                else
                    a[tt.v]-=x;
            }
            else {
                if (cnt%2==0) {
                    a[tt.v]-=x;
                    if (a[tt.v]<0) cnt++;    
                }
                else
                    a[tt.v]+=x;
            }
            q.push(tt.v);
            tot++;
        }
        for (int i=1;i<=n;i++) printf("%lld ",a[i]);   
    }
  • 相关阅读:
    转载:MyBatis获取插入记录的自增长字段值
    006---抽象类
    005---组合
    004---继承与派生
    003---属性查找和绑定方法
    002---类与对象
    001---面向对象和面向过程的区别
    017---Django的中间件解决跨域
    10---git安装
    007---归并排序
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13647216.html
Copyright © 2011-2022 走看看