题目链接

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

problem  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

题意:有n条恶龙,需要人同时持两把剑杀死,一把剑要求长为A,另一把剑长度在B~C之间,输入n条龙的A B C数据要求,求最少需要携带多少把剑?

思路:对于n组恶龙的数据,按照C的大小从左到右排序。先考虑数据A,即先把长度固定的剑需要的数量确定,有些A相同只计算一次,sum=0,sum+=unique(Ai)。然后考虑长度为区间(B~C)的剑,对于排好序的数据,循环处理,对于区间Bi~Ci 如果区间里没有剑或者有一把剑且长度和Ai相等,则添加一把剑长为Ci,sum++,这样可能在后面的区间中出现,使得剑的数量最少,否则不处理,表明这个区间中有剑,不需要加入剑。注意:在判断区间中剑的数量时,用树状数组很方便查询;

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
const int maxn=1e6+;
int c[maxn];
bool vis[maxn];
struct Node
{
int x,y;
int z;
}node[*];
bool cmp(const Node s1,const Node s2)
{
if(s1.y==s2.y) return s1.x<s2.x;
return s1.y<s2.y;
}
int Lowbit(int t)
{
return t&(t^(t-));
}
void add(int x)
{
while(x<maxn){
c[x]++;
x+=Lowbit(x);
}
}
int sum(int x)
{
int sum=;
while(x){
sum+=c[x];
x-=Lowbit(x);
}
return sum;
}
int main()
{
int T,Case=;
cin>>T;
while(T--)
{
memset(vis,,sizeof(vis));
memset(c,,sizeof(c));
int N;
scanf("%d",&N);
for(int i=;i<N;i++)
{
scanf("%d",&node[i].z);
if(!vis[node[i].z]){
add(node[i].z);
vis[node[i].z]=true;
}
scanf("%d%d",&node[i].x,&node[i].y);
}
sort(node,node+N,cmp);
for(int i=;i<N;i++)
{
int t=sum(node[i].y)-sum(node[i].x-);
if(t==&&node[i].z>=node[i].x&&node[i].z<=node[i].y)
add(node[i].y);
else if(!t) add(node[i].y);
}
printf("Case #%d: %d\n",Case++,sum(maxn-));
}
return ;
}

这题也可以用集合,其实和上面思路差不多,用集合判断区间中是否有剑;

在网上看到有人用set写,代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
struct Node
{
int x,y;
int z;
}node[];
bool cmp(const Node s1,const Node s2)
{
if(s1.y==s2.y) return s1.x<s2.x;
return s1.y<s2.y;
}
set<int>s;
set<int>::iterator it; int main()
{
int T,Case=;
cin>>T;
while(T--)
{
s.clear();
int N;
scanf("%d",&N);
for(int i=;i<N;i++)
{
scanf("%d",&node[i].z);
s.insert(node[i].z);
scanf("%d%d",&node[i].x,&node[i].y);
}
sort(node,node+N,cmp);
int sum=;
for(int i=;i<N;i++)
{
it=s.lower_bound(node[i].x);
if(it!=s.end()&&*it==node[i].z) it++;
if(it==s.end()||*it>node[i].y){
if(node[i].z==node[i].y)
s.insert(-node[i].y);
//sum++;///set有去重功能;
else s.insert(node[i].y);
}
}
printf("Case #%d: %d\n",Case++,s.size()+sum);
}
return ;
}

这样写确实AC了,但我感觉有BUG 我认真看了程序,想了一个数据:

1

2

4 2 4

4 4 6

正确答案是2

运行这个程序结果是3

但是用多重集合是可以避免这个BUG的

代码如下:

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <cstring>
#include <cmath>
#include <queue>
#include <set>
#include <bitset>
using namespace std;
const int maxn=1e6+;
bool vis[maxn];
struct Node
{
int x,y;
int z;
}node[];
bool cmp(const Node s1,const Node s2)
{
if(s1.y==s2.y) return s1.x<s2.x;
return s1.y<s2.y;
}
multiset<int>s;
multiset<int>::iterator it; int main()
{
int T,Case=;
cin>>T;
while(T--)
{
memset(vis,,sizeof(vis));
s.clear();
int N;
scanf("%d",&N);
for(int i=;i<N;i++)
{
scanf("%d",&node[i].z);
if(!vis[node[i].z]){
s.insert(node[i].z);
vis[node[i].z]=true;
}
scanf("%d%d",&node[i].x,&node[i].y);
}
sort(node,node+N,cmp);
for(int i=;i<N;i++)
{
it=s.lower_bound(node[i].x);
if(it!=s.end()&&*it==node[i].z) it++;
if(it==s.end()||*it>node[i].y){
s.insert(node[i].y);
}
}
printf("Case #%d: %d\n",Case++,s.size());
}
return ;
}
/*
345
2
4 2 4
4 4 6
2
4 2 4
4 3 4
*/

