mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-12-17 04:33:02 +00:00
feat: 添加P14507.cpp解题代码
实现一个处理特定输入格式并计算结果的算法
This commit is contained in:
parent
1b4eec97e6
commit
80860dc70b
81
src/11/16/P14507.cpp
Normal file
81
src/11/16/P14507.cpp
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdio>
|
||||||
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <map>
|
||||||
|
#include <set>
|
||||||
|
#include <vector>
|
||||||
|
using ll = int64_t;
|
||||||
|
#define sl static inline
|
||||||
|
|
||||||
|
ll n,q;
|
||||||
|
std::map<ll, ll> m;
|
||||||
|
|
||||||
|
sl void solve(){
|
||||||
|
std::cin>>n>>q;
|
||||||
|
bool isnotok=false;
|
||||||
|
m.clear();
|
||||||
|
ll last=-1;
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
ll a,b;
|
||||||
|
std::cin>>a>>b;
|
||||||
|
if(isnotok)continue;
|
||||||
|
if(a!=last+1){
|
||||||
|
isnotok=true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
last=a;
|
||||||
|
m[a]+=b;
|
||||||
|
}
|
||||||
|
while(q--){
|
||||||
|
ll nq;
|
||||||
|
std::cin>>nq;
|
||||||
|
printf("getq = %lld\n",nq);
|
||||||
|
auto nm=m;
|
||||||
|
ll ans=0;
|
||||||
|
while(nm.size()){
|
||||||
|
auto pm = nm.lower_bound(nq);
|
||||||
|
if(nq==0||pm->first>nq)break;
|
||||||
|
ans++;
|
||||||
|
ll min=1e9+7;
|
||||||
|
if(pm!=nm.end())pm++;
|
||||||
|
for(auto wp=nm.begin();wp!=pm;wp++){
|
||||||
|
min=std::min(min,wp->second);
|
||||||
|
}
|
||||||
|
printf("got min=%lld, pm->first=%lld\n",min,pm->first);
|
||||||
|
ll maxget = nq/pm->first;
|
||||||
|
printf("maxget=%lld\n",maxget);
|
||||||
|
ll get=std::min(maxget,min);
|
||||||
|
printf("get=%lld\n",get);
|
||||||
|
bool isrm=false;
|
||||||
|
std::vector<decltype(nm.begin())> rm;
|
||||||
|
for(auto wp=nm.begin();wp!=pm;wp++){
|
||||||
|
if(isrm){
|
||||||
|
rm.push_back(wp);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
wp->second-=get;
|
||||||
|
if(wp->second==0){
|
||||||
|
isrm=true;
|
||||||
|
rm.push_back(wp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for(auto i:rm){
|
||||||
|
nm.erase(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout<<(ans==0?-1:ans)<<"\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
|
std::cin.tie(nullptr);
|
||||||
|
|
||||||
|
ll T;
|
||||||
|
std::cin>>T;
|
||||||
|
while(T--){
|
||||||
|
solve();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user