Cows

poj2481:http://poj.org/problem?id=2481

题意:有N头牛,每只牛有一个测试值[S,E],如果对于牛i和牛j来说,它们的测验值满足下面的条件则证明牛i比牛j强壮:Si
<=Sjand Ej <= Ei and Ei - Si > Ej -
Sj。现在已知每一头牛的测验值,要求输出每头牛有几头牛比其强壮

解题:先按s值排个序(由小到大),s值相同的按e排序(由大到小),然后按照排好的序列对e值进行求逆序数。注意处理s和e分别相同的情况。

 1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cstring>
5 #define lson u<<1
6 #define rson u<<1|1
7 using namespace std;
8 const int N=100002;
9 int ll[4*N],rr[4*N],sum[4*N];
10 int ans[N];
11 struct Node{
12 int x;
13 int y;
14 int id;//记录结果的顺序
15 bool operator<(const Node a)const{//排序
16 if(x<a.x)return true;
17 if(x==a.x&&y>a.y)return true;
18 return false;
19 }
20 }num[N];
21 void pushUP(int u){
22 sum[u]=sum[lson]+sum[rson];
23 }
24 void build(int l,int r,int u){
25 ll[u]=l;
26 rr[u]=r;
27 sum[u]=0;
28 if(l==r)
29 return ;
30 int mid=(l+r)/2;
31 build(l,mid,lson);
32 build(mid+1,r,rson);
33 }
34 void update(int pos ,int u){
35 if(ll[u]==rr[u]){
36 sum[u]++;//注意这里是++,因为可能有重复的值
37 return;
38 }
39 int mid=(ll[u]+rr[u])/2;
40 if(mid>=pos)update(pos,lson);
41 else
42 update(pos,rson);
43 pushUP(u);
44 }
45 int query(int l,int r,int u){
46 if(ll[u]==l&&rr[u]==r){
47 return sum[u];
48 }
49 int mid=(ll[u]+rr[u])/2;
50 if(mid>=r)return query(l,r,lson);
51 else if(mid<l)return query(l,r,rson);
52 else {
53 return query(l,mid,lson)+query(mid+1,r,rson);
54 }
55 }
56 int n,u,v;
57 int main(){
58 while(~scanf("%d",&n)&&n>0){
59 memset(ans,0,sizeof(ans));
60 for(int i=1;i<=n;i++){
61 scanf("%d%d",&u,&v);
62 num[i].x=u;
63 num[i].y=v;
64 num[i].id=i;
65 }
66 build(1,100000,1);//直接建100000的树,因为e的最大值就是100000
67 sort(num+1,num+n+1);//排序
68 for(int i=1;i<=n;i++){
69 int y=num[i].y;
70 if(i>=2&&num[i-1].x==num[i].x&&num[i-1].y==num[i].y)//对于重复值,不用计算,有前面的值即可得到
71 ans[num[i].id]=ans[num[i-1].id];
72 else
73 ans[num[i].id]=query(y,100000,1);//否则查询后面的的区间,注意要包括当前这个点,因为x值不同时,这个点是计数的
74 update(num[i].y,1);//更新
75 }
76 for(int i=1;i<n;i++)
77 printf("%d ",ans[i]);
78 printf("%d\n",ans[n]);
79 }
80 }

时间: 2024-08-04 20:17:26

Cows的相关文章

3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二

3381: [Usaco2004 Open]Cave Cows 2 洞穴里的牛之二 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 21  Solved: 18[Submit][Status][Discuss] Description 洞窟里有一道长长的通道.它由N(1≤N≤25000)段道尾相连构成,编号分别为1到N.每个通道有一个阈值,其范围在[1,10^9]依次通过i..j的通道,那奶牛的体重指数就不能超过i..j通道中阈值的最小值.贝茜有Q

POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛

http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33470   Accepted: 13634 Description Every cow's dream is to become the most popular cow in the herd. In a herd

[USACO08NOV]奶牛混合起来Mixed Up Cows

题目描述 Each of Farmer John's N (4 <= N <= 16) cows has a unique serial number S_i (1 <= S_i <= 25,000). The cows are so proud of it that each one now wears her number in a gangsta manner engraved in large letters on a gold plate hung around her

POJ 3348 Cows [凸包 面积]

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9022   Accepted: 3992 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

POJ 2186 Popular Cows(Targin缩点)

传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 12921 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <=

Poj2186Popular Cows

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31533   Accepted: 12817 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &

POJ2186 Popular Cows

Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popula

Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved: 238[Submit][Status][Discuss] Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一

Lettcode_299_Bulls and Cows

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/50768550 You are playing the following Bulls and Cows game with your friend: You write down a number and ask your friend to guess what the number is. Each time your friend makes a g

POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M &l