結果

問題 No.1858 Gorgeous Knapsack
ユーザー ramia777ramia777
提出日時 2022-05-07 16:46:20
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 126 ms / 2,000 ms
コード長 2,691 bytes
コンパイル時間 1,144 ms
コンパイル使用メモリ 93,896 KB
実行使用メモリ 198,948 KB
最終ジャッジ日時 2023-09-21 03:26:13
合計ジャッジ時間 4,719 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 20 ms
54,248 KB
testcase_05 AC 8 ms
26,672 KB
testcase_06 AC 30 ms
73,280 KB
testcase_07 AC 30 ms
77,168 KB
testcase_08 AC 46 ms
94,900 KB
testcase_09 AC 94 ms
154,504 KB
testcase_10 AC 44 ms
93,428 KB
testcase_11 AC 56 ms
109,980 KB
testcase_12 AC 77 ms
136,508 KB
testcase_13 AC 29 ms
76,636 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 1 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 9 ms
25,476 KB
testcase_25 AC 70 ms
129,296 KB
testcase_26 AC 36 ms
81,132 KB
testcase_27 AC 40 ms
83,504 KB
testcase_28 AC 44 ms
97,224 KB
testcase_29 AC 17 ms
62,864 KB
testcase_30 AC 31 ms
65,320 KB
testcase_31 AC 19 ms
65,108 KB
testcase_32 AC 17 ms
62,808 KB
testcase_33 AC 24 ms
69,500 KB
testcase_34 AC 126 ms
198,900 KB
testcase_35 AC 92 ms
198,932 KB
testcase_36 AC 104 ms
198,948 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 34 ms
73,212 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 91 ms
198,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
// Yukicoder
// No.1858
// 

//#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>//list
#include <set> //tree
#include <map> //連想配列
#include <unordered_set> //hash
#include <unordered_map> //hash
#include <algorithm>
#include <iomanip>
#include <string>
#include <stdlib.h>


using namespace std;
typedef unsigned long long ULL;
typedef signed long long SLL;
typedef unsigned int UINT;


#define START (0)
#define RIGHT (1)
#define UP    (2)
#define LEFT  (3)
#define DOWN  (4)

#define DATA_MAX (1000000)
#define FMAX(a,b)  ((a)>(b)?(a):(b))
#define FMIN(a,b)  ((a)<(b)?(a):(b))

//vectorは便利だが処理時間がかかるので注意
vector <vector <SLL>> memo2;//二次元可変配列
vector<SLL> memo1;


SLL H ,A, B, T, F, N, M, Result;


SLL backet[1000005];
SLL dp[5005][5005];

struct BOX {
	SLL w;
	SLL v;
};

BOX box[5005];


//美しさの降順にソート
int compare(const BOX * a, const BOX *b)
{
	if (a->v < b->v)
		return (1);
	else if (a->v > b->v)
		return (-1);
	return (0);
}



int main(int argc, char *argv[])
{
	ULL ans = 0;

	// 入力部
	cin >> N;
	cin >> M;


	for (int i = 0; i < N; i++)
	{
		cin >> box[i].v;
		cin >> box[i].w;
	}

	//Sorting
	qsort(&box[0], N, sizeof(BOX), (int(*)(const void*,const void*))compare);

	//ソート結果のデバッグ出力
	/*
	for (int i = 0; i < N; i++)
	{
		cout << "V=" << box[i].v << ",W=" << box[i].w << endl;
	}*/
	

	//最初の1行目(i=0)
	for (int j = 0; j <= M; j++)
	{
		if (j >= box[0].w)
		{
			dp[0][j] = box[0].v;
			ans = FMAX(ans, box[0].v * dp[0][j]);
		}
	}

	

	//美しさの降順になっているため、i番目以下で最も美しさの小さいものはi番目である
	for(int i=1;i<N;i++)
		for (int j = 0; j <= M; j++)//ナップサックの容量を1つづつ増やしていく
		{
			if (j >= box[i].w)
			{
					// "j" の容量のときに、下記AとBを比較する
				    //    A: 今回の宝石を入れた場合の価値  → dp[i - 1][j - box[i].w] + box[i].v
				    //    B: 今回の宝石を入れなかった場合の価値 → dp[i - 1][j]
				
				if (dp[i - 1][j - box[i].w] + box[i].v > dp[i - 1][j])
				{
					dp[i][j] = dp[i - 1][j - box[i].w] + box[i].v;
					ans = FMAX(ans, box[i].v * dp[i][j]);
				}
				else
				{
					dp[i][j] = dp[i - 1][j];
				}
					
			}
			else
			{
				dp[i][j] = dp[i - 1][j];
			}
		}

	
	cout << ans << endl;

	/*
	for (int i = 0; i < N; i++)
	{
		for (int j = 0; j <= M; j++)//ナップサックの容量を1つづつ増やしていく
		{
			cout << dp[i][j] << ",";
		}
		cout << endl;
	}
	*/

	///////////////////
	//cout << endl;
	getchar();

	return 0; //end
}
0