Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 1815 | Accepted: 822 |
Description
FJ's N (1 ≤ N ≤ 10,000) cows conveniently indexed 1..N are standing in a line. Each cow has a positive integer height (which is a bit of secret). You are told only the height H (1 ≤ H ≤ 1,000,000) of the tallest cow along with the index I of that cow.
FJ has made a list of R (0 ≤ R ≤ 10,000) lines of the form "cow 17 sees cow 34". This means that cow 34 is at least as tall as cow 17, and that every cow between 17 and 34 has a height that is strictly smaller than that of cow 17.
For each cow from 1..N, determine its maximum possible height, such that all of the information given is still correct. It is guaranteed that it is possible to satisfy all the constraints.
Input
Lines 2.. R+1: Two distinct space-separated integers A and B (1 ≤ A, B ≤ N), indicating that cow A can see cow B.
Output
Sample Input
9 3 5 5 1 3 5 3 4 3 3 7 9 8
Sample Output
5 4 5 3 4 4 5 5 5
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
const int len=10111;
const int lim=1000001;
const int inf=999999999;
int m,n,num,tall,see;
int a,b,c,x,y;
struct self{int x,y;short int w;}s[lim];
int first[lim],next[lim];
int d[len];
queue<int>q;
bool inq[len];
void makeside(int x,int y,int w)
{
n++;
s[n].x=x;s[n].y=y;s[n].w=w;
next[n]=first[x];first[x]=n;
}
void spfa()
{
q.push(0);
for(a=1;a<=m;a++)d[a]=inf;
inq[0]=1;
while(!q.empty())
{
int u=q.front();q.pop();
inq[u]=0;
for(int e=first[u];e!=-1;e=next[e])
if(d[s[e].y]>d[u]+s[e].w)
{
d[s[e].y]=d[u]+s[e].w;
if(!inq[s[e].y])
{
q.push(s[e].y);
inq[s[e].y]=1;
}
}
}
}
void print()
{
int a,t=tall-d[num];
for(a=1;a<=m;a++)cout<<d[a]+t<<'\n';
}
int main()
{
freopen("tallest.in","r",stdin);
freopen("tallest.out","w",stdout);
memset(first,-1,sizeof(first));
memset(next,-1,sizeof(next));
scanf("%d%d%d%d",&m,&num,&tall,&see);
for(a=1;a<=m;a++)makeside(0,a,0);
for(a=1;a<=see;a++)
{
scanf("%d%d",&x,&y);
makeside(y,x,0);
if(x>y)swap(x,y);
for(b=x+1;b<y;b++)
{
makeside(x,b,-1);
makeside(y,b,-1);
}
}
spfa();
print();
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
using namespace std;
set<int>se;
int m,num,tall,see;
int a,b,c;
int x,y;
int s[11111];
bool ok(int l,int r)
{
int add=l*10001+r;
if(se.insert(add).second)return true;
return false;
}
void dec(int l,int r)
{
for(int a=l;a<=r;a++)s[a]--;
}
void print()
{
int a;
for(a=1;a<=m;a++)cout<<(s[a]-s[num])+tall<<'\n';
}
int main()
{
freopen("tallest.in","r",stdin);
freopen("tallest.out","w",stdout);
scanf("%d%d%d%d",&m,&num,&tall,&see);
for(a=1;a<=see;a++)
{
scanf("%d%d",&x,&y);
if(x>y)swap(x,y);
if(ok(x,y))
dec(x+1,y-1);
}
print();
return 0;
}
var
i,j,k,m,n,anx,max,x,y,z,h,t,s,hm,r,tmp:longint;
a,b,c,f,d,aa:array[0..500000]of longint;
q:array[-10..3000000]of longint;
v:array[-10..100000]of boolean;
procedure qsort(l,r:longint);
var i,j,mid,p:longint;
begin
i:=l;j:=r;mid:=a[(l+r)shr 1];
repeat
while(a[i]<mid)do inc(i);
while(a[j]>mid)do dec(j);
if(i<=j)then
begin
p:=a[i];a[i]:=a[j];a[j]:=p;
p:=b[i];b[i]:=b[j];b[j]:=p;
p:=c[i];c[i]:=c[j];c[j]:=p;
inc(i);
dec(j);
end;
until i>j;
if(i<r)then qsort(i,r);
if(l<j)then qsort(l,j);
end;
begin
assign(input,'tallest.in');assign(output,'tallest.out');reset(input);rewrite(output);
readln(n,k,hm,r);
for i:=1 to r do
begin
readln(x,y);
inc(m);
a[m]:=y;b[m]:=x;c[m]:=0;
if x<y then
for j:=x+1 to y-1 do
begin
inc(m);a[m]:=x;b[m]:=j;c[m]:=-1;
end;
if(x>y)then
for j:=y+1 to x-1 do
begin
inc(m);a[m]:=x;b[m]:=j;c[m]:=-1;
end;
end;
qsort(1,m);
f[0]:=1;j:=1;
for i:=1 to n do
begin
while(a[j]=i)do inc(j);
f[i]:=j;
end;
h:=0;t:=0;
fillchar(v,sizeof(v),true);
fillchar(d,sizeof(d),0);
for i:=1 to n do
begin
inc(t);
q[t]:=i;
end;
while not(h=t)do
begin
inc(h);
x:=q[h];
v[x]:=false;
if(h>3000000)
then h:=1;
for i:=f[x-1] to f[x]-1 do
begin
if d[x]+c[i]<d[b[i]]then
begin
d[b[i]]:=c[i]+d[x];
if not v[b[i]] then
begin
v[b[i]]:=true;
inc(t);
if(t>3000000)then t:=1;
q[t]:=b[i];
end;
end;
end;
end;
for i:=1 to n do aa[i]:=hm+d[i];
for i:=1 to n do writeln(aa[i]);
close(input);
close(output);
end.