点击这里去寻找母牛的顺序
Lost Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 14186 | Accepted: 9043 |
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 'watering hole' and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
Regrettably, FJ does not have a way to sort them. Furthermore, he's not very good at observing problems. Instead of writing down each cow's brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Input
* Line 1: A single integer, N
* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
Output
* Lines 1..N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
Sample Input
5
1
2
1
0
Sample Output
2
4
5
3
1
每头牛都有自己的标号,他们的顺序乱了,但是知道他的前面有几头比自己标号小的牛 我们存在数组a[n]中
因为刚做完一道差分的Tallest Cows ,测试就碰到这个了,还以为是差分呢,,,事实并不是这样
咱们先看最后一头牛,标号最大,那么他的位置一定是a[n]+1,想想是为什么
这时候我们从后往前看,我们不看最后一头牛的位置,道理也是一样的
我们就可以开一个标记数组 book[n],一开始的时候置零,那么从后往前遍历a[i]的时候,第a[i]+1个 0 的位置就是当前这个 i 的位置,存起来,找到后我们将这个位置的标记改成 1 。遍历完就全部找到喽
这里还有一个类似的题目可以趁机来练练手,原理一模一样,我就不多说了
http://poj.org/problemlistBuy Tickets
这是暴力算法
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=8888;
int main()
{
int a[mm];
a[1]=0;
int n;
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",&a[i]);
int book[mm];
int res[mm];//
mem(book,0);
res[n]=a[n]+1;
book[a[n]+1]=1;
int cnt=0;
for(int i=n-1;i>=1;i--){
for(int j=1;j<=n;j++){
if(book[j]==0)
cnt++;
if(cnt==a[i]+1){
res[i]=j;
cnt=0;
book[j]=1;
break;
}
}
}
for(int i=1;i<=n;i++)
printf("%d\n",res[i]);
return 0;
}
其实这个道理明白之后,还可以用线段树给写出来,同理我们先将每个叶子结点置为1,同样是从后往前遍历,只不过这里的位置就要用线段树的 sum 来反应,同样是找到之后置零 仔细想想 原理是一样的
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
const int mm=8888;
int a[mm];
int res[mm];
struct node{
int l,r;
int sum;
}pp[mm<<2];
void build(int k,int l,int r){
pp[k].l=l;
pp[k].r=r;
if(l==r){
pp[k].sum=1;
return ;
}
int mid=(l+r)>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
pp[k].sum=pp[k<<1].sum+pp[k<<1|1].sum;
}
int get(int k,int pos){
pp[k].sum--;//一定在区间内
if(pp[k].l==pp[k].r)
return pp[k].l;
if(pos<=pp[k<<1].sum)
return get(k<<1,pos);
else
return get(k<<1|1,pos-pp[k<<1].sum);//注意右边的查询
}
int main()
{
int n;
a[1]=0;
scanf("%d",&n);
for(int i=2;i<=n;i++)
scanf("%d",&a[i]);
build(1,1,n);
for(int i=n;i>=1;i--)
res[i]=get(1,a[i]+1);
for(int i=1;i<=n;i++)
printf("%d\n",res[i]);
return 0;
}
还有就是可以用树状数组,但你需要确定第 a[i]+1 个 1 的位置,这就要用到二分去查找
其实树状数组就是线段树的简化,而线段树本身就含有二分的性质,所以二者是一个意思
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int mm=8888;
int n;
int a[mm],c[mm];
int res[mm];
int lbt(int x){
return x&-x;
}
void change(int pos,int k){
while(pos<=n){
c[pos]+=k;
pos+=lbt(pos);
}
}
int getsum(int pos){
int res=0;
while(pos>0){
res+=c[pos];
pos-=lbt(pos);
}
return res;
}
int find(int num){//二分查找位置 确定第a[i]+1个1的位置
int l=1,r=n;
while(l<=r){
int mid=(l+r)>>1;
int sum=getsum(mid);
if(sum>=num)
r=mid-1;
else
l=mid+1;
}
return l;
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
change(i,1);//初始化为1
a[1]=0;
for(int i=2;i<=n;i++)
scanf("%d",&a[i]);
for(int i=n;i>=1;i--){
int pos=find(a[i]+1);
change(pos,-1);
res[i]=pos;
}
for(int i=1;i<=n;i++)
printf("%d\n",res[i]);
return 0;
}