Submission #68601988


Source Code Expand

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pl;
typedef pair<ll,pl> ppl;
typedef pair<ll,ppl> pppl;
typedef pair<ll,pppl> pppppl;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) begin(a),end(a)
#define sz(a) (int)(a).size()
#define F first
#define S second
#define bs(A,x) binary_search(all(A),x)
#define lb(A,x) (ll)(lower_bound(all(A),x)-A.begin())
#define ub(A,x) (ll)(upper_bound(all(A),x)-A.begin())
#define cou(A,x) (ll)(upper_bound(all(A),x)-lower_bound(all(A),x))
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
//*
using mint=modint998244353;
const ll mod=998244353;
//*/
/*
using mint=modint1000000007;
const ll mod=1000000007;
//*/
//using mint=modint;
//*
typedef vector<mint> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
typedef vector<vvvm> vvvvm;
ostream&operator<<(ostream&os,mint a){os<<a.val();return os;}
istream&operator>>(istream&is,mint&a){int x;is>>x;a=mint(x);return is;}
//*/
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,0,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}
int main(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  ll N;cin>>N;
  vl P(N);cin>>P;
  rep(i,0,N)P[i]--;
  vl C;
  {
    dsu G(N);
    rep(i,0,N)G.merge(i,P[i]);
    auto gr=G.groups();
    for(auto v:gr)C.emplace_back(sz(v));
  }
  
  sort(all(C));
  
  //L=1     L=2        L=3          L=4        L=5
  // 01     022        0233         02444      024555
  // 0      04         035          0366       03677
  //        0          04           048        0479
  //                   0            04         048
  //                                0          04
  //                                           0
  
  //f(i,j,L)  =  min(L,2i)+min(L-L%2,2i,2j,(i==jのときはi=j=L/2以外なら2i-1))
  // rep(L,1,6){
  //   cout<<"L="<<L<<endl;
  //   rep(j,0,L+1){rep(i,0,L-j+1)cout<<min(L,2*i)+min({L-L%2,2*i,2*j,(i==j&&2*i!=L&&i?2*i-1:L)})<<" ";cout<<endl;}
  // }
  
  vl E,O;
  for(auto c:C)(c%2?O:E).emplace_back(c);
  
  reverse(all(O));
  vl AE(sz(E)+1),AO(sz(O)+1),BO(sz(O)+1);
  rep(i,0,sz(E))AE[i+1]=AE[i]+E[i];
  rep(i,0,sz(O))AO[i+1]=AO[i]+O[i];
  rep(i,0,sz(O))BO[i+1]=BO[i]+O[i]-1;
  
  bitset<400000>B;
  B[0]=1;
  for(auto e:E)B|=B<<e;
  
  //cout<<C<<endl;
  
  ll zero=count(all(O),1);
  
  ll Q;cin>>Q;
  while(Q--){
    ll a,b,c;cin>>a>>b>>c;
    if(a==b&&B[a*2]){cout<<4*a<<endl;continue;}
    ll t=ub(AE,min(a,b)*2);
    ll ans=2*AE[t-1];
    a-=AE[t-1]/2;b-=AE[t-1]/2;
    if(t==sz(AE)){
      ll u=ub(BO,2*a);
      if(u==sz(BO)){
        chmin(a,BO.back()/2+sz(O));
        ans+=BO.back()+(a-BO.back()/2);
        ll bb=max(sz(O)-a+BO.back()/2-zero,0ll);
        ll two=BO.back()/2-bb;
        ll one=2*bb;
        if(b<=two)ans+=2*b;
        else ans+=2*two+min(one,b-two);
      }
      else{
        if(BO[u-1]==2*a)u--;
        ans+=2*a;
        ll two=a-u;
        ll one=2*u;
        if(b<=two)ans+=2*b;
        else ans+=2*two+min(one,b-two);
      }
    }
    else if(a<E[t-1]/2){
      ans+=2*a+min(2*b,(a==b&&a?2*a-1:2*a));
    }
    else{
      ans+=E[t-1]+2*b;a-=E[t-1]/2;
      ll rest=AE.back()-AE[t]+AO.back()-sz(O);
      if(rest>=2*a)ans+=2*a;
      else ans+=rest+min((ll)sz(O),a-rest/2);
    }
    cout<<ans<<endl;
  }
  return 0;
}

Submission Info

