Color the ball(HDU-1556)
生活随笔
收集整理的這篇文章主要介紹了
Color the ball(HDU-1556)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Problem Description
N個氣球排成一排,從左到右依次編號為1,2,3....N.每次給定2個整數a b(a <= b),lele便為騎上他的“小飛鴿"牌電動車從氣球a開始到氣球b依次給每個氣球涂一次顏色。但是N次以后lele已經忘記了第I個氣球已經涂過幾次顏色了,你能幫他算出每個氣球被涂過幾次顏色嗎?
Input
每個測試實例第一行為一個整數N,(N <= 100000).接下來的N行,每行包括2個整數a b(1 <= a <= b <= N)。
當N = 0,輸入結束。
Output
每個測試實例輸出一行,包括N個整數,第I個數代表第I個氣球總共被涂色的次數。
Sample Input
3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1
思路:差分數組
直接記錄各次操作,并求差分數組,然后對差分數組進行對應修改,即加 1,最后直接求各項的值即可
Source Program
#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #define PI acos(-1.0) #define E 1e-9 #define INF 0x3f3f3f3f #define LL long long const int MOD=1000000007; const int N=100000+5; const int dx[]= {-1,1,0,0}; const int dy[]= {0,0,-1,1}; using namespace std;int n; int f[N]; int a[N]; int main(){while(scanf("%d",&n)!=EOF&&n){memset(f,0,sizeof(f));for(int i=1;i<=n;i++){//對差分數組修改int l,r;scanf("%d%d",&l,&r);f[l]+=1;f[r+1]-=1;}for(int i=1;i<=n;i++)//單點查詢每個值a[i]=a[i-1]+f[i];for(int i=1;i<=n;i++)printf("%d ",a[i]);printf("\n");}return 0; }總結
以上是生活随笔為你收集整理的Color the ball(HDU-1556)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 和为 k 的组合(51Nod-1268)
- 下一篇: 线性代数 —— 矩阵快速幂