5. +
Main
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int m = Integer.parseInt(sc.nextLine());
int n = Integer.parseInt(sc.nextLine());
Company[] companies = new Company[n];
for(int i=0;i<companies.length;i++){
String[] strs = sc.nextLine().split(" ");
companies[i] = new Company(Integer.parseInt(strs[0]),
Integer.parseInt(strs[1]));
}
System.out.println(new Execution(m,companies).getResult());
}
}
データの入力
Companyは会社情報を格納するクラ
スでその会社の人員数と費用を持つ
計算結果の出力
6. +
Execution
※コンストラクタ部は省略
private void calculation(){
result = null;
for(int i=0;i<bg.size();i++){
Result tmp = new Result(bg.get(i),companies);
if(result==null&&m<=tmp.getNum()){
result=tmp;
}
if(m<=tmp.getNum()&&tmp.getCost()<result.getCost()) {
result = tmp;
}
}
}
public Result getResult(){
return result;
}
}
与えられた組み合わせのコストを計算するResultク
ラスを使用して条件を満たし、かつ最小のコストの
組み合わせを計算する
7. +
Result
class Result{
private String str;
private Company[] comps;
private int num;
private int cost;
public Result(String str,Company[] comps){
this.str=str;
this.comps=comps;
analysis();
}
private void analysis(){
num=0;
cost=0;
for(int i=0;i<str.length();i++){
if(str.charAt(i)=='1'){
num+=comps[i].num;
cost+=comps[i].cost;
}
}
}
//getter,setterを省略
}
ビットが立っている時にその
会社の人数とコストを加える
8. +
Company
class Company{
public int num;
public int cost;
public Company(int num,int cost){
this.num=num;
this.cost=cost;
}
public String toString(){
return num+" "+cost;
}
}
9. +
BitGenerator
class BitGenerator{
private int n;
private String[] dataSet;
public BitGenerator(int n){
this.n=n;
dataSet = new String[(int)Math.pow(2,n)];
calculation();
}
private void calculation(){
for(int i=0;i<dataSet.length;i++){
dataSet[i] = complementBit(Integer.toBinaryString(i));
}
}
private void lambdaCalculation(){
IntStream.range(0,dataSet.length).parallel().forEach(e ->
dataSet[e]=complementBit(Integer.toBinaryString(e)));
}
指定されたビット長の2進数
を生成する
10. + private String complementBit(String str){
for(int i=str.length();i<n;i++){
str="0"+str;
}
return str;
}
public int size(){
return dataSet.length;
}
public String get(int index){
return dataSet[index];
}
public void print(){
for(String s:dataSet){
System.out.println(s);
}
}
}
生成された2進数を整形する
例えば生成ビット長が8bitのとき
100011という2進数を00100011に
17. +
Main
import java.util.Scanner;
/**
* Created by nullzine on 2014/09/03.
*/
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int m = Integer.parseInt(sc.nextLine());
int n = Integer.parseInt(sc.nextLine());
Corp[] corps = new Corp[n];
for(int i=0;i<corps.length;i++){
String[] strs = sc.nextLine().split(" ");
corps[i] = new Corp(Integer.parseInt(strs[0]),Integer.parseInt(strs[1]));
}
new Execution(corps,m);
}
}
18. +
Execution class Execution{
private Corp[] corps;
private int m;
public Execution(Corp[] corps,int m){
this.corps=corps;
this.m=m;
calculation();
}
private void calculation(){
int totalMan = 0;
int totalCost = 0;
for (int i = 0; i < corps.length; i++) {
totalMan += corps[i].q;
totalCost += corps[i].r;
}
// 削減可能な人数
int reducingMax = totalMan - m;
// 削減可能コストbefore
int rdCostBefore[] = new int[reducingMax + 1];
// 削減可能コストcurrent
int rdCostCorrent[] = new int[reducingMax + 1];
for (int i = 0; i < corps.length; i++) {
int copyCount = Math.min(corps[i].q, reducingMax + 1);
for (int j = 0; j < copyCount; j++) {
rdCostCorrent[j] = rdCostBefore[j];
}
for (int j = corps[i].q; j <= reducingMax; j++) {
rdCostCorrent[j] = Math.max(rdCostBefore[j],rdCostBefore[j - corps[i].q] + corps[i].r);
}
rdCostBefore = rdCostCorrent;
rdCostCorrent = new int[reducingMax + 1];
}
System.out.println(totalCost - rdCostBefore[reducingMax]);
}
}
19. +
Corp
class Corp{
public int q;
public int r;
public Corp(int q,int r){
this.q=q;
this.r=r;
}
}