题解 CF2229D Me When Median Problem
题解 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.
断言 每次取
s_2+s_3 最小的对一定最优。
::::info[证明]{open}
由于这里操作的过程是一个类似链表删除的结构,所以直接像本场 B 题或 F 题那样直接交换来证是不太容易的。所以我们先考虑容易交换的情况,即
此时我还是不太会证。但是我们是 OIer,所以我们可以花五分钟央求我们温柔美丽可爱强大的 C++ 姐姐帮我们证:
:::success[Code]{open}
//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;
}