Sunday, 10 May 2015

MINIMUM NO NEED TO INSERT TO FORM 1 TO N NUMBERS

                                                          GOOGLE CODEJAM --
 
FIND MINIMUM NO OF NUMBERS NEEDED TO INSERT IN A SET OF GIVEN NUMBERS SUCH THAT ALL NO TILL 'D' CAN BE  FORMED BY SUMMING UP THE NOS OF GIVEN SET AND ADDED NEW ELEMENTS ..
NO NUMBER CAN BE USED TWICE IN FORMING ANY NUMBER ..


EX-
LET GIVEN SET = 1,2 AND WE NEED TO FORM SUMM TILL 5
THAN 1 CAN BE FORMED ,
2 CAN BE FORMRD
3 CAN BE FORMED (1+2)
4 CAT BE FORMED SOO ADD 4 IN SET
5 NOW 5 CAN BE FORMED BY(4+1) SINCE 4 IS ADDED IN THE SET

HENCE NO OF NUMBER INSERTED =1 ( NO IS 4);

-----------------------------------------------------------EDITORIAL-------------------------------------------
  FIRST CALCULATE ALL NOS WHICH CAN BE FORMED FROM GIVEN SET OF NUMBERS ..
 NOW ITERATE FROM 1 TO D AND IN ANY NO IS NOTE FORMED THAN INSERT THAT NO IN THE SET AND ALSO CALCULATE ALL NEW NOS FORMED DUE TO INSERTION OF THIS NEW NO ...

------------------------------------------CODE--------------------------------------------------------------

#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <numeric>
#include <utility>
#include <bitset>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
using namespace std;
 #define tr(container, it) \
    for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define maX(a,b) (a) > (b) ? (a) : (b)
#define pii pair< int, int >
#define pip pair< int, pii >
#define FOR(i,n) for(int i=0; i<(int)n ;i++)
#define REP(i,a,n) for(int i=a;i<(int)n;i++)
#define pb push_back
#define mp make_pair
typedef long long ll;
//int dx[]= {-1,0,1,0};
//int dy[]= {0,1,0,-1};
int A[105];
int main()
{
// freopen("C-small-attempt0.in","r",stdin);
  //  freopen("C-small-attempt0.out","w",stdout);
    int t;
    scanf("%d",&t);
    int kas = 0;
    while(t--)
    {
    kas++;
    int c,d,v;
        cin >>  d >> v;
        FOR(i,d)
        scanf("%d",&A[i]);// GIVEN SET
        set<int> S;
        S.insert(0);
        for(int i=0;i<d;i++)// CALCULATING ALL NOS THAT CANBE FORMED FROM                                                            //GIVENSET
        {
            std::vector<int> G;
            for(set<int>::iterator it = S.begin(); it != S.end();it++)
            {
                G.pb(*it + A[i]);
            }
            FOR(k,G.size())
            {
                S.insert(G[k]);
            }
        }
     

        int ans = 0;
        for(int i=1;i<=v;i++)
        {
            if(S.find(i) == S.end())// IF NOT FOUND I IN THE SET THAN INSERT IT AND ALL                                                         //NEW NOSTHAT CAN BE FORMED DUE TOTHIS INSERTION
            {
                ans++;
                std::vector<int> G;
                for(set<int>::iterator it = S.begin(); it != S.end();it++)
                {
                    G.pb(*it + i);
                }
                FOR(k,G.size())
                {
                    S.insert(G[k]);
                }
            }
        }
        printf("Case #%d: %d\n",kas,ans);
    }
return 0;
}

No comments:

Post a Comment