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. 014、Java中byte自动转型的操作

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  2. 010-PHP输出数组中第某个元素

    <?php $monthName = array(1 => "January", "February", "March",//初 ...

  3. Ubuntu 终端命令速查表

    1.man: shell命令的说明指南 该命令代表manual,提供一个给定命令的说明指南. 用法:man <shell command> 用例:man ls 上述命令请求命令‘ls’的说 ...

  4. 移动 web 开发问题和优化小结

    1.前言 到目前为止,互联网行业里,手机越来越智能化,移动端占有的比例越来越高,尤其实在电商,新闻,广告,游戏领域.用户要求越来越高,网站功能越来越好,效果越来越炫酷,这就要求我们产品质量越来越高,w ...

  5. delphi的dbgrid控件点击title排序

    procedure TfrmMain.DBGridEhTitleClick(Column: TColumnEh);var i : integer;begin for i:= 1 to DBGridEh ...

  6. pacificrack 控制面板登录不上的问题

    我今天又试了一下: https://master-stack01.pacificrack.com还是登不上(这个一键烦恼了我一个星期了,但是我今天百度出来了解决办法) 然后用这个就可以了  https ...

  7. Day 24:XML基础

    Q: 什么是XML,为什么学习XML? A:XML为可扩展标记语言. 标签由开发者自己制定的(要按照一定的语法定义)描述带关系的数据(作为软件的配置文件): 包含与被包含的关系 properties文 ...

  8. 使用Python绘制漫步图

    代码如下: import matplotlib.pyplot as plt from random import choice class RandomWalk(): def __init__(sel ...

  9. 32位CPU和64位CPU 区别

    操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的CPU设计. 64位操作系统针对的64位的CPU设计.操作系统只是硬件和应用软件中间的一个平台. 32位操作系统针对的32位的C ...

  10. TensorFlow--交互式使用--tf.InteractiveSession()

    用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行.IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSes ...