[Codeforces Round #248 (Div. 2)] A. Kitahara Haruki's Gift

7307 단어 codeforces
A. Kitahara Haruki's Gift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Kitahara Haruki has bought n apples for Touma Kazusa and Ogiso Setsuna. Now he wants to divide all the apples between the friends.
Each apple weights 100 grams or 200 grams. Of course Kitahara Haruki doesn't want to offend any of his friend. Therefore the total weight of the apples given to Touma Kazusa must be equal to the total weight of the apples given to Ogiso Setsuna.
But unfortunately Kitahara Haruki doesn't have a knife right now, so he cannot split any apple into some parts. Please, tell him: is it possible to divide all the apples in a fair way between his friends?
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of apples. The second line contains n integers w1, w2, ..., wn (wi = 100 or wi = 200), where wi is the weight of the i-th apple.
Output
In a single line print "YES"(without the quotes) if it is possible to divide all the apples between his friends. Otherwise print "NO"(without the quotes).
Sample test(s)
input
3
100 200 100

output
YES

input
4
100 100 100 200

output
NO

Note
In the first test sample Kitahara Haruki can give the first and the last apple to Ogiso Setsuna and the middle apple to Touma Kazusa.
 
문제풀이: 가방 문제로 전환, 가방 용량은 모든 애플 총 무게의 절반으로 채워질지 확인.100이 아니면 200이기 때문에 애플 무게와 가방 총 용량을 100으로 나누어 메모리를 최적화할 수 있다.
코드:
 
 1 #include<stdio.h>

 2 #include<string.h>

 3 #include<stdbool.h>

 4 #include<stdlib.h>

 5 #include<math.h>

 6 #include<ctype.h>

 7 #include<time.h>

 8 

 9 #define rep(i,a,b)  for(i=(a);i<=(b);i++)

10 #define red(i,a,b)  for(i=(a);i>=(b);i--)

11 #define sqr(x)      ((x)*(x))

12 #define clr(x,y)    memset(x,y,sizeof(x))

13 #define LL          long long

14 

15 int i,j,n,m,sum,

16     f[205],a[100];

17 

18 int main()

19 {

20     int i;

21     

22     clr(f,-1);

23     f[0]=0;

24 

25     scanf("%d",&n);

26     

27     rep(i,1,n){

28         scanf("%d",&a[i]);

29         a[i]/=100;

30         sum+=a[i];

31     }

32     

33     if(sum%2!=0){

34         printf("NO
"); 35 exit(0); 36 } 37 38 sum/=2; 39 rep(i,1,n) 40 rep(j,0,sum-a[i]){ 41 if(f[j]!=-1) 42 f[j+a[i]]=f[j]; 43 } 44 45 if(f[sum]==0) printf("YES"); 46 else printf("NO"); 47 48 49 return 0; 50 }

 
 

좋은 웹페이지 즐겨찾기