7-2 How Many Ways to Buy a Piece of Land
作者:
Kue
,
2020-11-29 19:39:53
,
所有人可见
,
阅读 531
核心:判定买的地是连续的,需要用前缀和算法,由于数据量较大,需要剪枝
#include <iostream>
#include <cstring>
using namespace std;
const int N = 10010;
int p[N];
int main()
{
int m, sum;
cin >> m >> sum;
for(int i = 1; i <= m; i++)
{
cin >> p[i];
p[i] = p[i] + p[i - 1];
}
int cnt = 0;
for(int i = 0; i < m; i++)
{
for(int j = i + 1; j <= m; j++)
{
//printf("%d %d %d\n", p[j] - p[i], p[j], p[i]);
if (p[j] - p[i] <= sum) cnt++;
else break;
}
}
printf("%d", cnt);
}