UVA12325Zombie's Treasure Chest 寶箱
阿新 • • 發佈:2018-11-20
題意:給定兩個箱子體積s1,s2,價值v1,v2,給出一個體積為V的寶箱,求可裝入的最大價值。
分析:正常寫肯定是超時的,把狀況簡化,第一種,當s1,s2都很小時,就看它們的價值比,v1/s1 ,v2/s2當v1/s1>v2/s2時,就說明v1的價值更大,更多的搜尋v1,v2寶箱最多搜尋s1個。下面同理。
第二種,當s1,s2都很大,s1>s2時,優先搜尋s1,s1很快就能搜尋完。
程式碼部分:
#include<cstdio> #include<cstring> #include<cctype> #include<queue> #include<iostream> #include<vector> #include<list> using namespace std; typedef long long LL; int s1, s2, v1, v2,M; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin>>M >> s1 >> v1 >> s2 >> v2; LL curM = 0; if (s1 < s2) { int temp = s1; s1 = s2; s2 = temp; temp = v1; v1 = v2; v2 = temp; } if (M/s1>=65536) { for (LL j = 0; j <= s1; j++) { curM = max(curM, j*v2 + ((M - j * s2) / s1)*v1); } for (LL j = 0; j <= s2; j++) { curM = max(curM, j*v1 + ((M - j * s1) / s2)*v2); } } else { for (LL j = 0; s1*j<=M; j++) { curM = max(curM, j*v1 + ((M - j * s1) / s2)*v2); } } cout << "Case #" << i <<": "<<curM<< endl; } //system("pause"); return 0; }