Codeforces 161 B. Discounts (贪心)
题目链接:http://codeforces.com/contest/161/problem/B
题意:
有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车中有凳子,那么这辆购物车中最便宜的那个物品的价格能减少50%,问你如何放这些物品才能使总价钱最少。
思路:
简单贪心,判断凳子数量是否大于等于k行。
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 typedef pair <LL, int> P; 5 const int N = 1005; 6 LL a[N]; 7 P val[N], val2[N]; 8 LL inf = 1e9 + 7; 9 vector <int> res[N]; 10 int main() 11 { 12 int n, m; 13 scanf("%d %d", &n, &m); 14 int pos = 0, pos2 = 0, c; 15 for(int i = 1; i <= n; ++i) { 16 scanf("%lld %d", a + i, &c); 17 if(c == 2) { 18 val2[++pos2].first = a[i]; 19 val2[pos2].second = i; 20 } else { 21 val[++pos].first = a[i]; 22 val[pos].second = i; 23 } 24 } 25 sort(val + 1, val + pos + 1); 26 sort(val2 + 1, val2 + pos2 + 1); 27 double ans = 0; 28 int f = 0; 29 if(pos >= m) { 30 int i, j; 31 for(i = pos, j = m - 1; j; --i, --j) { 32 ans += val[i].first * 1.0 / 2; 33 res[++f].push_back(val[i].second); 34 } 35 ++f; 36 for(; i >= 2; --i) { 37 ans += val[i].first; 38 res[f].push_back(val[i].second); 39 } 40 for(i = pos2; i > 1; --i) { 41 ans += val2[i].first; 42 res[f].push_back(val2[i].second); 43 } 44 if((val[1].first >= val2[1].first && pos2)) { 45 ans += val[1].first; 46 ans += val2[1].first*1.0 / 2; 47 res[f].push_back(val[1].second); 48 res[f].push_back(val2[1].second); 49 } else if(pos2) { 50 ans += val2[1].first; 51 ans += val[1].first*1.0 / 2; 52 res[f].push_back(val2[1].second); 53 res[f].push_back(val[1].second); 54 } else { 55 ans += val[1].first*1.0 / 2; 56 res[f].push_back(val[1].second); 57 } 58 } else { 59 for(int i = pos; i; --i) { 60 ans += val[i].first * 1.0 / 2; 61 res[++f].push_back(val[i].second); 62 } 63 int i, j; 64 for(i = pos2, j = pos + 1; i && j <= m; --i, ++j) { 65 ans += val2[i].first; 66 res[++f].push_back(val2[i].second); 67 } 68 for(; i; --i) { 69 ans += val2[i].first; 70 res[f].push_back(val2[i].second); 71 } 72 } 73 printf("%.1f\n", ans); 74 for(int i = 1; i <= m; ++i) { 75 printf("%d ", res[i].size()); 76 for(int j = 0; j < res[i].size(); ++j) { 77 printf("%d%c", res[i][j], j == res[i].size() - 1? '\n': ' '); 78 } 79 } 80 return 0; 81 }