Submission Time
Task C - Maximize Sum of Mex
User TKTYI
Language C++ 20 (gcc 12.2)
Score 900
Code Size 4256 Byte
Status AC
Exec Time 1484 ms
Memory 21652 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 900 / 900
Status
AC × 3
AC × 51
Set Name Test Cases
Sample 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt
All 01-sample-01.txt, 01-sample-02.txt, 01-sample-03.txt, 02-small-01.txt, 02-small-02.txt, 02-small-03.txt, 02-small-04.txt, 02-small-05.txt, 02-small-06.txt, 02-small-07.txt, 02-small-08.txt, 02-small-09.txt, 02-small-10.txt, 03-random-01.txt, 03-random-02.txt, 03-random-03.txt, 03-random-04.txt, 03-random-05.txt, 03-random-06.txt, 03-random-07.txt, 03-random-08.txt, 03-random-09.txt, 03-random-10.txt, 04-divide-01.txt, 04-divide-02.txt, 04-divide-03.txt, 04-divide-04.txt, 04-divide-05.txt, 04-divide-06.txt, 04-divide-07.txt, 04-divide-08.txt, 04-divide-09.txt, 04-divide-10.txt, 05-even-divide-01.txt, 05-even-divide-02.txt, 05-even-divide-03.txt, 05-even-divide-04.txt, 05-even-divide-05.txt, 05-even-divide-06.txt, 05-even-divide-07.txt, 05-even-divide-08.txt, 05-even-divide-09.txt, 05-even-divide-10.txt, 06-even-triangular-divide-01.txt, 06-even-triangular-divide-02.txt, 07-large-cycle-01.txt, 07-large-cycle-02.txt, 08-odd-triangular-divide-01.txt, 08-odd-triangular-divide-02.txt, 09-triangular-divide-01.txt, 09-triangular-divide-02.txt
Case Name Status Exec Time Memory
01-sample-01.txt AC 1 ms 3768 KiB
01-sample-02.txt AC 1 ms 3624 KiB
01-sample-03.txt AC 1 ms 3640 KiB
02-small-01.txt AC 1 ms 3620 KiB
02-small-02.txt AC 1 ms 3564 KiB
02-small-03.txt AC 1 ms 3764 KiB
02-small-04.txt AC 1 ms 3696 KiB
02-small-05.txt AC 1 ms 3572 KiB
02-small-06.txt AC 1 ms 3496 KiB
02-small-07.txt AC 1 ms 3620 KiB
02-small-08.txt AC 1 ms 3620 KiB
02-small-09.txt AC 1 ms 3644 KiB
02-small-10.txt AC 1 ms 3624 KiB
03-random-01.txt AC 359 ms 17168 KiB
03-random-02.txt AC 357 ms 16772 KiB
03-random-03.txt AC 359 ms 17428 KiB
03-random-04.txt AC 356 ms 16736 KiB
03-random-05.txt AC 358 ms 16900 KiB
03-random-06.txt AC 359 ms 17332 KiB
03-random-07.txt AC 360 ms 17244 KiB
03-random-08.txt AC 359 ms 16456 KiB
03-random-09.txt AC 360 ms 16808 KiB
03-random-10.txt AC 362 ms 16716 KiB
04-divide-01.txt AC 449 ms 17328 KiB
04-divide-02.txt AC 391 ms 17004 KiB
04-divide-03.txt AC 461 ms 17696 KiB
04-divide-04.txt AC 407 ms 17036 KiB
04-divide-05.txt AC 482 ms 17272 KiB
04-divide-06.txt AC 464 ms 17328 KiB
04-divide-07.txt AC 450 ms 17264 KiB
04-divide-08.txt AC 472 ms 17168 KiB
04-divide-09.txt AC 406 ms 16984 KiB
04-divide-10.txt AC 468 ms 17568 KiB
05-even-divide-01.txt AC 1484 ms 21652 KiB
05-even-divide-02.txt AC 938 ms 18600 KiB
05-even-divide-03.txt AC 755 ms 17984 KiB
05-even-divide-04.txt AC 660 ms 17828 KiB
05-even-divide-05.txt AC 601 ms 17692 KiB
05-even-divide-06.txt AC 564 ms 17528 KiB
05-even-divide-07.txt AC 537 ms 17568 KiB
05-even-divide-08.txt AC 520 ms 17524 KiB
05-even-divide-09.txt AC 502 ms 17544 KiB
05-even-divide-10.txt AC 490 ms 17600 KiB
06-even-triangular-divide-01.txt AC 370 ms 17428 KiB
06-even-triangular-divide-02.txt AC 369 ms 16720 KiB
07-large-cycle-01.txt AC 360 ms 17340 KiB
07-large-cycle-02.txt AC 356 ms 16980 KiB
08-odd-triangular-divide-01.txt AC 365 ms 17364 KiB
08-odd-triangular-divide-02.txt AC 364 ms 16936 KiB
09-triangular-divide-01.txt AC 368 ms 17300 KiB
09-triangular-divide-02.txt AC 368 ms 16780 KiB