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]);   
    }
  • 相关阅读:
    Spring:@ConfigurationProperties配置绑定
    Linux:性能诊断
    【第二章】:深浅拷贝剖析
    【第二章】:模块和运算符
    python 扩展注册功能装饰器举例
    python 函数 之 用户注册register()
    python 之 函数 基础
    python 函数
    python 文件操作
    python 的 数据类型
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13647216.html
Copyright © 2011-2022 走看看