博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Monkeying Around Gym - 101350F
阅读量:4114 次
发布时间:2019-05-25

本文共 2926 字,大约阅读时间需要 9 分钟。

题意:

When the monkey professor leaves his class for a short time, all the monkeys go bananas. N monkeys are lined up sitting side by side on their chairs. They each have the same joke book. Before the professor returns, M jokes were heard.

Each of the M jokes are said in the order given and have the following properties:

xi - position of the monkey who said it.

li – index of the joke in the book.

ki – volume the monkey says that joke.

When the monkey at position xi says the joke li, all monkeys at a distance less than or equal to ki from that monkey (including the monkey who said the joke) will fall off their chairs in laughter if they have never heard the joke li before.

If the joke li has been heard anytime during the past before, and the monkey hears it again, then he will sit back up in his chair.

A monkey can fall off his chair more than once (every time he hears a new joke), and if he is already on the ground and hears a new joke, he will stay on the ground.

Can you figure out how many monkeys will be in their seats by the time the professor comes back?

思路:这个题有两种做法,其中一种使用线段树的做法,用两棵线段树,第一棵用于记录最后听到的笑话编号,第二棵用于记录某一个位置听到某一笑话几次。

第二种是一种很巧妙的方法,主要是思路,先对所有的笑话进行处理,在能听到这个笑话的两个端点进行处理,然后在对每一个猴子进行处理,用set保存当前能被听到的笑话个数,如果在当前这个位置的猴子是听到这个笑话的左端点,就把这个笑话放入set中,如果在当前这个位置恰好是听不到这个笑话的位置就把这个笑话进行删除,如果当前set中没有任何笑话,那么这个猴子一定是坐在凳子上的,如果说这个猴子最后的那个笑话听过多次,那么这个猴子也是坐在凳子上的,其他情况猴子在地上。

#include 
using namespace std;const int maxn=1e5+50;struct Node{ int x,y,idx,num;} e[maxn];int num[maxn];struct Nodes{ int idx,num; bool operator<(const Nodes &n1) const { if(n1.idx==idx) return num
sd;vector
v[maxn];set
::iterator it;int main(){ int T; scanf("%d",&T); while(T--) { int n,m; scanf("%d%d",&n,&m); memset(num,0,sizeof(num)); sd.clear(); for(int i=0; i<=n; i++) v[i].clear(); for(int i=1; i<=m; i++) { int x,l,k; scanf("%d%d%d",&x,&l,&k); e[i].idx=i; e[i].num=l; e[i].y=min(n,x+k); e[i].x=max(1,x-k); v[e[i].y+1].push_back(-i); v[e[i].x].push_back(i); } int ans=0; for(int i=1; i<=n; i++) { for(int j=0; j
0) { num[e[v[i][j]].num]++; sd.insert((Nodes) { e[v[i][j]].idx,e[v[i][j]].num }); } else { num[e[-v[i][j]].num]--; sd.erase((Nodes) { e[-v[i][j]].idx,e[-v[i][j]].num }); } } if(sd.size()==0) ans++; else { it=sd.end(); it--; if(num[(*it).num]!=1) ans++; } } cout<
<

转载地址:http://kbgsi.baihongyu.com/

你可能感兴趣的文章
LeetCode第43题思悟——字符串相乘(multiply-strings)
查看>>
LeetCode第44题思悟——通配符匹配(wildcard-matching)
查看>>
LeetCode第45题思悟——跳跃游戏(jump-game-ii)
查看>>
LeetCode第46题思悟——全排列(permutations)
查看>>
LeetCode第47题思悟—— 全排列 II(permutations-ii)
查看>>
LeetCode第48题思悟——旋转图像(rotate-image)
查看>>
驱动力3.0,动力全开~
查看>>
记CSDN访问量10万+
查看>>
Linux下Oracle数据库账户被锁:the account is locked问题的解决
查看>>
记CSDN访问20万+
查看>>
Windows 环境下Webstorm 2020.3 版本在右下角找不到Git分支切换部件的一种解决方法
查看>>
Electron-Vue项目中遇到fs.rm is not a function问题的解决过程
查看>>
飞机换乘次数最少问题的两种解决方案
查看>>
有向无回路图的理解
查看>>
设计模式中英文汇总分类
查看>>
WPF实现蜘蛛纸牌游戏
查看>>
单例模式
查看>>
工厂方法模式
查看>>
模板方法模式
查看>>
数据结构之队列、栈
查看>>