T2 P6821 [PA 2012] Tanie linie
考试的时候没写,稍微写一下正确思路。
把原序列正负合并,得到交替正负的新序列
初始时把所有正数段都选上,得到最大段数与总和。如果正数段数量 ≤k,直接输出总和即可。
如果需要减少段数,就要反悔:
要么舍弃一个正数段,代价是损失这个正数; 要么合并两个正数段,吃掉中间负数,代价是损失这个负数的绝对值。
AC CODE
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=1e6+10;
const int INF=1e18;
int n,m;
int ans,cnt,a[maxx],b[maxx];
struct node{
int val,pos;
bool operator < (const node &tmp) const{
return val>tmp.val;
}
};
priority_queue<node> pq;
struct Link{
int pre,nxt;
}lnk[maxx];
bool vis[maxx];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int tot=0;
int now=a[1];
for(int i=2;i<=n;i++){
if((now>0&&a[i]>0)||(now<=0&&a[i]<=0)){
now+=a[i];
}else{
b[++tot]=now;
now=a[i];
}
}
b[++tot]=now;
int l=1,r=tot;
while(l<=r&&b[l]<=0) l++;
while(l<=r&&b[r]<=0) r--;
if(l>r){
cout<<0;
return 0;
}
int len=0;
int sumAll=0;
for(int i=l;i<=r;i++){
b[++len]=b[i];
if(b[len]>0) sumAll+=b[len];
}
tot=len;
int posCnt=0;
for(int i=1;i<=tot;i++){
if(b[i]>0) posCnt++;
}
if(m==0){
cout<<0;
return 0;
}
if(m>=posCnt){
cout<<sumAll;
return 0;
}
int need=posCnt-m;
for(int i=1;i<=tot;i++){
lnk[i].pre=i-1;
lnk[i].nxt=i+1;
int w;
if(b[i]>0) w=b[i];
else w=-b[i];
pq.push((node){w,i});
}
b[0]=b[tot+1]=INF;
while(!pq.empty() && cnt<need){
node tmp=pq.top();
pq.pop();
if(vis[tmp.pos]==true) continue;
cnt++;
ans+=tmp.val;
int cur=tmp.pos,pre=lnk[cur].pre,nxt=lnk[cur].nxt;
vis[pre]=true;
vis[nxt]=true;
b[cur]=b[pre]+b[cur]+b[nxt];
lnk[cur].pre=lnk[pre].pre;
lnk[lnk[cur].pre].nxt=cur;
lnk[cur].nxt=lnk[nxt].nxt;
lnk[lnk[cur].nxt].pre=cur;
int neww;
if(b[cur]>0) neww=b[cur];
else neww=-b[cur];
pq.push((node){neww,cur});
}
int res=sumAll-ans;
cout<<max(res,0ll);
return 0;
}T3 P15726 [JAG 2023 Summer Camp #3] Best parentheses
题意
给定字符串 ,每个位置括号有收益,选取合法的子序列,使得收益和最大。
思路
这道题我们想到的肯定是贪心。不妨先来想想最基础的贪心思路,我们可以判断当前的右括号是否和目前收益最大的且未选的左括号(大根堆对顶的的左括号)的价值和是否为正,如果为正就选取。
然而,这样贪心的问题很明显,我们举一个反例:
现在有三个括号,且有对应的价值如下:
( ) )
-10 50 100如果我们按刚才所说的方法,选取的是第一个左括号和第一个右括号,价值和为 ,然而后面的 显然大于 ,明显更优。
这里就需要反悔,我们在选取一个右括号后,虚拟出一个价值为相反数的左括号入堆,如果后续选择了这个虚拟的左括号,实际上就是完成了一次反悔,因为这里虚拟的左括号和原有的那个有括号的价值抵消了,选取的是更优的一对。
接着看刚刚的例子,选择前两个括号后,加入虚拟的左括号:
( ) ( )
-10 50 -50 100此时 为正数, 也为正数,那这四个都选择,就相当于把最开始选的右括号反悔掉了,选择了更优的方案。
此做法时间复杂度为 。
代码
AC CODE
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=3e5+10;
int n;
struct node{
int val;
bool operator < (const node &tmp) const{
return val < tmp.val;
}
};
priority_queue<node> pq; //大根堆
char s[maxx];
int c[maxx];
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>s; //输入
for(int i=0;i<n;i++){
cin>>c[i];
}
int ans=0;
for(int i=0;i<n;i++){
if(s[i]=='('){ //如果是左括号,放入大根堆
pq.push({c[i]});
}else{
if(pq.empty()) continue;
node top=pq.top();
pq.pop();
int sum=top.val+c[i]; //选择一个右括号和堆顶的左括号
if(sum>0){
ans+=sum;
pq.push({-c[i]}); //虚拟左括号
}else{
// 收益为负,左括号不能丢,重新放回堆
pq.push(top);
}
}
}
cout<<ans;
return 0;
}T4 P16289 [蓝桥杯 2026 省 Python A 组] 购电优化
尽量用更便宜的电,贵的电能不用就不用、能少存就少存。
我们按时间顺序处理每个时段,维护电池里的电:
先用便宜电,扔掉贵的旧电,有空位就充满。
用双端队列维护电池里的电,保证队列里电价单调递增。
AC CODE
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=5e5+10;
int n;
int k,w[maxx],c[maxx],suf[maxx];
void print(__int128 x){
if(x==0){
cout<<0;
return;
}
if(x<0){
cout<<'-';
x=-x;
}
string s;
while(x>0){
s.push_back('0'+(x%10));
x/=10;
}
reverse(s.begin(),s.end());
cout<<s;
return;
}
deque<pair<int,int>> dq;
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>w[i];
for(int i=1;i<=n;i++) cin>>c[i];
int sum=0;
for(int i=n;i>=1;i--){
suf[i]=sum;
sum+=w[i];
}
int cur=0;
__int128 ans=0;
for(int i=1;i<=n;i++){
int need=w[i];
while(need>0){
if(!dq.empty() && dq.front().first<c[i]){
int use=min(need,dq.front().second);
need-=use;
cur-=use;
dq.front().second-=use;
if(dq.front().second==0) dq.pop_front();
}
else{
ans+=(__int128)need*c[i];
need=0;
}
}
while(!dq.empty() && dq.back().first>c[i]){
int price=dq.back().first;
int cnt=dq.back().second;
ans-=(__int128)price*cnt;
cur-=cnt;
dq.pop_back();
}
int limit=min(k,suf[i]);
while(cur>limit){
int price=dq.back().first;
int cnt=dq.back().second;
int d=min(cnt,cur-limit);
ans-=(__int128)price*d;
cur-=d;
dq.back().second-=d;
if(dq.back().second==0) dq.pop_back();
}
if(cur<limit){
int add=limit-cur;
ans+=(__int128)add*c[i];
cur=limit;
if(!dq.empty() && dq.back().first==c[i]) dq.back().second+=add;
else dq.push_back({c[i],add});
}
}
print(ans);
return 0;
}