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

UVALive 6911 Double Swords 树状数组的更多相关文章

  1. UVALive 2191 Potentiometers (树状数组)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  2. UVALive - 4329 Ping pong 树状数组

    这题不是一眼题,值得做. 思路: 假设第个选手作为裁判,定义表示在裁判左边的中的能力值小于他的人数,表示裁判右边的中的能力值小于他的人数,那么可以组织场比赛. 那么现在考虑如何求得和数组.根据的定义知 ...

  3. UVALive 6911 Double Swords (Set,贪心,求区间交集)

    补:华中VJ这个题目很多标程都不能AC了,包括我下面原本AC了的代码,再交就WA掉了,感觉是样例有问题呢-- 首先左边的是必须要选的,然后右边的需要注意,有些区间是可以舍掉的.1.区间里有两个不同的A ...

  4. UVALive 6911---Double Swords(贪心+树状数组(或集合))

    题目链接 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. UVALive 4329 Ping pong(树状数组)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=13895 题意:一条街上住有n个乒乓选手,每个人都有一个技能值,现在 ...

  6. UvaLive 6667 Longest Chain (分治求三元组LIS&amp;树状数组)

    题目链接: here 题意: 和hdu4742类似.差别就是一部分三元组是直接给出的.另一部分是用他给的那个函数生成的.还有就是这里的大于是严格的大于a>b必须ax>bx,ay>by ...

  7. UVALive 4329 树状数组第二题

    大白书上的题目,比较巧妙的是其分析,为了求某个i点做裁判的时候的情况数,只要知道左边有多少比它小的记为ansc,右边有多少比它小的记为ansd,则总种数,必定为 ansc*(右边总数-ansd)+an ...

  8. UVA 1513 Movie collection (树状数组+反向存储)

    题意:给你n盘歌碟按照(1....n)从上到下放,接着m个询问,每一次拿出x碟,输出x上方有多少碟并将此碟放到开头 直接想其实就是一线段的区间更新,单点求值,但是根据题意我们可以这样想 首先我们倒着存 ...

  9. HDU 4605 Magic Ball Game (dfs+离线树状数组)

    题意:给你一颗有根树,它的孩子要么只有两个,要么没有,且每个点都有一个权值w. 接着给你一个权值为x的球,它从更节点开始向下掉,有三种情况 x=w[now]:停在此点 x<w[now]:当有孩子 ...

随机推荐

  1. dede调用多级导航的方法

    <div id="navWrapper"> <div class="content"> <ul class="nav m ...

  2. vue-cli构建项目使用 less

    在vue-cli中构建的项目是可以使用less的,但是查看package.json可以发现,并没有less相关的插件,所以我们需要自行安装. 第一步:安装 npm install less less- ...

  3. 关于aspx.designer.cs

    .aspx文件..aspx.cs文件和.aspx.designer.cs的一些说明 .aspx文件:(页面)书写页面代码.存储的是页面design代码.只是放各个控件的代码,处理代码一般放在.cs文件 ...

  4. CSS的力量:用一个DIV画图

    这些图片都是用一个DIV绘制出来的,其实原理并不复杂. 这些图片都是由CSS绘制出来的,通过background-image叠加实现, 如蘑菇头的实现,通过 radial-gradient 径向渐变  ...

  5. iOS手势UIGestureRecognizer的使用及手势冲突的解决办法【转】

    转自:iOS开发中的手势体系——UIGestureRecognizer分析及其子类的使用 关于手势的一篇很好的帖子,转载过来免得丢失.你可能最感兴趣的是手势间的互斥处理,那么就搜索 4.手势间的互斥处 ...

  6. MySQL用户密码过期登陆报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

    今天接到主从复制失败告警,查看MySQL,发现MySQL能够登陆但是执行命令报错, ERROR 1820 (HY000): You must reset your password using ALT ...

  7. 001_fpm打包命令详解

    使用fpm来制作rpm包 2017/2/22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  8. Windows下安装Python及Eclipse中配置PyDev插件

    最近开始接触Python,鉴于之前安装Java的教训,决定这次边安装Python,边写下历程,供日后反复使用. 在Python官网http://www.python.org/下载Python版本,鉴于 ...

  9. Smarty 模板引擎简介

    前言 Smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一.它分离了逻辑代码和外在的内容,提供了一种易于管理和使用的方法,用来将原本与HTML代码混杂在一起PHP代码逻辑 ...

  10. 【转】使用TCP协议连续传输大量数据时,是否会丢包,应如何避免?

    使用TCP协议连续传输大量数据时,是否会丢包,应如何避免? 比如发送文件.记得有人提过可能会发生什么堆栈溢出.怎样避免呢?是不是可以收到数据后发送确认包,收到确认包后再继续发送.或是发送方发送了一些数 ...