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]);   
    }
  • 相关阅读:
    centos安装docker-compose
    CentOS安装Docker
    【JPA】【笔记】GenerationType四中类型
    【maven】多个子项目、父项目之间的引用问题【转】
    一个页面多个echarts图表自适应
    jQuery操作单选按钮(radio)用法
    CASE WHEN .... THEN END 的用法
    遮罩层
    oracle 数据库的字段的增删改主键设定删除
    自动获取ROI-用于SFR算法的ROI获取
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13647216.html
Copyright © 2011-2022 走看看