zoukankan      html  css  js  c++  java
  • CodeForces

     

    Roma works in a company that sells TVs. Now he has to prepare a report for the last year.

    Roma has got a list of the company's incomes. The list is a sequence that consists of n integers. The total income of the company is the sum of all integers in sequence. Roma decided to perform exactly k changes of signs of several numbers in the sequence. He can also change the sign of a number one, two or more times.

    The operation of changing a number's sign is the operation of multiplying this number by -1.

    Help Roma perform the changes so as to make the total income of the company (the sum of numbers in the resulting sequence) maximum. Note that Roma should perform exactlyk changes.

    Input

    The first line contains two integers n and k (1 ≤ n, k ≤ 105), showing, how many numbers are in the sequence and how many swaps are to be made.

    The second line contains a non-decreasing sequence, consisting of n integers ai(|ai| ≤ 104).

    The numbers in the lines are separated by single spaces. Please note that the given sequence is sorted in non-decreasing order.

    Output

    In the single line print the answer to the problem — the maximum total income that we can obtain after exactly k changes.

    Examples

    Input

    3 2
    -1 -1 1

    Output

    3

    Input

    3 1
    -1 -1 1

    Output

    1

    Note

    In the first sample we can get sequence [1, 1, 1], thus the total income equals 3.

    In the second test, the optimal strategy is to get sequence [-1, 1, 1], thus the total income equals 1.

    变K次,而且本来数组就是有序的,想让最早的负数都变成,然后就只有如下可能:

    1.K次变换结束后,仍存在负数

    2.K次变换结束后,恰好无负数

    3.K次变换未完成,无负数

    前两种直接求和,因为每次对最小值取反,一定是最优解。

    第三种,如果数组中有0,或者 剩余K为偶数,那么最优是保持原来数组的大小即为最优,K次全部作用于偶数,或作用于零,不改变数组大小。

    另外一种情况,K次操作后必然至少会有一个值被取反,所以一定是最小的数字,时间允许,直接排序,干就完事。

    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<set>
    #include<cmath>
    #include<vector>
    #include<map>
    #include<stack>
    #include<bitset>
    #include<cstdio>
    #include<cstring>
    //---------------------------------Sexy operation--------------------------//
    
    #define cini(n) scanf("%d",&n)
    #define cinl(n) scanf("%lld",&n)
    #define cinc(n) scanf("%c",&n)
    #define cins(s) scanf("%s",s)
    #define coui(n) printf("%d",n)
    #define couc(n) printf("%c",n)
    #define coul(n) printf("%lld",n)
    #define speed ios_base::sync_with_stdio(0)
    #define file  freopen("input.txt","r",stdin);freopen("output.txt","w",stdout)
    //-------------------------------Actual option------------------------------//
    
    #define Swap(a,b) a^=b^=a^=b
    #define Max(a,b) a>b?a:b
    #define Min(a,b) a<b?a:b
    #define mem(n,x) memset(n,x,sizeof(n))
    #define mp(a,b) make_pair(a,b)
    //--------------------------------constant----------------------------------//
    
    #define INF  0x3f3f3f3f
    #define maxn  100010
    #define esp  1e-9
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> PII;
    //------------------------------Dividing Line--------------------------------//
    int n,m;
    int a[maxn];
    int main()
    {
        cini(m),cini(n);
        int t=0;
        for(int i=0;i<m;i++)
        {
            cin>>a[i];
            if(a[i]<0){
                if(a[i]==0) t=i;
                if(n)
                {
                    a[i]=-a[i];
                    n--;
                }
            }
    
        }
        long long ans=0;
        if(t==0&&n!=0&&n%2==1)
        {
            sort(a,a+m);
            a[0]=-a[0];
            for(int i=0;i<m;i++) ans+=a[i];
            cout<<ans<<endl;
            return 0;
    
        }
        else {
             for(int i=0;i<m;i++) ans+=a[i];
            cout<<ans<<endl;
            return 0;
    
        }
    
    }
  • 相关阅读:
    Java深入学习31:ArrayList并发异常以及解决方案
    Java深入学习30:CAS中的ABA问题以及解决方案
    Java深入学习29:线程等待和唤醒的两个方案
    Redis学习05:Springboot集成Redis集群cluster
    项目总结66:Springboot项目继承kafka集群
    项目总结65:内存溢出OOM问题处理
    异常处理009:Windows10远程桌面连接提示:出现身份验证错误,要求的函数不受支持
    项目总结64:分别使用Redisson和Zookeeper分布式锁模拟模拟抢红包业务
    项目总结63:使用Spring AOP和BindingResult实现对接口的请求数据校验,并用@ExceptionHandler返回校验结果
    Java深入学习04:深入理解HashMap
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798682.html
Copyright © 2011-2022 走看看