1. 程式人生 > >Codeforces Global Round B Tape

Codeforces Global Round B Tape

blank c代碼 開始 lan bsp 一段 typedef main pro

題目:點我轉移

【題意】


x軸上有m個連續的點,從1標號到m.
其中有n個點是特殊點。
讓你用k段區間將這n個點覆蓋。
要求區間的總長度最小。

解:

一開始假設我們需要n個膠帶(即包含每一個點)
然後因為m<=n
所以可能膠帶不夠用。
那麽就得一個膠帶跨過兩個點。
怎麽選擇最好呢?
可以把b[i]-b[i-1]-1處理出來排個序。
(優先取較小的花費)
然後取前n-m個累加和sum。
因為每取一個就少用一段膠帶.

AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int
maxn=1e5+10; int a[maxn]; int b[maxn]; int main() { int n,k,m; cin>>n>>k>>m; for(int i=0;i<n;i++) cin>>a[i]; int p=0; for(int i=1;i<n;i++) b[p++]=a[i]-a[i-1]-1; ll ans=0; ans+=n; sort(b,b+p); for(int
i=0;i<n-m;i++) ans+=b[i]; cout<<ans<<endl; return 0; }

Codeforces Global Round B Tape