题解 CF2229D Me When Median Problem
自评:绿。
前情提要:这里有非常逆天的做法,建议看看。
简要题意
给定长度为 的数组 ,每次操作可以选择一个 ,并将 替换为一个 中的第二小, 替换为一个 中的第三小。这样重复 次, 和 中就都只剩下一个元素了,求这两个元素的最小值最大可以是多少。
数据范围:多测,。
做法一
高一补过十来场 CF,这个想法在这之间好像也不是第一次出现了,可以当个模型记忆。
求最小值最大且有单调性,考虑二分答案。check 时将 的数变为 , 的数变为 ,如果最终能剩下 就返回 true。
在本题中 的“地位”是相同的,故每个 上只有三种情况:、 和 。 和别的合并时,别的不受影响,故可以忽略 。然后应该是 和 合成 ,最后只要能剩下 就算成功。于是为了省一些 我们发现 和 合成完还是 ,即忽略 后连续的 可以视为一个。这样算下来 的个数能严格小于 的个数就可以留下 了。
check 是 的,故单组复杂度是 的。
//Please ignore those headfiles.I write them just because
//DEV-C++ won't support completion if i use <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<queue>
#define ll long long
#define ull unsigned long long
#define lf double
#define ld long double
#define LL __int128
#define ui unsigned int
using namespace std;
ll T,n,a[100010],b[100010];
bool check(ll now){
ll c0=0,c1=0,flg=0;
for(int i=0;i<n;i++){
if(a[i]>=now&&b[i]>=now){
c1++;
flg=0;
}
else{
if(a[i]<now&&b[i]<now&&!flg){
c0++;
flg=1;
}
}
}
return c0<c1;
}
int main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
cin>>n;
ll l=0,r=0;
for(int i=0;i<n;i++){
cin>>a[i];
r=max(r,a[i]);
}
for(int i=0;i<n;i++){
cin>>b[i];
r=max(r,b[i]);
}
while(l<=r){
ll mid=(l+r>>1);
if(check(mid)){
l=mid+1;
}
else{
r=mid-1;
}
}
cout<<l-1<<endl;
}
return 0;
}做法二
Solution from @akane646, proof by myself.
断言 每次取 最小的对一定最优。
证明
由于这里操作的过程是一个类似链表删除的结构,所以直接像本场 B 题或 F 题那样直接交换来证是不太容易的。所以我们先考虑容易交换的情况,即 。
此时我还是不太会证。但是我们是 OIer,所以我们可以花五分钟央求我们温柔美丽可爱强大的 C++ 姐姐帮我们证:
Code
//Please ignore those headfiles.I write them just because
//DEV-C++ won't support completion if i use <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<queue>
#define ll long long
#define ull unsigned long long
#define lf double
#define ld long double
#define LL __int128
#define ui unsigned int
using namespace std;
ll a[]={1,2,3,4,5,6};
ll sec(vector<ll> tmp){
sort(tmp.begin(),tmp.end());
return tmp[1];
}
ll thi(vector<ll> tmp){
sort(tmp.begin(),tmp.end());
return tmp[2];
}
int main(){
ios::sync_with_stdio(0);
for(int i=0;i<720;i++){
ll l=thi({sec({a[0],a[1],a[2],a[3]}),thi({a[0],a[1],a[2],a[3]}),a[4],a[5]}),r=thi({sec({a[4],a[5],a[2],a[3]}),thi({a[4],a[5],a[2],a[3]}),a[0],a[1]});
if(l==r){
continue;
}
if(!((l<r)^(sec({a[0],a[1],a[2],a[3]})+thi({a[0],a[1],a[2],a[3]})<=sec({a[4],a[5],a[2],a[3]})+thi({a[4],a[5],a[2],a[3]})))){
for(int j=0;j<6;j++){
cout<<a[j]<<' ';
}
}
next_permutation(a,a+6);
}
return 0;
}她什么也没说,看来是默许了,于是在 时以上结论成立。
于是对于 ,对于不符合我们条件的方案,我们可以利用以上证明进行邻项交换,将其变为符合我们条件的方案。
维护当前每个位置的前一个和后一个,并使用 multiset 维护各对 的 即可做到 。
//Please ignore those headfiles.I write them just because
//DEV-C++ won't support completion if i use <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<unordered_map>
#include<set>
#include<queue>
#define ll long long
#define ull unsigned long long
#define lf double
#define ld long double
#define LL __int128
#define ui unsigned int
using namespace std;
struct node{
ll s,pos;
};
bool operator <(node x,node y){
return x.s==y.s?x.pos<y.pos:x.s<y.s;
}
ll T,n,a[100010],b[100010],nxt[100010],pre[100010];
multiset<node> s;
ll sec(ll x){
ll tmp[]={a[x],a[nxt[x]],b[x],b[nxt[x]]};
sort(tmp,tmp+4);
return tmp[1];
}
ll thi(ll x){
ll tmp[]={a[x],a[nxt[x]],b[x],b[nxt[x]]};
sort(tmp,tmp+4);
return tmp[2];
}
int main(){
ios::sync_with_stdio(0);
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
nxt[i]=i+1;
pre[i]=i-1;
}
for(int i=0;i<n;i++){
cin>>b[i];
if(i){
s.insert({sec(i-1)+thi(i-1),i-1});
}
}
while(s.size()){
node tmp=(*s.begin());
s.erase(s.begin());
if(nxt[nxt[tmp.pos]]<n){
s.erase(s.find({sec(nxt[tmp.pos])+thi(nxt[tmp.pos]),nxt[tmp.pos]}));
}
if(pre[tmp.pos]>=0){
s.erase(s.find({sec(pre[tmp.pos])+thi(pre[tmp.pos]),pre[tmp.pos]}));
}
ll t1=sec(tmp.pos),t2=thi(tmp.pos);
a[tmp.pos]=t1;
b[tmp.pos]=t2;
nxt[tmp.pos]=nxt[nxt[tmp.pos]];
pre[nxt[tmp.pos]]=tmp.pos;
if(nxt[tmp.pos]<n){
s.insert({sec(tmp.pos)+thi(tmp.pos),tmp.pos});
}
if(pre[tmp.pos]>=0){
s.insert({sec(pre[tmp.pos])+thi(pre[tmp.pos]),pre[tmp.pos]});
}
}
cout<<min(a[0],b[0])<<endl;
}
return 0;
}