codeforces 721d

Maxim and Array

题意:n个数,k次操作,每次对任意一个数+x 或 -x,使得所有的数乘积最小,求最后的数组

思路:一共有3种情况,1、初始有奇数个负号, 2、初始有偶数个负号,但是可以转化成奇数个负号, 3、初始有偶数个负号,并且不能转化成奇数个负号;分好类之后贪心,如果有奇数个负号,那么每次选择绝对值小的数操作,使其绝对值变大,如果是偶数个,那么先对绝对值最小的数操作,使其符号变化,如果是负数那么+x,否则-x,变为奇数个负号之后就和前面一样了

AC代码:

#include "iostream"
#include "iomanip"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#pragma comment(linker, "/STACK:102400000,102400000")
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a,x) memset(a,x,sizeof(a))
#define step(x) fixed<< setprecision(x)<<
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define ll long long
#define endl ("\n")
#define ft first
#define sd second
#define lrt (rt<<1)
#define rrt (rt<<1|1)
using namespace std;
const ll mod=1e9+7;
const ll INF = 1e18+1LL;
const int inf = 1e9+1e8;
const double PI=acos(-1.0);
const int N=2e5+100;

struct Node{
    ll v, fv, id;
    void myabs(){
        fv=abs(v);
    }
    bool friend operator<(Node a, Node b){
        return a.fv>b.fv;
    }
}a;
priority_queue<Node> Q;
ll n,f,x,k,ans[N];
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>k>>x;
    for(int i=1; i<=n; ++i){
        cin>>a.v; a.id=i, a.myabs();//a.fv=abs(a.v);
        Q.push(a);
        if(a.v<0) f++;
    }
    if(f&1){
        while(k--){
            Node now=Q.top();
            Q.pop();
            if(now.v<0) now.v-=x;
            else now.v+=x;
            now.myabs();
            Q.push(now);
        }
    }
    else{
        Node now=Q.top();
        Q.pop();
        while(k--){
            int f1=now.v>=0?1:-1;
            if(now.v<0) now.v+=x;
            else now.v-=x;
            now.myabs();
            int f2=now.v>=0?1:-1;
            if(f1+f2==0){
                Q.push(now);
                break;
            }
        }
        if(k==-1){
            Q.push(now);
            k=0;
        }
        while(k--){
            now=Q.top();
            Q.pop();
            if(now.v<0) now.v-=x;
            else now.v+=x;
            now.myabs();
            Q.push(now);
        }
    }
    int t=Q.size();
    while(!Q.empty()){
        ans[Q.top().id]=Q.top().v, Q.pop();
    }
    for(int i=1; i<=t; ++i){
        cout<<ans[i]<<" ";
    }
    return 0;
}

 

posted on 2017-10-09 23:16  lazzzy  阅读(147)  评论(0编辑  收藏  举报

导航