XidianOJ 1099 A simple problem
生活随笔
收集整理的這篇文章主要介紹了
XidianOJ 1099 A simple problem
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目描述
一個長度為N的數組A, 所有數都是整數 ,0 <= A[i] <= 1000000,1 <= i <= N,1 <= N <= 100000,對于 任意i,j ,1 <= ?i ?<= ?j ?<= N,[i, j]中所有數為原數組的一個子區間, 現在要求子區間的和小于等于K的子區間有多少個, 0 <= ?K ?<= 10000。
雖然xry111很SB,但還是在O(N)的時間復雜度內就做出了這題,你呢?
? ? ? ? ? ? ??
?
?
輸入
第一行整數T 代表數據組數,1 <= T ?<= ?12
每組數據第一行 整數 N, K。
接下來一行N個整數, ?由空格隔開。
?
輸出
輸出子區間的和小于等于K的子區間的個數。 每組輸出占一行。
--正文
明明是簡單的題,卻做了半天。。。
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;typedef long long LL; long long a[100001]; int n,k,i,j; int main(){int time,T; scanf("%d",&T);for (time=1;time<=T;time++){int now = 1;LL sum = 0,res = 0;scanf("%d %d",&n,&k);for (i=1;i<=n;i++){scanf("%lld",&a[i]);sum += a[i];if (sum <= k) {res += i - now + 1;}else {while (sum > k){sum = sum - a[now]; now ++;} res += i - now + 1;} }printf("%lld\n",res);}return 0; }?
轉載于:https://www.cnblogs.com/ToTOrz/p/6169490.html
總結
以上是生活随笔為你收集整理的XidianOJ 1099 A simple problem的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 斐波那契数列c++代码_轮到你了,斐波那
- 下一篇: 计算机维护系统Win8PE,U盘启动计算