URAL 1005 Stone Pile

2024-08-21 08:18
文章标签 ural 1005 stone pile

本文主要是介绍URAL 1005 Stone Pile,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


   这种题型: Balanced Partition

   刚开始用的方法:

   从最大的开始放,每次放到数量较小的那一堆上,最后看相差为多少。

   这样很容易想,可惜是错的,反例如下。

   5
 10 9 8 7 2

  如果这么算,分的是 (10,7) 和 (9,8,2)  相差2    而最小的答案是(10,8)和(9,7,2)  相差0  



  看了这道题的讨论,基本就两种解法,暴力或DP;

  我是用的DP;

  用bool   dp [ i ] [ j ] 表示只用 i 之前的石头,能否使部分和为 j 。石头重量为 a[ i ] 

 递推式:dp [ i ] [ j ] =dp [ i - 1 ] [ j ]  ||  dp [ i - 1 ] [ j - a[ i ] ]    

 i 的范围:0~n-1      j 的范围: 0~S  (S为总和)      

最后搜索一遍可以达到的分组的和,各自求分成的两堆的差,然后求最小值就行了。

代码如下:

int a[20];int N;
bool dp[20][2000000];
int A(int a){return a>0?a:-a;
}
int main(void)
{ cin>>N;memset(dp,0,sizeof(dp));int S=0;For(i,N) scanf("%d",&a[i]),S+=a[i];dp[0][0]=1;dp[0][a[0]]=1;for(int i=1;i<N;i++){for(int j=0;j<S;j++){dp[i][j]=dp[i-1][j];if(j-a[i]>=0) dp[i][j]=dp[i][j]||dp[i-1][j-a[i]];}}int ANS=inf;for(int i=0;i<S;i++){if(dp[N-1][i]){//若存在一个组合的Sum为i //则这么分组的差为:  S-2i; if(A(S-2*i)<ANS) ANS=A(S-2*i);}}cout<<ANS<<endl;
return 0;
}



这篇关于URAL 1005 Stone Pile的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092618

相关文章

ural 1297. Palindrome dp

1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unli

ural 1149. Sinus Dances dfs

1149. Sinus Dances Time limit: 1.0 second Memory limit: 64 MB Let  An = sin(1–sin(2+sin(3–sin(4+…sin( n))…) Let  Sn = (…( A 1+ n) A 2+ n–1) A 3+…+2) An+1 For given  N print  SN Input One

ural 1820. Ural Steaks 贪心

1820. Ural Steaks Time limit: 0.5 second Memory limit: 64 MB After the personal contest, happy but hungry programmers dropped into the restaurant “Ural Steaks” and ordered  n specialty steaks

ural 1017. Staircases DP

1017. Staircases Time limit: 1.0 second Memory limit: 64 MB One curious child has a set of  N little bricks (5 ≤  N ≤ 500). From these bricks he builds different staircases. Staircase consist

ural 1026. Questions and Answers 查询

1026. Questions and Answers Time limit: 2.0 second Memory limit: 64 MB Background The database of the Pentagon contains a top-secret information. We don’t know what the information is — you

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

【SGU】271. Book Pile(双端队列模拟)

一摞书,2个操作,一个操作是在书堆上加一本,第二个将前K个书翻转 看别人用Splay树做的,但是可以用双端队列模拟,因为K个书之后的书位置已经定下来了,所以只需要记录在队列头加书还是尾加书 #include<cstdio>#include<string>#include<algorithm>#include<queue>#include<stack>#include<cstrin

后缀数组 - 求最长回文子串 + 模板题 --- ural 1297

1297. Palindrome Time Limit: 1.0 second Memory Limit: 16 MB The “U.S. Robots” HQ has just received a rather alarming anonymous letter. It states that the agent from the competing «Robots Unlim

【URAL】1057 Amount of Degrees 数位DP

传送门:【URAL】1057 Amount of Degrees 题目分析:将数转化成能达到的最大的01串,串上从右往左第i位为1表示该数包括B^i。 代码如下: #include <cstdio>#include <cstring>#include <algorithm>using namespace std ;typedef long long LL ;#de

HDU 1005(矩阵乘法)

A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). 题意:给出一个递推公式,求第N项。 题解:开始试图找出一个能够直接算的递