CF-12D - Ball(排序+MAP)
来源:互联网 发布:vb怎样弹出对话框 编辑:程序博客网 时间:2024/06/05 13:26
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
N ladies attend the ball in the King's palace. Every lady can be described with three values: beauty, intellect and richness. King's Master of Ceremonies knows that ladies are very special creatures. If some lady understands that there is other lady at the ball which is more beautiful, smarter and more rich, she can jump out of the window. He knows values of all ladies and wants to find out how many probable self-murderers will be on the ball. Lets denote beauty of thei-th lady by Bi, her intellect byIi and her richness byRi. Theni-th lady is a probable self-murderer if there is somej-th lady that Bi < Bj, Ii < Ij, Ri < Rj. Find the number of probable self-murderers.
Input
The first line contains one integer N (1 ≤ N ≤ 500000). The second line containsN integer numbersBi, separated by single spaces. The third and the fourth lines contain sequencesIi andRi in the same format. It is guaranteed that0 ≤ Bi, Ii, Ri ≤ 109.
Output
Output the answer to the problem.
Sample Input
31 4 24 3 22 5 3
1
思路:l[i]代表第i个人的信息。把l按三个关键字排序,假设三个关键字分别为x,y,z。x,z从小到大,y从大到小。
因为已经先按x从小到大排了。 y从尾开始执行插入操作。
所以每次只需往集合里找到第一个y大于要插入的值比较z看看要插入的是否自杀,若会,则自杀人数加1.并不做插入操作。
若不会自杀,则插入。并且去掉y,z都比l[i]小的集合中不会自杀的人
因为接下来会因这些人而自杀的人肯定会因为l[i]而自杀
y执行到0结果也就出来了。
代码有注释,好理解。
#include<iostream>#include<algorithm>#include<map>using namespace std;const int mm=501000;const int oo=1e9+2;class node{public: int x,y,z;}l[mm];int n;bool cmp(node a,node b)///x从小到大,y从大到小,z小到大{ return a.x!=b.x? a.x<b.x:a.y!=b.y?a.y>b.y:a.z<b.z;}map<int,int>mp;map<int,int >::iterator it;int main(){ while(cin>>n) { for(int i=0;i<n;i++)cin>>l[i].x; for(int i=0;i<n;i++)cin>>l[i].y; for(int i=0;i<n;i++)cin>>l[i].z; sort(l,l+n,cmp); mp[-oo]=oo;mp[oo]=-oo;///标记左右边界 //cout<<mp[-oo]<<" "<<mp[oo]; int ans=0; ///x是从小到大排 for(int i=n-1;i>=0;i--) { it=mp.upper_bound(l[i].y);///找到比l[i].y大的第一个数 if(it->second>l[i].z)ans++;///找到让l[i]自杀的人了 else if(mp[l[i].y]<l[i].z) { mp[l[i].y]=l[i].z;///更新值,插入不会自杀的人 ///去掉y,z都比l[i]小的集合中不会自杀的人 ///因为接下来会因这些人而自杀的人肯定会因为l[i]而自杀 for(it=--mp.lower_bound(l[i].y);it->second<l[i].z;) mp.erase(it--); } } cout<<ans<<"\n"; }}
- CF-12D - Ball(排序+MAP)
- CF 12D. Ball
- CF #12 D Ball
- D. Ball CF
- CF 12D BALL 线段树 && 树状数组
- CF 15D Map
- Codeforces 12D. Ball(线段树&多维排序)
- CodeForces 12D Ball 多级排序 + 离散 + 线段树
- Codeforces 12D Ball 树状数组模拟3个元素的排序
- 树状数组模拟3个元素的排序 Codeforces 12D Ball
- Codeforces 12D Ball (树状数组)
- Codeforces 12D Ball (线段树)
- CF 274D Lovely Matrix(拓扑排序)
- cf 274D Lovely Matrix 拓扑排序
- CF 632D(Longest Subsequence-计数排序)
- CF 274D Lovely Matrix(拓扑排序)
- 排序+链表 CF——752D
- CF 277.5 B. BerSU Ball
- C# Socket编程笔记
- Ubuntu默认启动到字符界面
- wpf Canvas 限定范围拖动 鼠标滚轴改变大小
- bash参考手册之三(基本的Shell特性)
- poj-1751-Highways-(最小生成树)
- CF-12D - Ball(排序+MAP)
- C语言中不安全的函数以及解决方案
- ajax能访问struts action中特定的某个方法
- Javascript 模块化编程
- dex2jar 使用
- [Spring3.x源码]Acegi(一)验证器
- GIT初学的学习笔记
- spring mail中附件名乱码问题
- C应用程序增加调试便利的方法,程序运行过程中输出运行的源文件名称 函数名称和当前行号