UVALive 6911---Double Swords(贪心+树状数组(或集合))的更多相关文章

  1. 【bzoj4240】有趣的家庭菜园 贪心+树状数组

    题目描述 对家庭菜园有兴趣的JOI君每年在自家的田地中种植一种叫做IOI草的植物.JOI君的田地沿东西方向被划分为N个区域,由西到东标号为1~N.IOI草一共有N株,每个区域种植着一株.在第i个区域种 ...

  2. UVALive 6911 Double Swords 树状数组

    Double Swords 题目连接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8 ...

  3. 贪心+树状数组维护一下 Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) D

    http://codeforces.com/contest/724/problem/D 题目大意:给你一个串,从串中挑选字符,挑选是有条件的,按照这个条件所挑选出来的字符集合sort一定是最后选择当中 ...

  4. D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

    题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个 ...

  5. [BZOJ4240]有趣的家庭菜园(贪心+树状数组)

    最后数列一定是单峰的,问题就是最小化最后的位置序列的逆序对数. 从大到小加数,每次贪心看放左边和右边哪个产生的逆序对数更少,树状数组即可. 由于大数放哪对小数不产生影响,所以正确性显然. 注意相同数之 ...

  6. [P4064][JXOI2017]加法(贪心+树状数组+堆)

    题目描述 可怜有一个长度为 n 的正整数序列 A,但是她觉得 A 中的数字太小了,这让她很不开心. 于是她选择了 m 个区间 [li, ri] 和两个正整数 a, k.她打算从这 m 个区间里选出恰好 ...

  7. [luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)

    传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include < ...

  8. codeforces 1249 D2 Too Many Segments (hard version) 贪心+树状数组

    题意 给定n个线段,线段可以相交,第\(i\)个线段覆盖的区间为\([l_i,r_i]\),问最少删除多少个线段让覆盖每个点的线段数量小于等于k. 分析 从左往右扫每个点\(x\),若覆盖点\(x\) ...

  9. [JZO6401]:Time(贪心+树状数组)

    题目描述 小$A$现在有一个长度为$n$的序列$\{x_i\}$,但是小$A$认为这个序列不够优美. 小$A$认为一个序列是优美的,当且仅当存在$k\in [1,n]$,满足:$$x_1\leqsla ...

随机推荐

  1. 我所理解的RESTful Web API [设计篇]

    <我所理解的RESTful Web API [Web标准篇]>Web服务已经成为了异质系统之间的互联与集成的主要手段,在过去一段不短的时间里,Web服务几乎清一水地采用SOAP来构建.构建 ...

  2. Azure PowerShell (9) 使用PowerShell导出订阅下所有的Azure VM的Public IP和Private IP

    <Windows Azure Platform 系列文章目录> 笔者在之前的工作中,有客户提出想一次性查看Azure订阅下的所有Azure VM的Public IP和Private IP. ...

  3. 《Entity Framework 6 Recipes》中文翻译系列 (39) ------ 第七章 使用对象服务之配置模型和使用单复数服务

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 7-3  配置模型 问题 你想了解配置模型中的各种选项. 解决方案 当你添加一个AD ...

  4. Android开发学习之路-Android Studio开发小技巧

    上一次发过了一个介绍Studio的,这里再发一个补充下. 我们都知道,Android Studio的功能是非常强大的,也是很智能的.如果有人告诉你学Android开发要用命令行,你可以告诉他Andro ...

  5. Atitit 图像处理知识点体系知识图谱 路线图attilax总结 v4 qcb.xlsx

    Atitit 图像处理知识点体系知识图谱 路线图attilax总结 v4 qcb.xlsx 分类 图像处理知识点体系 v2 qb24.xlsx 分类 分类 理论知识 图像金字塔 常用底层操作 卷积扫描 ...

  6. Jass 技能模型定义(—):半人马酋长的反击光环

      Jass是什么?       先阐释一下什么是jass吧,百度:JASS(正确地说是JASS 2)是魔兽3的程序语言,用于控制游戏和地图的进行,也是魔兽游戏和地图的基础. 地图编辑器中摆放的单位( ...

  7. ThinkPHP3.2设置404跳转页面

    在ThinkPHP3.2版本中当我们访问不存在的页面时会出现非常不友好错误提示页面,类如下图: 解决办法: 1.在ThinkPHP3.2详细的介绍了该框架下的ThinkPHP惯例配置文件convent ...

  8. SpringMVC那点事

    一.SpringMVC返回json数据的三种方式 1.第一种方式是spring2时代的产物,也就是每个json视图controller配置一个Jsoniew. 如:<bean id=" ...

  9. GoldenGate碎碎念

    1. 在启动mgr进程的过程中报如下错误 GGSCI (node1.being.com) > start mgr Cannot - No such file or directory Canno ...

  10. geotrellis使用(四)geotrellis数据处理部分细节

    前面写了几篇博客介绍了Geotrellis的简单使用,具体链接在文后,今天我主要介绍一下Geotrellis在数据处理的过程中需要注意的细节,或者一些简单的经验技巧以供参考. 一.直接操作本地Geot ...