分析: 我们假设购买一种可乐p瓶,我们可以得到期望:p*(m/n*a[i]+(n-m)/n*b[i]),由这个式子我们可以看出唯一的变量是i,所以可以遍历i找出式子的最大值
#include <map> #include <set> #include <stack> #include <cmath> #include <queue> #include <cstdio> #include <vector> #include <string> #include <cstring> #include <iostream> #include <algorithm> #define debug(a) cout << #a << " " << a << endl using namespace std; const int maxn = 2e5 + 10; const int mod = 1e9 + 7; typedef long long ll; ll a[maxn], b[maxn]; int main() { ll n, m, k; while( cin >> n >> m >> k ) { ll sum = -1e9, j = 0, flag; for( ll i = 0; i < k; i ++ ) { cin >> a[i] >> b[i]; if( m*a[i]+(n-m)*b[i] >= sum ) { flag = i; sum = m*a[i]+(n-m)*b[i]; } } for( ll i = 0; i < k; i ++ ) { if( m*a[i]+(n-m)*b[i] == sum && i == flag ) { //取字典序最小 if( i == 0 ) { cout << n; } else { cout << " " << n; } } else { if( i == 0 ) { cout << 0; } else { cout << " " << 0; } } } cout << endl; } return 0; }