AcWing 692. G巴士计数 差分+前缀和
生活随笔
收集整理的這篇文章主要介紹了
AcWing 692. G巴士计数 差分+前缀和
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目描述
https://www.acwing.com/problem/content/description/694/
經(jīng)典的差分再前綴和題目
不斷給一個(gè)范圍內(nèi)的所有數(shù)加上同一個(gè)定值,最后再求具體的某處的值。
差分 前綴和 秒殺
思路
見我之前的算法思路:https://blog.csdn.net/weixin_45798993/article/details/122495960
代碼
#include<iostream> #include<cstring> using namespace std; const int N=5010; int city[N]; int t,n,a,b,p,c; int cnt=1; int main() {scanf("%d",&t);while(t--){memset(city,0,sizeof(city)); //清空數(shù)組內(nèi)容scanf("%d",&n);for(int i=0;i<n;i++){scanf("%d%d",&a,&b);city[a]++; //差分操作city[b+1]--;}for(int i=1;i<=5000;i++) city[i]+=city[i-1]; //求前綴和//注意上限不是n,而是5000,而是范圍所能達(dá)到的上限scanf("%d",&p);printf("Case #%d: ",cnt++);for(int i=0;i<p;i++){scanf("%d",&c);printf("%d ",city[c]);}printf("\n");}return 0; }總結(jié)
以上是生活随笔為你收集整理的AcWing 692. G巴士计数 差分+前缀和的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ppt制作教程与原理介绍(学习记录)
- 下一篇: win10计算机怎么连接网络,如何创建宽