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. vs2010编译C++ 友元函数

    // CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...

  2. 编程题目: 两个队列实现栈(Python)

    感觉两个队列实现栈 比 两个栈实现队列 麻烦 1.栈为空:当两个队列都为空的时候,栈为空 2.入栈操作:当队列2为空的时候,将元素入队到队列1:当队列1位空的时候,将元素入队到队列2: 如果队列1 和 ...

  3. tools.lombok

    @Slf4j @Data @Accessors @Builder

  4. 剑指offer 把字符串转化为整数

    题目描述 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串,包括数字字母符号,可以为空 输出描述: 如果是合法 ...

  5. [Luogu][P2458] [SDOI2006]保安站岗

    题目链接 看起来似乎跟最小点覆盖有点像.但区别在于: 最小点覆盖要求所有边在其中,而本题要求所有点在其中. 即:一个点不选时,它的儿子不一定需要全选. 画图理解: 对于这样一幅图,本题中可以这样选择: ...

  6. 018、Java中除法的是用,解决除法计算精度问题

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

  7. C# 控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  8. POJ1723 SOLDIERS 兄弟连

    SOLDIERS 有一个性质:在一个长为n的序列a中找一个数 \(a_k\) 使得 \(\sum\limits_{i=1}^n abs(a_i-a_k)\) 最小,则 \(a_k\) 是a的中位数. ...

  9. Golang的变量定义及使用案例

    Golang的变量定义及使用案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.变量的定义 package main import "fmt" func m ...

  10. Redis 详解 (七) AOF 持久化

    目录 1.AOF简介 2.AOF 配置 3.开启 AOF 4.AOF 文件恢复 5. AOF 重写 6.AOF的优缺点 上一篇文章我们介绍了Redis的RDB持久化,RDB 持久化存在一个缺点是一定时 ...