LOJ#2347. 「JOI 2018 Final」寒冬暖炉

LOJ#2347. 「JOI 2018 Final」寒冬暖炉

题面

链接

题解

普及组难度贪心

把间隔时间算出来

然后从大到小减掉

#include<bits/stdc++.h>

#define LL long long

using namespace std;

inline LL read()
{
    LL f = 1 , x = 0;
    char ch;
    do
    {
        ch = getchar();
        if(ch=='-') f=-1;
    } while(ch<'0'||ch>'9');
    do
    {
        x=(x<<3) + (x<<1) + ch - '0';
        ch = getchar();
    }while(ch>='0'&&ch<='9');
    return f*x;
}

const int MAXN = 100000 + 10;

int N,k;
int a[MAXN];
int b[MAXN];
int ans;

inline bool cmp(int a1,int a2)
{
    return a1 > a2;
}

int main()
{
    N = read();k = read();
    for(register int i=1;i<=N;i++)
    {
        a[i] = read();
    }
    sort(a+1,a+N+1);
    ans = a[N] - a[1] + 1;
    for(register int i=2;i<=N;i++)
    {
        b[i-1] = a[i] - a[i-1] - 1;
    }
    sort(b+1,b+N,cmp);
    for(int i=1;i<k;i++) ans -= b[i];
    cout << ans << endl;
    return 0;
}

 

posted @ 2020-03-08 22:59  wlzs1432  阅读(216)  评论(0编辑  收藏  举报