Posted: Sat Sep 09, 2006 5:57 am
[quote="ImLazy"]By the way, what means "1
But i think 0/1 knapsack. You can try with it. From ranklist its not betterKallol wrote:.......For parttioning into two parts I can imagine a way .......Whats goin to be a better algorithm ?
Code: Select all
#include<stdio.h>
#include<stdlib.h>
int comp(void const * a, void const * b)
{
return ( *(int*) b - *(int*)a);
}
int main()
{
int tcase;
scanf("%d",&tcase);
while(tcase--)
{
int a[100],i=0;
int sum = 0;
while(scanf("%d",&a[i]))
{
sum += a[i++];
if((getchar())=='\n') break;
}
qsort(a,i,sizeof(int),comp);
int n = i,tot,val,j,flag;
if(sum % 2==0)
{
In this section I just checked if the
}
if(flag==1)
printf("YES\n");
else printf("NO\n");
}
else printf("NO\n");
//for(i=0;i<n;i++)
//printf("%d ",a[i]);
}
return 0;
}
Code: Select all
#include <iostream>
#include <algorithm>
#include <string>
#include <sstream>
using namespace std;
bool dp[201];
int a[201];
int main()
{
int n;
string s;
stringstream str;
cin >> n;
getline(cin , s);
for(int pp =0 ; pp < n ; pp++)
{
fill(a, a+201 , 0);
getline(cin , s);
str.str(s);
int tmp;
long long int total = 0;
int ind = 0;
while(str >> tmp)
{
a[ind] = tmp;
ind++;
total+=tmp;
}
if(total%2 == 1)
{
cout << "NO" << endl;
continue;
}
int final = total/2;
fill(dp , dp+201 , false);
sort(a , a+ind );
dp[0] = true;
for(int i = 0 ; i < ind ; i++)
{
for(int j = final ; j>= a[i] ; j--)
{
if(j-a[i] >= 0 && dp[j-a[i]] == true)
dp[j] = true;
}
}
if(dp[final])
cout << "YES" << endl;
else
cout << "NO" << endl;
str.clear();
}
return 0;
}