Tallest Cow POJ - 3263 差分 前缀和
Tallest Cow
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
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
Sample Output
5
4
5
3
4
4
5
5
5
有n頭牛,第i頭牛是最高的,高度為H,現在給出r對牛之間的關系,
牛1可以看到牛3,說明牛2一定小于牛1.牛3是<=牛1;
要求求出n頭牛的最大的高度。(當然給出的關系可能會有重復的,所以要去重)
普通-兩層循環
#include<iostream> #include<cstdio> #include<set> #include<string> #include<cstring> #include<sstream> #include<cmath> #include<algorithm> using namespace std; set<string> que; int c[10077]; int main() {int n,i,h,r;int a,b,mina,maxb;string s = "";scanf("%d%d%d%d", &n,&i,&h,&r);memset(c,0,sizeof(c));for(int i = 0; i < r; i++){scanf("%d%d", &a, &b);mina = min(a,b);maxb = max(a,b);stringstream ss;ss << mina;ss << maxb;ss >> s; if(que.count(s)) //通過set來判斷給出的關系不能有重復的{continue;}for(int j = mina + 1; j < maxb; j++)//將a和b之間的牛的高度都減少c[j]--;que.insert(s);}for(int i = 1; i <= n; i++){printf("%d\n", c[i]+h);}return 0; }?差分計算 前綴和
定義:即為數組的每一項存儲的是 數列的每一項與其前一項的差值。
設有一數列d,那么d的差分數組f中:f[1]=d[1];對于整數i∈[2,n],f[i]=d[i]-d[i-1];
性質:
(1):通過差分數組f來求數列d各項的值:
? ? ? ? ? ? d[2]? =? ?f[2]+f[1]? ? =? ?d[2]-d[1]+d[1]? =? d[2]? 即 d[i] 為f[i]的前綴和
應用:
(1):快速區間加減
要將區間[1,3]之間的值都+3
d[1] = f[1] + 3;
d[2] = f[1] + 3 + f[2];
d[3] = f[1] + 3 + f[2] + f[3];
d[4] =?f[1] + 3 + f[2] + f[3] + f[4] - 3;
可以發現使用差分數組,當f[1]+3后,后面數列元素在計算過程中都會加上3;最后一個受影響的差分數組中的元素為f[3],所以令f[3+1]-3,即可保證不會影響到3以后數列元素的計算。這樣我們不必對區間[1,3]內每一個數進行處理,只需處理兩個邊界的差分值即可;
那么這個題,使所有的牛初始高度都為0,那么差分數組f的初始也都為0,就可以直接對牛a+1高度-1,牛b的高度+1,最后求出d數列,輸出時每項+h即可
#include<iostream> #include<cstdio> #include<set> #include<string> #include<sstream> #include<cmath> #include<algorithm> using namespace std; set<string> que; int d[10077],f[10077]; int main() {int n,i,h,r;int a,b,mina,maxb;string s = "";scanf("%d%d%d%d", &n,&i,&h,&r);for(int i = 0; i < r; i++){scanf("%d%d", &a, &b);mina = min(a,b);maxb = max(a,b);stringstream ss;ss << mina;ss << maxb;ss >> s;if(que.count(s)){continue;} f[mina + 1]--;f[maxb]++;que.insert(s);}for(int i = 1; i <= n; i++){d[i] = d[i - 1] + f[i];printf("%d\n", d[i]+h);}return 0; }?
總結
以上是生活随笔為你收集整理的Tallest Cow POJ - 3263 差分 前缀和的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 目前 人们所说的个人计算机属于(),职称
- 下一篇: Java开发私教_笃学私教:7年Java