贪心 特技飞行

版权声明:未经本蒟蒻同意,请勿转载本蒻博客 https://blog.csdn.net/wddwjlss/article/details/81810977

题意:有N个单位时间,每个单位时间可以进行一项动作,可选的动作有K种,每种动作有一个刺激程度Ci。定义某次动作的价值为(距上次该动作的时间)*Ci,若为第一次进行该动作,价值为0。安排一种方案,使得总价值最大。

做法:贪心,让Ci越大的动作相距越远,在首尾固定的情况下,该动作无论做多少次贡献的价值与只进行头尾两次动作相等。

#include<bits/stdc++.h>
using namespace std;
int n,k,c[100100],ans,l,r;
int main()
{
    cin>>n>>k;
    for(int i=1;i<=k;++i)
        scanf("%d",&c[i]);
    sort(c+1,c+k+1);
    l=1,r=n;
    for(int i=k;i>=1;--i)
    {
        ans+=(r-l)*c[i];
        r--;
        l++;
        if(r<l)
            break;
    }
    cout<<ans;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wddwjlss/article/details/81810977