feat: 添加P7990题解实现

实现P7990问题的解决方案,包括输入处理、排序算法和贪心策略。主要功能是计算在给定条件下能够获得的最大价值。

- 处理输入数据并排序
- 使用贪心算法计算各区间的最大价值
- 输出最终结果
This commit is contained in:
Zengtudor 2025-10-17 21:02:38 +08:00
parent 8ab26b6389
commit e867cd207f

70
src/10/17/P7990.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iostream>
#include <istream>
#include <vector>
using ll = int64_t;
#define sl static inline
const ll maxn = 2e5+5;
ll k,m,n,f[maxn],l[maxn],r[maxn];
struct C{
ll p,t;
inline bool operator<(const C& o){
return p<o.p;
}
}c[maxn];
std::vector<ll> v;
int main(){
std::iostream::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin>>k>>m>>n;
for(ll i=1;i<=k;i++){
std::cin>>c[i].p>>c[i].t;
}
for(ll i=1;i<=m;i++){
std::cin>>f[i];
}
std::sort(c+1,c+1+k);
std::sort(f+1,f+1+m);
v.push_back(0);
ll s;
for(s=1;s<=k && c[s].p<f[1];s++){
v.front()+=c[s].t;
}
// printf("fans=%lld\n",v.front());
while(c[s].p==f[1])s++;
// printf("start=%lld\n",s);
for(ll i=1;i<m;i++){
ll one=0,two=0;
for(;s<=k && c[s].p<f[i+1];s++){
if(c[s].p-f[i] < f[i+1]-c[s].p){
l[i]+=c[s].t;
}else{
r[i]+=c[s].t;
}
two+=c[s].t;
}
one=std::max(l[i],r[i]);
v.push_back(one);
v.push_back(two-one);
while(c[s].p==f[i+1])s++;
// printf("from=%lld, to=%lld, one=%lld, two=%lld\n",f[i],f[i+1],one,two);
}
while(c[s].p==f[m])s++;
v.push_back(0);
for(;s<=k;s++){
v.back()+=c[s].t;
}
// printf("end grass=%lld\n",v.back());
std::sort(v.begin(),v.end(),std::greater<>());
ll ans=0;
for(ll i=0;i<v.size() && i<n;i++){
ans+=v[i];
}
std::cout<<ans<<"\n";
}