Codeforces 812 C Sagheer and Nubian Market
题目链接:http://codeforces.com/contest/812/problem/C
题意:他给你n个数字,给你一个最多能花的代价,让你求最多能买多少的东西,只是他每个物品代价和要买多少个物品有关系(如买k件物品,则代价为 Axj + xj·k )
题解:第一个想法就是二分,二分每次要买的东西个数,每次在按那个规则排序就能写出来了(PS居然是因为精度问题一直在wa,最后才发现)
1 #include <iostream> 2 #include <cstring> 3 #include <string> 4 #include <vector> 5 #include <queue> 6 #include <map> 7 #include <set> 8 #include <algorithm> 9 #define N 100010 10 #define inf 0x3f3f3f3f 11 #define LL long long 12 using namespace std; 13 LL mid; 14 LL cnt; 15 LL n, m; 16 struct node { 17 LL id, num; 18 }a[N]; 19 bool cmp(node aa, node b) { 20 return aa.id*mid + aa.num < b.id*mid + b.num; 21 } 22 bool solve() { 23 LL sum = 0; 24 sort(a, a + n, cmp); 25 for (int i = 0; i < mid; i++) { 26 sum += a[i].id*mid + a[i].num; 27 } 28 if (sum <= m) { 29 cnt = sum; 30 return true; 31 } 32 return false; 33 } 34 int main() { 35 cin.sync_with_stdio(false); 36 while (cin >> n >> m) { 37 for (int i = 0; i < n; i++) { 38 cin >> a[i].num; 39 a[i].id = i + 1; 40 } 41 cnt = 0; 42 LL l = 0; 43 LL ans = 0; 44 LL r = n; 45 while (l <= r) { 46 mid = (l + r) >> 1; 47 if (solve()) { 48 ans = mid; 49 l = mid + 1; 50 } 51 else { 52 r = mid - 1; 53 } 54 } 55 cout << ans << " " << cnt << endl; 56 } 57 return 0; 58 }
2017-06-02 09:21:38