首页 技术 正文
技术 2022年11月17日
0 收藏 909 点赞 4,618 浏览 3006 个字

Double Swords

题目连接:

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4923

Description

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

Hint

题意

有n条龙,你需要杀掉这n条龙

每一条龙需要至少两把剑来干掉他们,龙需要锋利值为a[i]的剑,和能力值在b[i],c[i]之间的一把剑。

问你最少需要多少剑,就可以把所有龙干掉了。

题解:

首先对于每个a[i]我都准备一把剑,然后把区间按照右端点排序,如果这个区间有一把剑,如果这把剑就是a[i]那把,那么你再插一把剑插到右端点。

如果没有剑,你就插一把到右端点,否则这条龙就不需要额外的剑了。

用树状数组维护一下,扫一遍就行了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int cas = 0;
int a[maxn],b[maxn],c[maxn];
int d[maxn];
int lowbit(int x){
return x&(-x);
}
void update(int x,int v){
for(int i=x;i<maxn;i+=lowbit(i))
d[i]+=v;
}
int get(int x){
int ans = 0;
for(int i=x;i;i-=lowbit(i))
ans+=d[i];
return ans;
}
void solve(){
memset(d,0,sizeof(d));
int n;scanf("%d",&n);
vector<pair<pair<int,int>,int> >V;
int ans = 0;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i],&b[i],&c[i]);
a[i]++,b[i]++,c[i]++;
if(get(a[i])-get(a[i]-1)==0)
update(a[i],1);
V.push_back(make_pair(make_pair(c[i],b[i]),a[i]));
}
sort(V.begin(),V.end());
for(int i=0;i<V.size();i++){
int tmp = get(V[i].first.first)-get(V[i].first.second-1);
if(tmp==0)
update(V[i].first.first,1);
else if(tmp==1&&V[i].first.first>=V[i].second&&V[i].first.second<=V[i].second)
update(V[i].first.first,1);
}
printf("Case #%d: %d\n",++cas,get(maxn-1));
}
int main(){
//freopen("1.txt","r",stdin);
int t;
scanf("%d",&t);
while(t--)solve();
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902