Sum
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 10494   Accepted: 6895

Description

Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S. The problem is to determine for a given sum S the minimum number N for which we can obtain S by associating
signs for all numbers between 1 to N. 



For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem. 

Input

The only line contains in the first line a positive integer S (0< S <= 100000) which represents the sum to be obtained.

Output

The output will contain the minimum number N for which the sum S can be obtained.

Sample Input

12

Sample Output

7

Hint

The sum 12 can be obtained from at least 7 terms in the following way: 12 = -1+2+3+4+5+6-7.

给一个数sum,问从1到n,各个数可以取正取负,想找到最小的n,从1到n加起来是sum。

这个题记一下在于数组的使用,做的时候发现开不了那么大的数组,于是就得利用当前的数只和前面一个数的状态有关,所以2个数组就好用了,&1这里决定要记一下。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int a[3][200005]; int main()
{
int i, j, x; while (cin >> x)
{
memset(a[0], 0, sizeof(a[0]));
memset(a[1], 0, sizeof(a[1]));
a[0][100000] = 1;
for (i = 1;; i++)
{
memset(a[i&1], 0, sizeof(a[i&1]));
for (j = 0; j <= 200000; j++)
{
if (a[(i - 1) & 1][j] == 1)
{
a[i & 1][j + i] = 1;
a[i & 1][j - i] = 1;
}
}
if (a[i & 1][100000 + x] == 1 || a[i & 1][100000 - x] == 1)
{
cout << i << endl;
break;
}
}
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1844:Sum ”滚动“数组的更多相关文章

  1. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  2. POJ 2063 Investment 滚动数组+完全背包

    题目链接: http://poj.org/problem?id=2063 题意: 你现在有现金m元,你要做n年的存款投资,给你k种投资方式,每种需要现金vi元,能获得xi元的理论,一年到期后你要利用拿 ...

  3. OpenJudge/Poj 1844 Sum

    1.链接地址: http://bailian.openjudge.cn/practice/1844 http://poj.org/problem?id=1844 2.题目: Sum Time Limi ...

  4. POJ 1844 Sum【简单数学】

    链接: http://poj.org/problem?id=1844 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=29256#probl ...

  5. POJ 1844 Sum

    题意:给一个整数n,求当n由1到k的连续整数加或减组成时的最小的k. 解法:一开始觉得dp……后来觉得复杂度太大了……GG……百度了一下是个数学题orz. 如果n全部由加法组成,那么k可以组成k(k+ ...

  6. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  7. 【算法系列学习】DP和滚动数组 [kuangbin带你飞]专题十二 基础DP1 A - Max Sum Plus Plus

    A - Max Sum Plus Plus https://vjudge.net/contest/68966#problem/A http://www.cnblogs.com/kuangbin/arc ...

  8. HDU1024 Max Sum Plus Plus —— DP + 滚动数组

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...

  9. HDU1024_Max Sum Plus Plus【滚动数组】

    Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

随机推荐

  1. 【题集】k倍区间(抽屉原理)

    例1:http://lx.lanqiao.cn/problem.page?gpid=T444 蓝桥杯 问题描述 给定一个长度为N的数列,A1, A2, ... AN,如果其中一段连续的子序列Ai, A ...

  2. 2019年第十届蓝桥杯【C++省赛B组】

    试题 A: 组队 本题总分:5 分 作为篮球队教练,你需要从以下名单中选出 1 号位至 5 号位各一名球员,组成球队的首发阵容.每位球员担任 1 号位至 5 号位时的评分如下表所示.请你计算首发阵容 ...

  3. Day6 - 牛客203E

    https://ac.nowcoder.com/acm/contest/203/E 埋坑不会做

  4. 八、Delphi10.3读取JSON文件,并修改JSON数组一条内容后保存到文件

    一.我们有一个JSON文件,如下: { "在野": [ { "城池": 0, "武将": 74, "登场年": 190 ...

  5. 7.8 Varnish Log

  6. JuJu团队11月28号工作汇报

    JuJu团队11月28号工作汇报 JuJu   Scrum 团队成员 今日工作 剩余任务 困难 于达 解决了数据接口的bug 生成generator形式, 并用熟悉Julia处理数据的方法 处理数据步 ...

  7. TBLASTN

    TBLASTN search translated nucleotide databases using a protein query

  8. Linux学习-预习第五六七章节关于用户权限管理以及磁盘文件系统

  9. POJ1088:滑雪

    滑雪 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 82112   Accepted: 30706 Description ...

  10. C语言小游戏: 2048.c

    概要:2048.c是一个C语言编写的2048游戏,本文将详细分析它的源码和实现.C语言是一种经典实用的编程语言,本身也不复杂,但是学会C语言和能够编写实用的程序还是有一道鸿沟的.本文试图通过一个例子展 ...