P2879 [USACO07JAN]区间统计Tallest Cow

我会带着你远行 2023-07-06 06:10 68阅读 0赞

题目描述

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.

输入格式

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.

输出格式

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

题意翻译

题目描述:

FarmerJohn 有n头牛,它们按顺序排成一列。 FarmerJohn 只知道其中最高的奶牛的序号及它的高度,其他奶牛的高度都是未知的。现在 FarmerJohn 手上有RR条信息,每条信息上有两头奶牛的序号(aa和bb),其中bb奶牛的高度一定大于等于aa奶牛的高度,且aa,bb之间的所有奶牛的高度都比aa小。现在FarmerJohnFarmerJohn想让你根据这些信息求出每一头奶牛的可能的最大的高度。(数据保证有解)

输入格式:

第1行:四个以空格分隔的整数:nn,ii,hh和RR(nn和RR意义见题面; ii 和 hh 表示第 ii 头牛的高度为 hh ,他是最高的奶牛)

接下来R行:两个不同的整数aa和bb(1 ≤ aa,bb ≤ n)

输出格式:

一共n行,表示每头奶牛的最大可能高度.

数据范围:

1 ≤ n ≤ 10000 ; 1 ≤ h ≤ 1000000 ; 0 ≤ R ≤ 10000)

Translate provided by @酥皮

输入输出样例

输入 #1复制

  1. 9 3 5 5
  2. 1 3
  3. 5 3
  4. 4 3
  5. 3 7
  6. 9 8

输出 #1复制

  1. 5
  2. 4
  3. 5
  4. 3
  5. 4
  6. 4
  7. 5
  8. 5
  9. 5

刚开始假设每头牛的身高都最高

牛序号: 1 2 3 4 5 6 7 8 9

初始身高:5 5 5 5 5 5 5 5 5

1,3说明第1头牛的身高<=第3头牛的身高,1-3之间的第2头牛身高需要减去1

本题需要注意特殊情况,即

  1. 9 3 5 7
  2. 1 3
  3. 1 3
  4. 1 3
  5. 5 3
  6. 4 3
  7. 3 7
  8. 9 8

此时1 3中的第2头牛只需要减一次就行。所以需要对序列进行排序。

  1. #include<iostream>
  2. #include<cmath>
  3. #include<algorithm>
  4. #define N 10005
  5. int a1[N];
  6. using namespace std;
  7. struct node{
  8. int a;
  9. int b;
  10. }c[N];
  11. bool cmp(node x, node y)//自定义排序规则,只需要对a的值从小到大排序
  12. {
  13. if(x.a <= y.a) return true;
  14. else return false;
  15. }
  16. int main()
  17. {
  18. int n,i,h,R;
  19. cin >> n >> i >> h >> R;
  20. for(int i = 1; i <= n; ++i) a1[i] = h;//初始情况假设n头牛的身高都最高
  21. for(int i = 1; i <= R; ++i)
  22. {
  23. cin >> c[i].a >> c[i].b;
  24. }
  25. sort(c+1,c+1+R,cmp);
  26. /*
  27. for(int i = 1; i <= R; ++i)
  28. {
  29. cout << c[i].a << " " << c[i].b << endl;
  30. }
  31. cout << endl;
  32. */
  33. for(int i = 1; i <= R; ++i)
  34. {
  35. if(c[i].a == c[i-1].a && c[i].b == c[i-1].b) continue;//如果和前一个区间相等,则不计算进去
  36. if(c[i].a <= c[i].b)//a~b范围内的值减少1
  37. {
  38. for(int j = c[i].a+1; j <= c[i].b-1; ++j)
  39. {
  40. a1[j]-=1;//本步骤可以使用差分进行优化
  41. }
  42. }
  43. else if(c[i].a > c[i].b)
  44. {
  45. for(int j = c[i].b+1; j <= c[i].a-1; ++j)
  46. {
  47. a1[j]-=1;
  48. }
  49. }
  50. }
  51. for(int i = 1; i <= n; ++i) cout << a1[i] << endl;
  52. return 0;
  53. }

使用map去重

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<map>
  4. #include<algorithm>
  5. #define N 10005
  6. using namespace std;
  7. map<int,int> m[N];
  8. int a1[N];
  9. int main()
  10. {
  11. int n,i1,h,R;
  12. cin >> n >> i1 >> h >> R;
  13. int a,b;
  14. for(int i = 1; i <= n; ++i) a1[i] = h;
  15. while(R--)
  16. {
  17. cin >> a >> b;
  18. if(a>=b) swap(a,b);
  19. if(m[a][b]) continue;
  20. else m[a][b] = 1;
  21. for(int i = a+1; i <= b-1; ++i)
  22. a1[i] -= 1;
  23. }
  24. for(int i = 1; i <= n; ++i) cout << a1[i] << endl;
  25. return 0;
  26. }

使用差分和前缀和的解法,使用map去重。复杂度降低。

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <map>
  4. using namespace std;
  5. const int maxn = 100001 ;
  6. map<int,int> mp[maxn] ;
  7. int n,m,id,h,x,y ;
  8. int f[maxn] ;
  9. int main()
  10. {
  11. scanf("%d%d%d%d",&n,&id,&h,&m ) ;
  12. for(int i=1;i<=m;i++)
  13. {
  14. scanf("%d%d",&x,&y) ;
  15. if(x>y) swap(x,y) ;
  16. if(mp[x][y]) continue ;
  17. else mp[x][y]=1 ;
  18. f[x+1]--; f[y]++;//差分
  19. }
  20. for(int i=1;i<=n;i++)
  21. {
  22. f[i] = f[i]+f[i-1] ;//前缀后得到原数组
  23. printf("%d\n",f[i]+h) ;
  24. }
  25. return 0;
  26. }

发表评论

表情:
评论列表 (有 0 条评论,68人围观)

还没有评论,来说两句吧...

相关阅读

    相关 差分数组+Tallest Cow

    差分数组的定义和用途   1.定义:对于已知有n个元素的数列d,建立记录它每一项和前一项差值的差分数组f,f\[1\]=d\[1\]-0=d\[1\]。可以得到f\[i\