2014
02-14

# Lost Cows

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.

* 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.

* 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.

5
1
2
1
0

2
4
5
3
1

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 8005
#define M 16100
struct node
{
int x,y;
int min;
int flag;
}a[N*3];
int b[N],c[N];
int Min(int x,int y)
{
if(x<y)
return x;
else
return y;
}
void ChangeTree(int t,int k)
{
a[t].flag+=k;
a[t].min+=k;
return ;
}
void CreatTree(int t,int x,int y)
{
a[t].x=x;
a[t].y=y;
a[t].flag=0;
if(x==y)
{
a[t].min=b[x];
return ;
}
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
CreatTree(temp,x,mid);
CreatTree(temp+1,mid+1,y);
a[t].min=Min(a[temp].min,a[temp+1].min);
return ;
}
void InsertTree(int t,int x,int y,int k)
{
if(a[t].x==x&&a[t].y==y)
{
ChangeTree(t,k);
return ;
}
int temp=t*2;
int mid=(a[t].x+a[t].y)/2;
if(a[t].flag)
{
ChangeTree(temp,a[t].flag);
ChangeTree(temp+1,a[t].flag);
a[t].flag=0;
}
if(y<=mid)
InsertTree(temp,x,y,k);
else if(x>mid)
InsertTree(temp+1,x,y,k);
else
{
InsertTree(temp,x,mid,k);
InsertTree(temp+1,mid+1,y,k);
}
a[t].min=Min(a[temp].min,a[temp+1].min);
return ;
}
int FindTree(int t)
{
if(a[t].x==a[t].y)
{
if(a[t].min==0)
return a[t].x;
else
return 0;
}
int temp=t*2;
if(a[t].flag)
{
ChangeTree(temp,a[t].flag);
ChangeTree(temp+1,a[t].flag);
a[t].flag=0;
}
if(a[temp+1].min==0)
return FindTree(temp+1);
else
return FindTree(temp);
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int i,j;
b[1]=0;
for(i=2;i<=n;i++)
scanf("%d",&b[i]);
CreatTree(1,1,n);
j=1;
while(a[1].min==0)
{
int tt;
tt=FindTree(1);
if(!tt)
tt=1;
c[tt]=j++;
InsertTree(1,tt,tt,M);
InsertTree(1,tt,n,-1);
}
for(i=1;i<=n;i++)
printf("%d\n",c[i]);
}
return 0;
}

1. 秀肌肉，告诉那些蠢蠢欲动的国家，只要敢来，一定会付出代价，至于对你有什么意义，大概就是防止别国打到你家门口把你老母拉出来轮番干吧。

2. 第23行：
hash = -1是否应该改成hash[s ] = -1

因为是要把从字符串s的start位到当前位在hash中重置

修改提交后能accept，但是不修改居然也能accept

3. #include <stdio.h>
int main()
{
int n,p,t[100]={1};
for(int i=1;i<100;i++)
t =i;
while(scanf("%d",&n)&&n!=0){
if(n==1)
printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
else {
if(n%4) p=n/4+1;
else p=n/4;
int q=4*p;
printf("Printing order for %d pages:n",n);
for(int i=0;i<p;i++){
printf("Sheet %d, front: ",i+1);
if(q>n) {printf("Blank, %dn",t[2*i+1]);}
else {printf("%d, %dn",q,t[2*i+1]);}
q–;//打印表前
printf("Sheet %d, back : ",i+1);
if(q>n) {printf("%d, Blankn",t[2*i+2]);}
else {printf("%d, %dn",t[2*i+2],q);}
q–;//打印表后
}
}
}
return 0;
}