ural 1244. Gentlemen

來源:互聯網
上載者:User

標籤:

1244. GentlemenTime limit: 0.5 second
Memory limit: 64 MBLet‘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.InputThe 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.OutputIf 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
2704100110170200
2 4
2704100110160170
-1
2704100120160180
0
Problem Author: Alexander Petrov
Problem Source: Ural State University Personal Programming Contest, March 1, 2003Tags: dynamic programming  ()Difficulty: 284 題意:給出x 。然後n個ai,問哪幾個ai能夠組成x。若無解,輸出0,不止一個解,輸出-1,否則輸出那幾個數的編號分析:背包。 路徑。
  1 #include <cstdio>  2 #include <cstring>  3 #include <cstdlib>  4 #include <cmath>  5 #include <deque>  6 #include <vector>  7 #include <queue>  8 #include <iostream>  9 #include <algorithm> 10 #include <map> 11 #include <set> 12 #include <ctime> 13 #include <iomanip> 14 using namespace std; 15 typedef long long LL; 16 typedef double DB; 17 #define For(i, s, t) for(int i = (s); i <= (t); i++) 18 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 19 #define Rep(i, t) for(int i = (0); i < (t); i++) 20 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 21 #define rep(i, x, t) for(int i = (x); i < (t); i++) 22 #define MIT (2147483647) 23 #define INF (1000000001) 24 #define MLL (1000000000000000001LL) 25 #define sz(x) ((int) (x).size()) 26 #define clr(x, y) memset(x, y, sizeof(x)) 27 #define puf push_front 28 #define pub push_back 29 #define pof pop_front 30 #define pob pop_back 31 #define ft first 32 #define sd second 33 #define mk make_pair 34 inline void SetIO(string Name) 35 { 36     string Input = Name+".in", 37     Output = Name+".out"; 38     freopen(Input.c_str(), "r", stdin), 39     freopen(Output.c_str(), "w", stdout); 40 } 41  42 inline int Getint() 43 { 44     int Ret = 0; 45     char Ch = ‘ ‘; 46     bool Flag = 0; 47     while(!(Ch >= ‘0‘ && Ch <= ‘9‘)) 48     { 49         if(Ch == ‘-‘) Flag ^= 1; 50         Ch = getchar(); 51     } 52     while(Ch >= ‘0‘ && Ch <= ‘9‘) 53     { 54         Ret = Ret * 10 + Ch - ‘0‘; 55         Ch = getchar(); 56     } 57     return Flag ? -Ret : Ret; 58 } 59  60 const int N = 110, M = 100010; 61 int n, m, Arr[N]; 62 int Dp[M], G[M]; 63 bool Visit[N]; 64 vector<int> Ans; 65  66 inline void Input()  67 { 68     scanf("%d", &m); 69     scanf("%d", &n); 70     For(i, 1, n) scanf("%d", Arr + i); 71 } 72  73 inline void Search(int x) 74 { 75     if(!x) return; 76     Visit[G[x]] = 1; 77     Search(x - Arr[G[x]]); 78 } 79  80 inline void Solve() 81 { 82     Dp[0] = 1; 83     int Max = 0; 84     For(i, 1, n) 85     { 86         Max += Arr[i]; 87         if(Max > m) Max = m; 88         Ford(j, Max, Arr[i]) 89             if(Dp[j - Arr[i]]) 90             { 91                 Dp[j] += Dp[j - Arr[i]]; 92                 if(!G[j]) G[j] = i; 93             } 94     } 95      96     if(Dp[m] > 1) puts("-1"); 97     else if(!Dp[m]) puts("0"); 98     else 99     {100         Search(m);101         For(i, 1, n)102             if(!Visit[i]) Ans.pub(i);103         int Length = sz(Ans);104         Rep(i, Length - 1) printf("%d ", Ans[i]);105         printf("%d\n", Ans.back());106     }107 }108 109 int main()110 {111     #ifndef ONLINE_JUDGE 112     SetIO("C");113     #endif 114     Input();115     Solve();116     return 0;117 }
View Code

 

 

ural 1244. Gentlemen

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.