NOJ [1288] Eating Sweets
来源:互联网 发布:百度派 知乎 编辑:程序博客网 时间:2024/06/05 20:02
链接地址:http://ac.nbutoj.com/Problem/view.xhtml?id=1288
本题其实是一个大水题,但是需要一些优化。
首先,如果你用双重循环来计数,时间复杂度是O(n ^ 2),那么一定会超时的。
所以,我们可以仔细想一下,对于输入的a,b,c和d,那么循环的次数一定为a - max(c, d) + 1(想一想为什么)。
然后对于每次,增加的数量就是min(b - d + 1, i - d)(想一想为什么)。
那么时间复杂度就变成O(n)了。
以下是出题的代码。
本题其实是一个大水题,但是需要一些优化。
首先,如果你用双重循环来计数,时间复杂度是O(n ^ 2),那么一定会超时的。
所以,我们可以仔细想一下,对于输入的a,b,c和d,那么循环的次数一定为a - max(c, d) + 1(想一想为什么)。
然后对于每次,增加的数量就是min(b - d + 1, i - d)(想一想为什么)。
那么时间复杂度就变成O(n)了。
以下是出题的代码。
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a, b, c, d;
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
while (~scanf("%d%d%d%d", &a, &b, &c, &d))
{
int e = b - d + 1;
int s = 0;
for (int i = max(c, d); i <= a; i++)
{
s += min(e, i - d);
}
printf("%d\n", s);
}
return 0;
}
0 0
- NOJ [1288] Eating Sweets
- NOJ [1201] Chihuo II - Eating in China
- NOJ [1356] Hey! Where Is My Sweets(╯▔皿▔)╯ 一个数拆分成最多m个数的不重复的方法数
- Noj
- three bags of sweets
- SPOJ 16607. Sweets
- NYOJ--1105--sweets
- codeforces63E Sweets Game DP,dfs,博弈
- I want eating
- healthy eating habit
- hdu4302 Holedox Eating
- hdu 4302 Holedox Eating
- HDU 4302 holedox eating
- HDU 4302 - Holedox Eating
- HDU 4302 Holedox Eating
- Bad Eating Habits
- poj 3670 Eating Together
- Hdu 4302 Holedox Eating
- NOJ [1271] Nth Day is Which Day
- NOJ [1274] The battle of Red Cliff
- NOJ [1267] Flying Chess
- NOJ [1273] So Long the String
- NOJ [1275] Play or Not Play
- NOJ [1288] Eating Sweets
- NOJ [1285] The TT Candy
- NOJ [1280] Go for a Psychological Test
- NOJ [1296] Between The Island
- NOJ [1297] Code Of Keyboard
- gtest学习
- NOJ [1301] Gopher Hole
- 黑马程序员——单列集合 笔记第十篇
- NOJ [1300] Factor