题目大意:给出一个正整数M,给出N个正整数ai,让你在这些数中挑出一些数组成M的一个划分,如果符合条件的划分数超过两个,输出:-1,如果没有输出:0,如果有且仅有一个:则按顺序输出剩下的数的序号。

例如:

input output
270
4
100
110
170
200
2 4
270
4
100
110
160
170
-1
270
4
100
120
160
180
0

测例1中,有且仅有100+170=270,所以输出110与200的序号2 4。测例2中,100+170=160+110=270,所以输出-1。测例3中,没有任何两个数的和为270,所以输出0。

Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

数据规模:0<ai<=1000,2<=N<=100。

理论基础:无。

题目分析:我们用dp[i][j]表示仅用前i个数可以挑出的j的划分的个数。pre[i][j]表示第i个数被选择或者未被选择,用于最后输出结果。

我们可以得出如果需要输出时,那么方案数就是唯一的,所以途径的所有的状态的数都只能是被选或者未被选择,所以我们不用担心,a[i]被选与未被选都有解的情况。因为这时我们不需要输出。

下来是状态转移方程:dp[i][j]=dp[i-1][j]+(j>=a[i])*(dp[i-1][j-a[i]]);在每一层dp时,只要发现dp[i][M]大于等于2即可输出-1退出程序。

如果dp[N][M]为0,则输出0。否则,回溯找回答案并输出。

代码如下:

#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 0
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define N 100
#define M 100000
int dp[N+1][M+1];
int a[N+1],sum,n,cnt,ans[N+1];
bool pre[N+1][M+1];
int main()
{
scanf("%d%d",&sum,&n);
for(int i=1;i<=n;i++)scanf("%d",a+i);
dp[1][0]=1,dp[1][a[1]]=1,pre[1][a[1]]=true;
for(int i=2;i<=n;i++)
{
for(int j=0;j<=sum;j++)
{
dp[i][j]=dp[i-1][j]+(j>=a[i])*dp[i-1][j-a[i]];
if(dp[i-1][j-a[i]])pre[i][j]=true;
}
if(dp[i][sum]>=2)
{
printf("-1\n");
return 0;
}
}
if(dp[n][sum]==0)
{
printf("0\n");
return 0;
}
if(dp[n][sum]==1)
{
for(int i=n;i>=1;i--)
{
if(!pre[i][sum])ans[++cnt]=i;
sum=sum-a[i]*pre[i][sum];
}
pra(ans,1,cnt)
for(int i=cnt;i>=1;i--)
{
printf("%d%c",ans[i],i==1?'\n':' ');
}
}
return 0;
}

其中的初始化细节处理很容易理解,所以不用再细说了。

by:Jsun_moon http://blog.csdn.net/jsun_moon

URAL 1244的更多相关文章

  1. DP URAL 1244 Gentlemen

    题目传送门 /* 题意:已知丢失若干卡片后剩余的总体积,并知道原来所有卡片的各自的体积,问丢失的卡片的id DP递推:首先从丢失的卡片的总体积考虑,dp[i] 代表体积为i的方案数,从dp[0] = ...

  2. 递推DP URAL 1244 Gentlemen

    题目传送门 /* 题意:给出少了若干卡片后的总和,和原来所有卡片,问少了哪几张 DP:转化为少了的总和是否能有若干张卡片相加得到,dp[j+a[i]] += dp[j]; 记录一次路径,当第一次更新的 ...

  3. ural 1244. Gentlemen

    1244. Gentlemen Time limit: 0.5 secondMemory limit: 64 MB Let's remember one old joke: Once a gentle ...

  4. URAL 1244. Gentlemen(DP)

    题目链接 这题不难啊...标记一下就行了.表示啥想法也没有. #include <cstring> #include <cstdio> #include <string& ...

  5. URAL 1244. Gentlemen (DP)

    题目链接 题意 : 给出一幅不完全的纸牌.算出哪些牌丢失了. 思路 : 算是背包一个吧.if f[j]>0  f[j+a[i]] += f[j];然后在记录一下路径. #include < ...

  6. URAL DP第一发

    列表: URAL 1225 Flags URAL 1009 K-based Numbers URAL 1119 Metro URAL 1146 Maximum Sum URAL 1203 Scient ...

  7. 【51Nod 1244】莫比乌斯函数之和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...

  8. 51nod 1244 莫比乌斯函数之和

    题目链接:51nod 1244 莫比乌斯函数之和 题解参考syh学长的博客:http://www.cnblogs.com/AOQNRMGYXLMV/p/4932537.html %%% 关于这一类求积 ...

  9. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

随机推荐

  1. [转] thrift的使用介绍

    http://gemantic.iteye.com/blog/1199214 一.About  thrift   二.什么是thrift,怎么工作? 三.Thrift  IDL 四.Thrift   ...

  2. linux lvm的操作手册_pvcreate_vgcreate_lvcreate_相关

    一. 前言 每个Linux使用者在安装Linux时都会遇到这样的困境:在为系统分区时,如何精确评估和分配各个硬盘分区的容量,因为系统管理员不但要考虑到当前某 个分区需要的容量,还要预见该分区以后可能需 ...

  3. 安装android studio 出现的路径问题 tools.jar' seems to be not in Android Studio classpath

    尝试一下android studio  ,谁知出现路径问题 'tools.jar' seems to be not in Android Studio classpath. Please ensure ...

  4. ios 中如何应对UIScrollView快速滑动(暴力用户,暴力测试)

    1.实现UIScrollViewDelegate 开始滑动: - (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView 滑动 ...

  5. DiskLruCache 硬盘缓存 使用简介

    简介 LruCache只是管理了内存中图片的存储与释放,如果图片从内存中被移除的话,那么又需要从网络上重新加载一次图片,这显然非常耗时.对此,Google又提供了一套硬盘缓存的解决方案:DiskLru ...

  6. C#中的序列化与反序列化

    眼看XX鸟的课程关于C#的知识点就要学完了,翻看网络中流传的教程还是发现了一个课程中没有讲到的知识点:序列化与反序列化 无奈还是了解一下并操作一番,以备后用吧 介绍:就是将对象信息转化为二进制信息以便 ...

  7. (转)C#读取文件路径

    //获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssembly ( ) ...

  8. Asp.net笔记(1)

    1.下拉框,列表,下拉列表 下拉框其实是HTML的知识,在这里就是在复习一下: <select id="select1" runat="server"&g ...

  9. WCF理论 【转载】

    原文地址:http://blog.itpub.net/23109131/viewspace-661613/ WCF是什么? WCF是"Windows Communication Founda ...

  10. PHP Math

    PHP Math 简介 Math 函数能处理 integer 和 float 范围内的值. 安装 PHP Math 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. PHP 5 Math 函 ...