Margaritas on the River Walk
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Margaritas on the River Walk相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/OLE' title='OLE'>OLE
using ble sam tput otherwise script color centerOne of the more popular activities in San Antonio is to enjoy margaritas in the park along the river know as the River Walk. Margaritas may be purchased at many establishments along the River Walk from fancy hotels to Joe鈥檚 Taco and Margarita stand. (The problem is not to find out how Joe got a liquor license. That involves Texas politics and thus is much too difficult for an ACM contest problem.) The prices of the margaritas vary depending on the amount and quality of the ingredients and the ambience of the establishment. You have allocated a certain amount of money to sampling different margaritas.
Given the price of a single margarita (including applicable taxes and gratuities) at each of the various establishments and the amount allocated to sampling the margaritas, find out how many different maximal combinations, choosing at most one margarita from each establishment, you can purchase. A valid combination must have a total price no more than the allocated amount and the unused amount (allocated amount 鈥?total price) must be less than the price of any establishment that was not selected. (Otherwise you could add that establishment to the combination.)
For example, suppose you have $25 to spend and the prices (whole dollar amounts) are:
Vendor A B C D H J Price 8 9 8 7 16 5
Then possible combinations (with their prices) are:
ABC(25), ABD(24), ABJ(22), ACD(23), ACJ(21), ADJ( 20), AH(24), BCD(24), BCJ(22), BDJ(21), BH(25), CDJ(20), CH(24), DH(23) and HJ(21).
Thus the total number of combinations is 15.
The input begins with a line containing an integer value specifying the number of datasets that follow, N (1 鈮?N 鈮?1000). Each dataset starts with a line containing two integer values V and D representing the number of vendors (1 鈮?V 鈮?30) and the dollar amount to spend (1 鈮?D 鈮?1000) respectively. The two values will be separated by one or more spaces. The remainder of each dataset consists of one or more lines, each containing one or more integer values representing the cost of a margarita for each vendor. There will be a total of V cost values specified. The cost of a margarita is always at least one (1). Input values will be chosen so the result will fit in a 32 bit unsigned integer.
For each problem instance, the output will be a single line containing the dataset number, followed by a single space and then the number of combinations for that problem instance.
2 6 25 8 9 8 7 16 5 30 250 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30Sample Output
1 15 2 16509438Hint
Note: Some solution methods for this problem may be exponential in the number of vendors. For these methods, the time limit may be exceeded on problem instances with a large number of vendors such as the second example below.
棰樻剰锛氬缁勮緭鍏ワ紝姣忕粍缁欏畾鐗╁搧鏁?<=30)鍜岃儗鍖呭閲?<=1000)浠ュ強鎺ヤ笅鏉ユ瘡涓墿鍝佺殑浣撶Н锛岄棶鏈夊灏戠鏂规锛屼娇寰楄鍏ヤ竴浜涚墿鍝佸悗锛屾棤娉曡鍏ュ墿涓嬬殑浠绘剰涓€涓墿鍝併€?/span>
鍙互杞寲鎴?-1鑳屽寘鏉ュ仛锛岄鍏堟寜浣撶Н浠庡皬鍒板ぇ鎺掑簭锛屾灇涓锯€滃墿涓嬬殑鐗╁搧鈥濅腑浣撶Н鏈€灏忕殑銆傚墿涓嬬殑鐗╁搧涓綋绉渶灏忕殑涓篿鏃讹紝鍓峣-1涓墿鍝佹槸蹇呯劧琚鍏ヨ儗鍖呯殑锛岀劧鍚庡绗琲+1鍒扮n涓墿鍝佸仛0-1鑳屽寘闂锛岃浆绉绘柟绋媐[i][j]琛ㄧず鍓峣涓墿鍝佽鍏ュ閲忎负j鐨勮儗鍖呯殑鏂规鏁帮紝f[i][j] = f[i-1][j] + f[i-1][j-v[j]]銆傞偅涔堜究鏈変簡杩欎釜O(T*n*n*C)鐨勫仛娉曘€?/span>
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int maxn = 1000 + 10; typedef long long ll; ll d[maxn]; ll v[maxn]; int main() { int t,n,m; cin>>t; for(int k = 1; k <= t; k++) { cin>>n>>m; for(int i = 1; i <= n; i++) { scanf("%lld",&v[i]); } sort(v+1, v+1+n); if(v[1] > m) { printf("%d 0 ",k); continue; } ll ans = 0,sum = 0; for(int i = 1; i <= n; i++) { if(sum > m) break; memset(d,0,sizeof d); d[sum] = 1; for(int j = i+1; j <= n; j++) { for(int l = m; l >= sum+v[j]; l--) d[l] += d[l-v[j]]; } for(int j = m-v[i]+1; j <= m; j++) if(j >= sum) ans += d[j]; sum += v[i]; } printf("%d %lld ",k,ans); } return 0; }
浼樺寲锛氶€嗗簭鏋氫妇锛屾寜浣撶Н浠庡ぇ鍒板皬锛屽噺灏戜簡閲嶅瀵?span style="font-size: medium">绗琲+1鍒扮n涓墿鍝佸仛0-1鑳屽寘鐨勮绠?/span>
#include<stdio.h> #include<iostream> #include<algorithm> #include<string.h> using namespace std; const int maxn = 1000 + 10; typedef long long ll; ll d[maxn]; ll v[maxn]; int main() { int t,n,m; cin>>t; for(int k = 1; k <= t; k++) { cin>>n>>m; ll sum = 0; for(int i = 1; i <= n; i++) { scanf("%lld",&v[i]); sum += v[i]; } sort(v+1, v+1+n); if(v[1] > m) { printf("%d 0 ",k); continue; } ll ans = 0; memset(d,0,sizeof d); d[0] = 1; for(int i = n; i >= 1; i--) { sum -= v[i]; int k = max((ll)0,m-sum-v[i]+1); for(int j = k; j <= m-sum; j++) ans += d[j]; for(int j = m; j >= v[i]; j--) d[j] += d[j-v[i]]; } printf("%d %lld ",k,ans); } return 0; }
以上是关于Margaritas on the River Walk的主要内容,如果未能解决你的问题,请参考以下文章
为什么workbench3.3 每次启动都显示 faild to connect to the wind river registry on host “localhost“
SGU438 The Glorious Karlutka River =)(最大流)
三江源区高寒草地地上生物量遥感反演模型研究 Modeling Aboveground Biomass of Alpine Grassland in the Three-River Headwaters