Introduction

This is my blog of programming, I take notes and leave codes of computer science problems I solved here. Be my guest to comment :)

Thursday, November 22, 2012

uva 10664 - Luggage


/*
Problem link
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
using namespace std;
//-----------------------------
void readfile();
bool solve();
//-----------------------------
int a[25], g[210][25];
int n,s;
//-----------------------------
int main() {
 int ntest;
 cin >> ntest;
 string tmp;
 getline(cin,tmp);
 for (int test=1; test<=ntest; test++)
 {
  n = 0; s = 0;
  readfile();
  if (s%2!=0) cout << "NO" << endl;
  else
  {
   s/=2;
   if (solve()) cout << "YES" << endl;
   else cout << "NO" << endl;
  }
 }
 return 0;
}
//-----------------------------
void readfile() {
 int value = 0;
 string line;
 getline(cin,line);
 line = line+" ";
 for (int i=0; i<line.length(); i++)
 {
  if (line[i]==' ') 
  {
   a[++n] = value;
   s += value;
   value = 0;
  }
  else value = value*10 + int(line[i])-48;
 }
}
//-----------------------------
bool solve() {
 for (int v=0; v<=s; v++)
  for (int i=0; i<=n; i++) g[v][i] = 0;
 for (int v=0; v<=s; v++)
  for (int i=1; i<=n; i++)
   if (a[i]<=v) g[v][i] = max(g[v][i-1],g[v-a[i]][i-1]+a[i]);
 return(g[s][n]==s);
}
//-----------------------------

No comments:

Post a Comment