传送门
题目描述
你想买n朵花,商店提供了m种,第i种花,买第一支可以获得a[i]的价值,之后无论卖多少支,都是b[i]的价值,每种花都无限提供,问你能获得的最大价值是多少?
分析
挺水的一道题,就是细节挺多的
首先,最多只有一朵花会被购买一次以上,这个应该比较好证明,然后我们去枚举被多次购买的花,如果a[j] > b[i],说明j朵花第一次也需要被购买,排个序二分查找一下范围即可
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
char c=getchar();T x=0,f=1;while(!isdigit(c)){
if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
return (b>0)?gcd(b,a%b):a;}
ll n,m;
struct Node{
ll x,y;
}a[N];
ll w[N],sum[N];
bool cmp(Node A,Node B){
if(A.x != B.x) return A.x < B.x;
else return A.y > B.y;
}
int main(){
int T;
read(T);
while(T--){
memset(w,0,sizeof w);
memset(sum,0,sizeof sum);
read(m),read(n);
for(ll i = 1;i <= n;i++){
read(a[i].x),read(a[i].y);
}
sort(a + 1,a + 1 + n,cmp);
for(ll i = 1;i <= n;i++) {
w[i] = a[i].x;
sum[i] = sum[i - 1] + w[i];
}
ll ans = 0;
for(ll i = 1;i <= n;i++){
ll t = a[i].y;
ll p = upper_bound(w + 1,w + 1 + n,t) - w;
ll res;
if(w[p] > t){
if(n - p + 1 >= m){
res = sum[n] - sum[n - m];
}
else{
res = sum[n] - sum[p - 1];
res = res + t * (m - (n - p + 1) - 1);
if(p <= i) res += t;
else res += a[i].x;
}
}
else{
res = a[i].x + (m - 1) * t;
}
ans = max(ans,res);
}
if(m == 1) ans = sum[n] - sum[n - 1];
dl(ans);
}
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/