ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1244. Gentlemen

WA on #8
Posted by Sarvagya Agarwal 23 Jan 2017 01:04
/*
ye mera template hai
apna khud likho bc :P
*/

/*
Author : Sarvagya Agarwal
*/

#include<bits/stdc++.h>
using namespace std;

//defines
#define openin freopen("input.txt","r",stdin)
#define openout freopen("output.txt","w",stdout)
#define fast ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ll long long
#define int long long
#define mod 1000000007
#define repr(i,x,y) for (__typeof(x) i=x;i>=y;i--)
#define rep(i,x,y) for (__typeof(x) i=x;i<=y;i++)
#define all(c) (c).begin(),(c).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair

/* Print pair */
template <typename T,typename S>
ostream & operator << (ostream &os , const pair<T,S> &v) {
    os << "(" ;
    os << v.first << "," << v.second << ")" ;
    return os ;
}
/* Print vector */
template <typename T>
ostream & operator << (ostream &os , const vector<T> &v) {
    os << "[" ;
    int sz = v.size() ;
    for(int i = 0 ; i < sz ; ++i) {
        os << v[i] ;
        if(i!=sz-1)os << "," ;
    }
    os << "]\n" ;
    return os ;
}
/* Print set */
template <typename T>
ostream & operator << (ostream &os , const set<T> &v) {
    T last = *v.rbegin() ;
    os << "[" ;
    for(auto it : v) {
        os << it  ;
        if(it != last) os << "," ;
    }
    os << "]\n" ;
    return os ;
}
/* Print Map */
template <typename T,typename S>
ostream & operator << (ostream &os , const map<T,S> &v) {
    for(auto it : v) {
        os << it.first << " : " << it.second << "\n" ;
    }
    return os ;
}
int power(int a , int b)
{
    int res = 1 ;
    while(b)
    {
        if(b%2) {
            res = (res * a) % mod ;
        }
        b/=2 ;
        a = (a*a) % mod ;
    }
    return res ;
}

//debug
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
        cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif
int sum , n , arr[105] ;
int dp[105][1005] ;
int ans[105] ;
int solve(int index,int weight)
{
    if(weight == 0) {
        // found a configuration
        return 1 ;
    }
    if(index > n) {
        return 0 ;
    }
    int &res = dp[index][weight] ;
    if(res != -1) return res ;
    res = 0 ;
    res += solve(index + 1 , weight) ;
    if(arr[index] <= weight)res += solve(index + 1 , weight - arr[index] ) ;
    return res ;
}
void go(int index, int weight)
{
    if(index > n || weight == 0) return ;
    // can you take this ?
    if(arr[index] <= weight && solve(index + 1 , weight - arr[index]) == 1) {
         ans[index] = 1 ;
         go(index + 1 , weight - arr[index] ) ;
         return ;
    }
    go(index + 1 , weight) ;
    return  ;
}
int32_t main()
{
    fast;
    cin >> sum >> n ;
    rep(i,1,n) {
        cin >> arr[i] ;
    }
    memset(dp,-1,sizeof(dp)) ;
    int res = solve(1,sum) ;
    if(res == 0) {
        cout << res ;
        return 0 ;
    }
    if(res > 1) {
        cout << -1 ;
        return 0 ;
    }
    go(1,sum) ;
    for(int i = 1 ; i <= n ; ++i) {
        if(ans[i] == 0) {
            cout << i << " " ;
        }
    }
    return 0;
}
Re: WA on #8
Posted by Murodjon (TUIT Urgench) 11 May 2017 11:04
4950
100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 4951
ans = 100

Edited by author 11.05.2017 11:05