标签:end -- panel namespace for max sizeof cin algorithm
Problem Description
2
1 1
5
ans : 4
1
10
3
ans : 3
0
java版:
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { int n, m; int a[] = new int[5000]; int dp[] = new int[5000]; Scanner sc = new Scanner(System.in); while (sc.hasNext()) { n = sc.nextInt(); if (n == 0) break; Arrays.fill(a, 0); Arrays.fill(dp, 0); for (int i = 0; i < n; i++) { a[i] = sc.nextInt(); } Arrays.sort(a, 0, n); m = sc.nextInt(); for (int i = 0; i < n - 1; i++) for (int j = m - 5; j >= a[i]; j--) { dp[j] = Math.max(dp[j], dp[j - a[i]] + a[i]); } if (m < 5) System.out.println(m); else System.out.println(m - dp[m - 5] - a[n - 1]); } } }
c++版(并没什么吊用):
#include <iostream> #include <algorithm> #include <cstring> #include <cstdio> #include <cmath> #include <cstdlib> using namespace std; int a[10000]; int dp[10000]; int main(){ int n,m; while(cin>>n&&n){ memset(a,0,sizeof(a)); memset(dp,0,sizeof(dp)); for(int i = 0;i<n;i++){ cin>>a[i]; } cin>>m; sort(a,a+n); for(int i=0;i<n-1;i++){ for(int k=m-5;k>=a[i];k--){ dp[k]=max(dp[k],dp[k-a[i]]+a[i]); } } if(m<5) cout<<m<<endl; else cout<<m-dp[m-5]-a[n-1]<<endl; } return 0; }
标签:end -- panel namespace for max sizeof cin algorithm
原文地址:http://www.cnblogs.com/upstart/p/6692817.html