POJ2182 Lost Cows 树状数组

一群牛,编号为1到n,但是编号乱了,已知每只牛的前面有多少只编号比其小,求出牛的编号。

插点问段。

这道题要从后面反推回来,比如最后的一只牛,知道有a只编号比它小,则它的编号为a+1。

update  更新已经确定的编号,

sum(i)   查询已经确定的编号中(后面的牛的编号都确定了),有多少个比i小。

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<cstring>
 4 const int MAXN=8000+5;
 5 int c[MAXN];
 6 int a[MAXN];
 7 int ans[MAXN];
 8 int tot;
 9 int lowbit(int x)
10 {
11     return x&(-x);
12 }
13 void update(int x,int num,int n)
14 {
15     while(x<=n){
16         c[x]+=num;
17         x+=lowbit(x);
18     }
19 }
20 int sum(int x)
21 {
22     int temp=0;
23     while(x>0){
24         temp+=c[x];
25         x-=lowbit(x);
26     }
27     return temp;
28 }
29 int main()
30 {
31     int n;
32     while(scanf("%d",&n)!=EOF){
33         memset(c,0,sizeof(c));
34         memset(ans,0,sizeof(ans));
35         tot=n;
36         a[0]=a[1]=0;
37         for(int i=2;i<=n;i++)
38             scanf("%d",&a[i]);
39         for(int i=n;i>0;i--){
40             for(int j=a[i]+1;j<=n;j++){
41                 if(1+sum(j)+a[i]==j){
42                     ans[tot]=j;
43                     break;
44                 }
45             }
46             update(ans[tot],1,n);
47             tot--;
48         }
49         for(int i=1;i<=n;i++){
50             printf("%d\n",ans[i]);
51         }
52     }
53     return 0;
54 }

poj2182

时间: 2024-11-03 11:44:50

POJ2182 Lost Cows 树状数组的相关文章

POJ2182 Lost Cows 树状数组,二分

题意:给n个奶头身高从1到n,现在奶牛排成一列,给n-1个数字,为第二号奶牛到第n号奶牛前面比它矮的奶牛个数. 求这个序列每个奶牛的身高. 思路:从最后一个奶牛开始判断,因为最后一个奶牛是和前面所有奶牛做比较的,我们可以根据比它矮的奶牛个数确定它的身高,如果前面有3个比它矮那么它身高绝对为4,而继续判断前一个奶头身高时,要把这个奶牛剔除考虑, 所以我们想到可以维护一个长度为n的01序列,为1表示奶牛还在,奶牛从后往前遍历,设比它矮的有k个,每次找奶牛身高,就是找存在的奶牛中k+1的位置,也就是前

POJ 2481 Cows(树状数组)

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. Farmer John has N cows (we number the cows from 1 to N). Ea

POJ2481 Cows 树状数组的简单应用

题意给了你N头牛,每头牛的强壮值是一个区间[s,e],如果第 i 头牛比第 j 头牛强壮那么必须满足 Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj: 为了满足这三个条件我们进行排序,先以e降序排为先决条件,若e相等则让s升序排列,如此即可满足三个条件,这道题目任意两头牛的强壮值区间有可能完全一样,这样就不需要重新用树状数组求一次和了,直接赋值即可,这样可以省很多时间,因为已经排序处理了,所以即便区间相等的  肯定就是相邻的,所以直接扫一遍即可,若

Cows(树状数组)

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 19673   Accepted: 6699 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in hi

POJ2481:Cows(树状数组)

Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good. Farmer John has N cows (we number the cows from 1 to N). Ea

POJ 2182 Lost Cows (树状数组)

Lost Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9660   Accepted: 6219 Description N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'waterin

poj 2481 Cows 树状数组解法,详细解析。

Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 13445   Accepted: 4448 Description Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in hi

soj 1088. Cows(树状数组)

可以用树状数组解决. 先按左端点递增排序,左端点相等的按右端点降序排列. 然后从左往有扫,更新答案同时更新sum数组. 对于一只Cow i,ans[i]为f(i)-g(i). f(i)为满足p[j].s<=p[i].s&&p[j].e>=p[i].e(0<=j<n,j!=i)的Cow只数. g(i)为满足p[j].s==p[i].s&&p[j].e==p[i].e(0<=j<n,j!=i)的Cow只数. 开个d数组维护一下,d[i]即为g

POJ 2481 COWS(树状数组)

题目大意: 说给你n个线段的,告诉你每个线段的起始点S_i,和终止点E_i, 问这n条线段里有多少线段是相互包含的,如果两个端点重合不算包含. 解题思路: 用树状数组搞就可以了,这道题是star的变形题,star是让我们求出有多个星星在这个星星的左下角,而这道题是让我们求出有多少个星星在这个星星的左上角. 这样分析,对于(S_i,E_i)我们把他封装成为一个点的坐标,那么(S_j,E_j)同样也可以被封装成为一个点的坐标,而题目中说的S_i<S_j&&E_j<E_i&&