USACO-2017-FEB-金组 Why Did the Cow Cross the Road III
来源:互联网 发布:埋雷软件 编辑:程序博客网 时间:2024/06/05 05:32
题目描述
The layout of Farmer John’s farm is quite peculiar, with a large circular road running around the perimeter of the main field on which his cows graze during the day. Every morning, the cows cross this road on their way towards the field, and every evening they all cross again as they leave the field and return to the barn.
As we know, cows are creatures of habit, and they each cross the road the same way every day. Each cow crosses into the field at a different point from where she crosses out of the field, and all of these crossing points are distinct from each-other. Farmer John owns N cows, conveniently identified with the integer IDs 1…N, so there are precisely 2N crossing points around the road. Farmer John records these crossing points concisely by scanning around the circle clockwise, writing down the ID of the cow for each crossing point, ultimately forming a sequence with 2N numbers in which each number appears exactly twice. He does not record which crossing points are entry points and which are exit points.
Looking at his map of crossing points, Farmer John is curious how many times various pairs of cows might cross paths during the day. He calls a pair of cows (a,b) a “crossing” pair if cow a’s path from entry to exit must cross cow b’s path from entry to exit. Please help Farmer John count the total number of crossing pairs.
输入
The first line of input contains N (1≤N≤50,000), and the next 2N lines describe the cow IDs for the sequence of entry and exit points around the field.
输出
Please print the total number of crossing pairs.
样例输入
4
3
2
4
4
1
3
2
1
样例输出
3
题意
先给一个n,一个圆上2*n个点,依次给出每个点是哪头牛经过的,每头牛经过两个点,每头牛经过的点连成线,问线得交点的个数。(不存在两条以上的线交于一点的情况。)
思路
树状数组维护。
AC代码
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>using namespace std;typedef struct{ int l,r;} node;node a[100005];int n,t[100005];bool cmp (node j,node k){ if(j.l==k.l) return j.r<k.r; return j.l<k.l;}int lowbit (int x){ return x&-x;}void update (int x){ for( ; x<=2*n; x+=lowbit(x)) t[x]+=1;}int sum (int x){ int s=0; for( ; x;x-=lowbit(x)) s+=t[x]; return s;}int main (){#ifndef ONLINE_JUDGE freopen("1","r",stdin);#endif // ONLINE_JUDGE ios_base::sync_with_stdio(false); cin.tie(0); int x; cin>>n; for(int i=1; i<=n*2; ++i) { cin>>x; if(a[x].l==0) a[x].l=i; else a[x].r = i; } sort(a+1,a+1+n,cmp); x=0; for(int i=1;i<=n;++i) { update(a[i].r+1); x+=sum(a[i].r)-sum(a[i].l); } cout<<x<<endl; return 0;}/************************************************************** Language: C++ Result: 正确 Time:56 ms Memory:2864 kb****************************************************************/
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road III
- [Usaco2017 Feb]Why Did the Cow Cross the Road III
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road II
- 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4997: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组套Treap
- [BZOJ]4991: [Usaco2017 Feb]Why Did the Cow Cross the Road III CDQ分治+树状数组
- 4992: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4995: [Usaco2017 Feb]Why Did the Cow Cross the Road
- bzoj4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
- Bzoj4989 [Usaco2017 Feb]Why Did the Cow Cross the Road
- 求一个整数的二进制中1的个数
- ADS-B及雷达显示终端7.1
- 检查点
- /* compiled code */ ?
- 机器学习资料搜集
- USACO-2017-FEB-金组 Why Did the Cow Cross the Road III
- iptables&firewalld
- JavaScript实现[网易云音乐Web站登录窗口]拖拽功能
- Java之hashcode方法
- String,StringBuilder和StringBuidler的特点与用法
- python爬虫 爬取python高级教程到单独的txt文件
- ALIENTEK 的 ESP8266 WiFi Module 刷入固件后出现don’t use rtc mem data错误或者ESP8266开启串口不停出现乱码的解决方案
- 查找和排序
- 默认初始化规则