結果

問題 No.1858 Gorgeous Knapsack
ユーザー ramia777ramia777
提出日時 2022-05-07 16:20:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,643 bytes
コンパイル時間 820 ms
コンパイル使用メモリ 94,452 KB
実行使用メモリ 200,156 KB
最終ジャッジ日時 2023-09-21 02:55:09
合計ジャッジ時間 4,186 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 20 ms
54,320 KB
testcase_05 AC 9 ms
26,704 KB
testcase_06 AC 26 ms
78,524 KB
testcase_07 AC 32 ms
114,960 KB
testcase_08 AC 36 ms
107,424 KB
testcase_09 AC 66 ms
199,592 KB
testcase_10 AC 51 ms
198,660 KB
testcase_11 AC 54 ms
198,840 KB
testcase_12 AC 59 ms
199,152 KB
testcase_13 AC 47 ms
198,336 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 9 ms
25,460 KB
testcase_25 AC 55 ms
189,140 KB
testcase_26 AC 29 ms
83,084 KB
testcase_27 AC 30 ms
89,032 KB
testcase_28 AC 44 ms
161,960 KB
testcase_29 AC 23 ms
95,876 KB
testcase_30 AC 32 ms
138,836 KB
testcase_31 AC 26 ms
116,444 KB
testcase_32 AC 23 ms
97,956 KB
testcase_33 AC 41 ms
177,912 KB
testcase_34 AC 79 ms
199,988 KB
testcase_35 WA -
testcase_36 AC 79 ms
200,060 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 46 ms
198,244 KB
testcase_39 WA -
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

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[])
{
	SLL 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;
	}


	//美しさの降順になっているため、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