题解:P15102 [ICPC 2025 LAC] Horrible Restaurants
感觉看上去能按照增加一星的代价贪心的样子,玩一下第一个样例。
咦怎么不对。
哦有的餐厅的星被回退了,考虑下反悔贪心。
发现只有 +1、+2 -1、+3 -2、+3 -1 -1、+2 +2 -3 五种可能的情况。
即和为
考虑用小根堆维护增加一星、二星、三星,减少一星、二星、三星的代价。
然后每次就是判断以上五种情况哪个更优。
证明下这样操作一定能得到最优解。
::::info[proof]{open}
令
若存在一个星数变化子集,它存在非
- 若代价和的绝对值为
0 :则A 与B 不够相似。 - 若代价和的绝对值非
0 :则A 和B 中必有一个不是最优解。
所以不存在这种子集。
因此想从
| 选择一个最优的即可。 |
|---|
具体细节可以见代码注释,参考了这个 submission。
::::info[code]
#include<bits/stdc++.h>
#define forUp(i,a,b) for(int i=(a);i<=(b);++i)
#define forUP(i,a,b) for(int i=(a);i<(b);++i)
#define forDown(i,a,b) for(int i=(a);i>=(b);--i)
#define forG(u,v) for(int __i=head[u],v=to[__i];__i;__i=nxt[__i],v=to[__i])
#define forWG(u,v,c) for(int __i=head[u],v=to[__i],c=w[__i];__i;__i=nxt[__i],v=to[__i],c=w[__i])
#define pushb push_back
#define popb pop_back
#define pushf push_front
#define popf pop_front
#define popc __builtin_popcount
#define popc64 __builtin_popcountll
#define seteps(n) fixed<<setprecision(n)
bool __mst;using uint=unsigned int;using int64=long long;using uint64=unsigned long long;using int128=__int128;using uint128=unsigned __int128;using float64=double;using float80=long double;
constexpr int INF=0x3f3f3f3f,MINF=0xcfcfcfcf;constexpr int64 INF64=0x3f3f3f3f3f3f3f3f,MINF64=0xcfcfcfcfcfcfcfcf;constexpr float64 INFDB=1e50,eps=1e-6;
template<class _Tp>inline void chkMax(_Tp &x,const _Tp &y){if(x<y)x=y;}template<class _Tp>inline void chkMin(_Tp &x,const _Tp &y){if(x>y)x=y;}
inline int addMod(const int &x,const int &y,const int mod){int ans=x+y;return ans>=mod?ans-mod:ans;}inline int subMod(const int &x,const int &y,const int mod){int ans=x-y;return ans<0?ans+mod:ans;}
constexpr int N=2e5+10;int __test_num=1,__task_id,__tst;using namespace std;void __init();
int n;int64 A[N][3];
int cur[N];// current star
struct DS{
static constexpr int64 INF64=1e18;
priority_queue<array<int64,3>,vector<array<int64,3>>,greater<array<int64,3>>> pq;
// value pos star
DS(){
// sentinel
forUP(i,0,10)pq.push({INF64,-1,-1});
}
void push(const array<int64,3> &arr){
pq.push(arr);
}
vector<array<int64,3>> top(int k,bool pop=false){// return top "k" of "pq", if "pop" is true, then pop them from "pq"
vector<array<int64,3>> res;
while(k--){
while(!pq.empty()){
auto arr=pq.top();pq.pop();
// if "arr" is sentinel, then push "arr" to "res"
if(arr[1]<0){
res.pushb(arr);
break;
}
// if "star" of "arr" not right, then skip
if(arr[2]!=cur[arr[1]])continue;
bool success=true;
// if "pos" of "arr" already in "res", then skip
for(auto x:res)if(x[1]==arr[1]){
success=false;
break;
}
if(success){
res.pushb(arr);
break;
}
}
}
if(!pop)for(auto arr:res)push(arr);
return res;
}
}add1,add2,add3,sub1,sub2,sub3;
void update(int pos,int x){// update shop "pos" 's star to "x"
if(pos<0)return;// sentinel
cur[pos]=x;
if(x+1<=3)add1.push({A[pos][x],pos,x});
if(x+2<=3)add2.push({A[pos][x]+A[pos][x+1],pos,x});
if(x+3<=3)add3.push({A[pos][x]+A[pos][x+1]+A[pos][x+2],pos,x});
if(x-1>=0)sub1.push({-A[pos][x-1],pos,x});
if(x-2>=0)sub2.push({-A[pos][x-1]-A[pos][x-2],pos,x});
if(x-3>=0)sub3.push({-A[pos][x-1]-A[pos][x-2]-A[pos][x-3],pos,x});
}
bool __med;void __solve(int __test_id){
cin>>n;
forUp(i,1,n){
cin>>A[i][0]>>A[i][1]>>A[i][2];
A[i][2]-=A[i][1],A[i][1]-=A[i][0];
}
forUp(i,1,n)update(i,0);
int64 ans=0;
forUp(i,1,3*n){
array<int64,2> mndt={DS::INF64,0};// value type
{// +1
auto arr=add1.top(1);
chkMin(mndt,{arr[0][0],1});
}
{// +2 -1
// the "pos" of "+2" and "-1" maybe equal
auto arr1=add2.top(2);
auto arr2=sub1.top(2);
if(arr1[0][1]!=arr2[0][1])chkMin(mndt,{arr1[0][0]+arr2[0][0],2});
else{
chkMin(mndt,{arr1[0][0]+arr2[1][0],3});
chkMin(mndt,{arr1[1][0]+arr2[0][0],4});
}
}
{// +3 -2
auto arr1=add3.top(1);
auto arr2=sub2.top(1);
chkMin(mndt,{arr1[0][0]+arr2[0][0],5});
}
{// +3 -1 -1
auto arr1=add3.top(1);
auto arr2=sub1.top(2);
chkMin(mndt,{arr1[0][0]+arr2[0][0]+arr2[1][0],6});
}
{// +2 +2 -3
auto arr1=add2.top(2);
auto arr2=sub3.top(1);
chkMin(mndt,{arr1[0][0]+arr1[1][0]+arr2[0][0],7});
}
ans+=mndt[0];cout<<ans<<'\n';
switch((int)mndt[1]){
case 1:
{
auto arr=add1.top(1,true);
update(arr[0][1],arr[0][2]+1);
}
break;
case 2:
{
auto arr1=add2.top(2,true);
auto arr2=sub1.top(2,true);
update(arr1[1][1],arr1[1][2]);
update(arr2[1][1],arr2[1][2]);
update(arr1[0][1],arr1[0][2]+2);
update(arr2[0][1],arr2[0][2]-1);
}
break;
case 3:
{
auto arr1=add2.top(2,true);
auto arr2=sub1.top(2,true);
update(arr1[1][1],arr1[1][2]);
update(arr2[0][1],arr2[0][2]);
update(arr1[0][1],arr1[0][2]+2);
update(arr2[1][1],arr2[1][2]-1);
}
break;
case 4:
{
auto arr1=add2.top(2,true);
auto arr2=sub1.top(2,true);
update(arr1[0][1],arr1[0][2]);
update(arr2[1][1],arr2[1][2]);
update(arr1[1][1],arr1[1][2]+2);
update(arr2[0][1],arr2[0][2]-1);
}
break;
case 5:
{
auto arr1=add3.top(1,true);
auto arr2=sub2.top(1,true);
update(arr1[0][1],arr1[0][2]+3);
update(arr2[0][1],arr2[0][2]-2);
}
break;
case 6:
{
auto arr1=add3.top(1,true);
auto arr2=sub1.top(2,true);
update(arr1[0][1],arr1[0][2]+3);
update(arr2[0][1],arr2[0][2]-1);
update(arr2[1][1],arr2[1][2]-1);
}
break;
case 7:
{
auto arr1=add2.top(2,true);
auto arr2=sub3.top(1,true);
update(arr1[0][1],arr1[0][2]+2);
update(arr1[1][1],arr1[1][2]+2);
update(arr2[0][1],arr2[0][2]-3);
}
break;
default:
assert(false);
}
}
}
signed main(){
__init();
forUp(i,1,__test_num)__solve(i);
cerr<<1000.0*(clock()-__tst)/CLOCKS_PER_SEC<<"ms "<<((&__mst)-(&__med))/1024.0/1024.0<<"MB"<<'\n';
return 0;
}
void __init(){
__tst=clock();
#ifndef use_file
//#define use_file
#endif
#ifdef use_file
const string __file_name="test";freopen((__file_name+".in").c_str(),"r",stdin);freopen((__file_name+".out").c_str(),"w",stdout);
#endif
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//scanf("%d",&__test_num);
//cin>>__test_num;
}
::::