• 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]);   
    }
  • 相关阅读:
    C#可以自由移动和伸缩的TextBox
    CSS:position(定位)
    CSS:margin 和 padding
    JavaScript学习(一) 与C#语法重大区别
    Epplus使用
    正则表达式匹配IP地址
    C#窗体自定义控件特性(Attribute)的使用
    Sqlserver中OVER子句
    股票开头带字母
    CSS控制元素的显示类型和可见性
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13647216.html
Copyright © 2020-2023  润新知