1244. Gentlemen

Time limit: 0.5 second
Memory limit: 64 MB
Let's remember one old joke:
Once a gentleman said to another gentleman:
— What if we play cards?
— You know, I haven't played cards for ten years…
— And I haven't played for fifteen years…
So, little by little, they decided to resurrect their youth. The first gentleman asked a servant to bring a pack of cards, and before starting playing out weighed in his hand the pack.
— It seems to me, one card is missing from the pack… — he said and gave the pack to the other gentleman.
— Yes, the nine of spades, — the man agreed.
An incomplete pack of cards is given. The program should determine which cards are missing.

Input

The first line contains a positive integer, which is the weight in milligrams of the given incomplete pack. The second line contains an integer N, 2 ≤ N ≤ 100 — the number of cards in the complete pack. In the next N lines there are integers from 1 to 1000, which are the weights of the cards in milligrams. It's guaranteed that the total weight of all cards in the complete pack is strictly greater than the weight of the incomplete pack.

Output

If there is no solution, then output the single number 0. If there are more than one solutions, then you should write −1. Finally, if it is possible to determine unambiguously which cards are missing in the incomplete pack as compared to the complete one, then output the numbers of the missing cards separated with a space in ascending order.

Samples

input output
270
4
100
110
170
200
2 4
270
4
100
110
160
170
-1
270
4
100
120
160
180
0
Problem Author: Alexander Petrov
Problem Source: Ural State University Personal Programming Contest, March 1, 2003
Difficulty: 284
 
题意:给出x 。然后n个ai,问哪几个ai能够组成x。若无解,输出0,不止一个解,输出-1,否则输出那几个数的编号
分析:背包。 路径。
 #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <ctime>
#include <iomanip>
using namespace std;
typedef long long LL;
typedef double DB;
#define For(i, s, t) for(int i = (s); i <= (t); i++)
#define Ford(i, s, t) for(int i = (s); i >= (t); i--)
#define Rep(i, t) for(int i = (0); i < (t); i++)
#define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
#define rep(i, x, t) for(int i = (x); i < (t); i++)
#define MIT (2147483647)
#define INF (1000000001)
#define MLL (1000000000000000001LL)
#define sz(x) ((int) (x).size())
#define clr(x, y) memset(x, y, sizeof(x))
#define puf push_front
#define pub push_back
#define pof pop_front
#define pob pop_back
#define ft first
#define sd second
#define mk make_pair
inline void SetIO(string Name)
{
string Input = Name+".in",
Output = Name+".out";
freopen(Input.c_str(), "r", stdin),
freopen(Output.c_str(), "w", stdout);
} inline int Getint()
{
int Ret = ;
char Ch = ' ';
bool Flag = ;
while(!(Ch >= '' && Ch <= ''))
{
if(Ch == '-') Flag ^= ;
Ch = getchar();
}
while(Ch >= '' && Ch <= '')
{
Ret = Ret * + Ch - '';
Ch = getchar();
}
return Flag ? -Ret : Ret;
} const int N = , M = ;
int n, m, Arr[N];
int Dp[M], G[M];
bool Visit[N];
vector<int> Ans; inline void Input()
{
scanf("%d", &m);
scanf("%d", &n);
For(i, , n) scanf("%d", Arr + i);
} inline void Search(int x)
{
if(!x) return;
Visit[G[x]] = ;
Search(x - Arr[G[x]]);
} inline void Solve()
{
Dp[] = ;
int Max = ;
For(i, , n)
{
Max += Arr[i];
if(Max > m) Max = m;
Ford(j, Max, Arr[i])
if(Dp[j - Arr[i]])
{
Dp[j] += Dp[j - Arr[i]];
if(!G[j]) G[j] = i;
}
} if(Dp[m] > ) puts("-1");
else if(!Dp[m]) puts("");
else
{
Search(m);
For(i, , n)
if(!Visit[i]) Ans.pub(i);
int Length = sz(Ans);
Rep(i, Length - ) printf("%d ", Ans[i]);
printf("%d\n", Ans.back());
}
} int main()
{
#ifndef ONLINE_JUDGE
SetIO("C");
#endif
Input();
Solve();
return ;
}
 

ural 1244. Gentlemen的更多相关文章

  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(DP)

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

  4. URAL 1244. Gentlemen (DP)

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

  5. URAL 1244

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

  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. java笔记--超级类Object多线程的应用+哲学家进餐算法内部类与多线程结合

    关于Object类中的线程方法: Object类是所有Java类的 父类,在该类中定义了三个与线程操作有关的方法,使得所有的Java类在创建之后就支持多线程 这三个方法是:notify(),notif ...

  2. CSS3实现二十多种基本图形

    CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出.直接用CSS3画出这些图形,要比贴图性能更好,体验更加,是一种非常好的网页美观方式. 这32种图形分别为圆形,椭圆形,三角形,倒三角形, ...

  3. Access数据库之偏移注入

    /*转载请注明出处:珍惜少年时*/ 偏移注入主要是针对知道表,但是不知道字段的. 这里我已经知道了表明是:sys_admin 可以使用: select exists(selct * from sys_ ...

  4. HDU 2895 编辑距离

    #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...

  5. LInux 安全测试

    [CVE-2013-2094]Linux PREF_EVENTS Local Root 2.6.37-3.8.10 x86_64 踩(0)http://zone.wooyun.org/content/ ...

  6. cocos2dx混合模式应用———制作新手引导高亮区域

    先看下效果 制造这个椭圆高亮区域所使用原图是 附上代码 bool HelloWorld::init() { ////////////////////////////// // 1. super ini ...

  7. Android Your content must have a ListView whose id attribute is 'android.R.id.list'错误的解决办法

    在Android开发中,ListView有着很重要的地位,使用的场合也非常的多 错误提示:Your content must have a ListView whose id attribute is ...

  8. HDU 1848 Fibonacci again and again (斐波那契博弈SG函数)

    Fibonacci again and again Time Limit: 1000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & ...

  9. 奇怪的梦境(codevs 2833)

    题目描述 Description Aiden陷入了一个奇怪的梦境:他被困在一个小房子中,墙上有很多按钮,还有一个屏幕,上面显示了一些信息.屏幕上说,要将所有按钮都按下才能出去,而又给出了一些信息,说明 ...

  10. 手机 无法转移到SD卡 Andriod 导出应用程序

    今天下载了一个应用Meridian,因为是直接从google商店里面装的,因此无法找到SDK,需要从新找到导出方式: 豌豆荚==> 软件和游戏管理==>输入Meridian搜素==> ...