首页 > 其他 > 详细

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

时间:2020-03-08 23:07:54      阅读:84      评论:0      收藏:0      [点我收藏+]

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;
}

 

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

原文:https://www.cnblogs.com/wlzs1432/p/12445715.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!