BZOJ1635: [Usaco2007 Jan]Tallest Cow 最高的牛

1635: [Usaco2007 Jan]Tallest Cow 最高的牛

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 346  Solved: 184
[Submit][Status]

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.

有n(1 <= n <= 10000)头牛从1到n线性排列,每头牛的高度为h[i](1 <= i <= n),现在告诉你这里面的牛的最大高度为maxH,而且有r组关系,每组关系输入两个数字,假设为a和b,表示第a头牛能看到第b头牛,能看到的条件是 a, b之间的其它牛的高度都严格小于min(h[a], h[b]),而h[b] >= h[a]

Input

* Line 1: Four space-separated integers: N, I, H and R

 * 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

* Lines 1..N: Line i contains the maximum possible height of cow i.

Sample Input

9 3 5 5
1 3
5 3
4 3
3 7
9 8


INPUT DETAILS:

There are 9 cows, and the 3rd is the tallest with height 5.

Sample Output

5
4
5
3
4
4
5
5
5

HINT

Source

Silver

题解:

想到了正解以为不严密,就没敢写。。。感觉不会再爱了T-T。。。

详细题解戳这 http://blog.sina.com.cn/s/blog_ab8386bc0101gx9j.html

代码:

 1 #include<cstdio>
 2 
 3 #include<cstdlib>
 4 
 5 #include<cmath>
 6 
 7 #include<cstring>
 8 
 9 #include<algorithm>
10 
11 #include<iostream>
12 
13 #include<vector>
14 
15 #include<map>
16 
17 #include<set>
18 
19 #include<queue>
20 
21 #include<string>
22 
23 #define inf 1000000000
24 
25 #define maxn 10000+500
26 
27 #define maxm 500+100
28 
29 #define eps 1e-10
30 
31 #define ll long long
32 
33 #define pa pair<int,int>
34 
35 #define for0(i,n) for(int i=0;i<=(n);i++)
36 
37 #define for1(i,n) for(int i=1;i<=(n);i++)
38 
39 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
40 
41 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
42 
43 #define mod 1000000007
44 
45 using namespace std;
46 
47 inline int read()
48 
49 {
50 
51     int x=0,f=1;char ch=getchar();
52 
53     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
54 
55     while(ch>='0'&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
56 
57     return x*f;
58 
59 }
60 int b[maxn],n,ans,m;
61 struct rec{int l,r;}a[maxn];
62 inline bool cmp(rec a,rec b)
63 {
64     return a.l<b.l||(a.l==b.l&&a.r<b.r);
65 }
66 
67 int main()
68 
69 {
70 
71     freopen("input2.txt","r",stdin);
72 
73     freopen("output3.txt","w",stdout);
74 
75     n=read();ans=read();ans=read();m=read();
76     for1(i,m)
77      {
78          a[i].l=read();a[i].r=read();
79          if(a[i].l>a[i].r)swap(a[i].l,a[i].r);
80      }
81     sort(a+1,a+m+1,cmp);
82     for1(i,m)
83     {
84         if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r)continue;
85         b[a[i].l+1]--;b[a[i].r]++;
86     }
87     for1(i,n)
88     {
89          ans+=b[i];
90         printf("%d\n",ans);
91     }
92 
93     return 0;
94 
95 }
View Code

 

 

posted @ 2014-09-19 13:18  ZYF-ZYF  Views(145)  Comments(0Edit  收藏  举报