日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tallest Cow POJ - 3263 差分 前缀和

發布時間:2024/1/18 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 差分 前缀和的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。