题目:
题目描述
有一个整数序列,它的每个数各不相同,我们不知道它的长度是多少(即整数个数),
但我们知道在某些区间中至少有多少个整数,用区间(Li,Ri,Ci)来描述,
表示这个整数序列中至少有 Ci 个数来自区间[Li,Ri],给出若干个这样的区间,
问这个整数序列的长度最少能为多少?
输入输出格式
输入格式:
第一行一个整数N,表示区间个数;接下来N行,每行三个整数(Li,Ri,Ci),描述一个区间。
输出格式:
仅一个数,表示该整数序列的最小长度。
输入输出样例
输入样例#1:
4
4 5 1
6 10 3
7 10 3
5 6 1
输出样例#1:
4
说明
N<=1000,0<=Li<=Ri<=1000,1<=Ci<=Ri - Li + 1
思路:
按照右端点排序,然后把所有数都尽量放在靠后的地方。
代码:
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define maxn 1000
#define read(x) scanf("%d",&x);
struct Node {
int l,r,c;
Node() {}
Node(int ll,int rr,int cc) {
l=ll,r=rr,c=cc;
}
bool operator < (const Node& oth) const {
return r<oth.r||(r==oth.r&&l<oth.l);
}
};
int n;
Node a[maxn+5];
bool num[maxn+5];
void readin() {
read(n);
for(int i=1; i<=n; i++) {
read(a[i].l);read(a[i].r);read(a[i].c);
}
sort(a+1,a+n+1);
}
int main() {
readin();
for(int i=1; i<=n; i++) {
int s=0;
for(int j=a[i].l; j<=a[i].r; j++) {
s+=num[j];
}
if(s>=a[i].c) continue;
else s=a[i].c-s;
for(int j=a[i].r; j>=a[i].l&&s>0; j--) {
if(num[j]) continue;
num[j]=true;
s--;
}
}
int ans=0;
for(int i=1; i<=maxn; i++) {
ans+=num[i];
}
printf("%d",ans);
return 0;
}