http://vjudge.net/problem/UVALive-6911
Last night, Kingdom of Light was attacked by Kingdom of Dark! The queen of Kingdom of Light,
Queen Ar, was captured and locked inside a dark and creepy castle. The king of Kingdom of Light,
King Ash, wants to save the queen.
The castle is guarded by N dragons, conveniently numbered from 1 to N. To save Queen Ar, King
Ash must kill all the dragons. The kingdom’s oracle said that in order to kill the i-th dragon, King Ash
has to slay it with exactly two swords, one in each hand: one sword of length Ai units, and another
sword of length between Bi and Ci
, inclusive. King Ash can carry unlimited number of swords, and
each sword can be used multiple times.
The number of blacksmiths in the kingdom is limited, so it is important to make as few swords as
possible. Besides, making swords is expensive and takes much time. Determine the minimum number
of swords the kingdom has to make so that King Ash can save Queen Ar!
Input
The first line of input contains an integer T (T ≤ 20) denoting the number of cases. Each case begins
with an integer N (1 ≤ N ≤ 100,000) denoting the number of dragons which have to be killed. The next
N lines each contains three integers: Ai
, Bi
, and Ci (1 ≤ Ai ≤ 1,000,000; 1 ≤ Bi ≤ Ci ≤ 1,000,000)
denoting the swords’ length needed to kill the i-th dragon as described in the above problem statement.
Output
For each case, output ‘Case #X: Y ’, where X is the case number starts from 1 and Y is the minimum
number of swords the kingdom has to make and carry in order to defeat all the dragons and save Queen
Ar.
Explanation for 1st sample case:
The kingdom has to make two swords in order to defeat one dragon.
Explanation for 2nd sample case: All the dragons can be defeated by three swords, for example,
with lengths of: 3, 6, and 15.
• The fist dragon can be defeated by swords of length 3 and 6.
• The second dragon can be defeated by swords of length 6 and 15.
• The third dragon can be defeated by swords of length 3 and 15.
There also exists other combination of three swords.
Sample Input
4
1
7 6 8
3
3 5 10
6 11 15
3 13 15
4
1 10 20
3 50 60
2 30 40
4 70 80
2
5 100 200
150 1000 2000
Sample Output
Case #1: 2
Case #2: 3
Case #3: 8
Case #4: 3
题意 有n条龙要打败,对每条龙,有两把剑,一把长度固定,另一把给出剑的长度范围,必须同时用这两把剑打败,剑可以重复利用,问打败这n条龙最少需要多少把剑。
思路,首先对于给定的n把剑,一定要用且去重,用set即可,对于n个区间,按照右端点排序,利用set判断,如果区间内有set中元素,此区间舍去,否则单独拿出来再判断,按照右端点排序,没有重合部分即多用一把剑。
#include <iostream>
#include <cstdio>
#include <set>
#include <algorithm>
using namespace std;
set<int>S;
set<int>::iterator it;
struct node
{
int l,r,k;
}a[100005],sum[100005];
int cmp(node a,node b)
{
if(a.r==b.r)
return a.l<b.l;
else
return a.r<b.r;
}
int main()
{
int t;
cin>>t;
int n;
int cas=0;
while(t--)
{
S.clear();
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].k>>a[i].l>>a[i].r;
S.insert(a[i].k);
}
int tot=0;
for(int i=1;i<=n;i++)
{
S.erase(a[i].k);//因为必须同时用两把剑打,排除两把剑相同导致只剩一把剑的情况
it=S.lower_bound(a[i].l);//如果区间内没有set中的点
if(it==S.end()||(*it)>a[i].r)//就舍弃这条线段
{
sum[tot++]=a[i];
}
S.insert(a[i].k);
}
sort(sum,sum+tot,cmp);
int tmp;
if(tot==0) tmp=0;
else tmp=1;//第一条线段
int ans=S.size()+tmp;
int rr=sum[0].r;
for(int i=1;i<tot;i++)
{
if(rr<sum[i].l)
{
ans++;//无重合部分,多用一把剑
rr=sum[i].r;
}
}
printf("Case #%d: %d\n",++cas,ans);
}
return 0;
}