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. 【C】揭秘rand()函数;

    原文地址:http://www.cnblogs.com/ngnetboy/archive/2012/11/23/2784078.html 相信只要是程序猿都会知道rand()函数是用来取随机数的一个库 ...

  2. Metasploit学习笔记——环境配置

    <Metasploit渗透测试魔鬼训练营>书56页开始配置网络环境,一共五台机器,攻击机换成了自己更常用的kali,配置方法和back track相同. kali(攻击机) 10.10.1 ...

  3. Day7 - I - Semi-prime H-numbers POJ - 3292

    This problem is based on an exercise of David Hilbert, who pedagogically suggested that one study th ...

  4. Day4 - J - Rank of Tetris HDU - 1811

    自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...

  5. LabVIEW面向对象的ActorFramework(2)

    二.为什么要学习面向编程? 面向对象编程,如果将上文推荐的两本书读完后,基本上也就有了答案.从自我产品开发的经验中,理解为可以迅速解决中大型程序需求变化时,在不影响其他程序功能的情况下,能够实现新增功 ...

  6. Java开发程序员必须要学会的linux命令总结

    查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xml文 ...

  7. 003、Java的单行注释

    代码如下: package TIANPAN; public class TestDemo { public static void main(String args[]) { // JAVA的单行注释 ...

  8. Irecycleview 的初次使用简单介绍(irecycleview 下拉刷新上拉加载)

    导包 还得加一个maven地址自己也看一下作者git把有详细解释(自己也要导入recycleview的包) 我的例子下载地址  https://www.lanzous.com/i32yzaj impl ...

  9. MQTT 协议学习:007-Keep Alive 连接保活 与 对应报文(PINGREQ、PINGRESP)

    背景 keep alive 是 CONNECT 报文中可变头的一部分. 我们提到过 Broker 需要知道 Client 是否非正常地断开了和它的连接,以发送遗愿消息.实际上 Client 也需要能够 ...

  10. 小程序中的web-view与h5网页之间的交互

    官方文档:https://developers.weixin.qq.com/miniprogram/dev/component/web-view.html web-view 基础库 1.6.4 开始支 ...