poj.3253 Tallest Cow
题目描述:
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.
输入样例:
9 3 5 5
1 3
5 3
4 3
3 7
9 8
输出样例:
5
4
5
3
4
4
5
5
5
题目大意:
•有1~N头牛,我只被告诉了最高那头牛的号码(索引)和身高,我做了R行的清单,里面记录着17号牛能看见34号牛,并且17号到34号牛的身高要严格低于17号牛。
•第一行输入N(牛的个数),I(最高牛的索引),H(最高牛的身高),R(R行清单) (1<=A,B<=N)
•第二行输入a头牛能看见b头牛的索引
思路:
° A头牛能看见B头牛,意味着 第 A+1 头牛 到B -1 头牛 的身高同时低于 A头牛和B头牛,可以利用差分D[]来减去第 A+1头牛到B-1头牛的相对高度。
°对这第[A+1]头牛到第[B-1]头牛进行身高的修改,就是精确地修改这区间元素的值,同时不修改区间外元素的值。可以利用差分来解决这一问题。
°最高那头牛的相对高度可以看成0,其他牛的相对身高是负数。相对身高再加上最高奶牛的身高==真实奶牛身高。
AC代码
点击查看代码
#include<iostream>
#include<map>
using namespace std;
int N,I,H,R; // N头牛 最高牛索引 最高高度 R行
const int val = 10100;
map<pair<int,int>,bool>flag;
int a[val],c[val],s[val];
int main(){
scanf("%d%d%d%d",&N,&I,&H,&R);
for(int i = 1;i<=R;i++){
int a,b;
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
//查询是否有a,b两个键
if(flag[make_pair(a,b)]) continue;
c[a+1]--,c[b]++; //a+1 ~ b-1之间的相对高度-1 -> A牛 B牛互相看见
//c[a+1] -- 会导致c[b]
flag[make_pair(a,b)] = 1; //make_pair 创建一个piar对象
}
for(int i =1;i<=N;i++){
s[i] = s[i-1] +c[i];//每头牛的相对高度
printf("%d\n",s[i]+H);
}
}

浙公网安备 33010602011771号