Singapore NOI - Knapsack
Author: Kevin Sheng
Explanation
Note that the bound on is rather small and that , so we can group the items together by weight. After this, we can implement the knapsack problem on the grouped together items. We also sort items of the same weight by reverse value, as it is always optimal to select the item with greater value among two items of the same weight.
Note that you only need to consider the most valuable items for the set of all objects with weight . So in total you will need to consider items. This expression approximates to as . Each item weight takes to process, making the total time complexity .
Implemention
Time Complexity:
C++
#include <algorithm>#include <iostream>#include <map>#include <vector>using std::cout;using std::endl;using std::pair;using std::vector;
Java
Warning!
Due to Java's (relatively) slow speed, this solution will often TLE on the last subtask.
import java.io.*;import java.util.*;public class knapsack {public static void main(String[] args) throws IOException {BufferedReader read = new BufferedReader(new InputStreamReader(System.in));StringTokenizer initial = new StringTokenizer(read.readLine());int limit = Integer.parseInt(initial.nextToken());int typeNum = Integer.parseInt(initial.nextToken());HashMap<Integer, ArrayList<int[]>> byWeight = new HashMap<>();
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!