답안 #653188

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
653188 2022-10-26T03:39:33 Z guagua0407 Zoltan (COCI16_zoltan) C++17
140 / 140
531 ms 32180 KB
    #include <bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define f first
    #define s second
    #define all(x) x.begin(),x.end()
    #define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
     
    void setIO(string s) {
    	freopen((s + ".in").c_str(), "r", stdin);
    	freopen((s + ".out").c_str(), "w", stdout);
    }
     
    const ll mxn=2e5+5;
    const ll mod=1e9+7;
    ll arr[mxn];
    ll id[mxn];
    map<ll,ll> num;
    ll MX=0;
    pair<ll,ll> segtree[mxn*4][2];
     
    ll fpow(ll a,ll b){
    	ll ans=1;
    	while(b>0){
    		if(b&1) ans=(ans*a)%mod;
    		a=(a*a)%mod;
    		b/=2;
    	}
    	return ans;
    }
     
    pair<ll,ll> comb(pair<ll,ll> a,pair<ll,ll> b){
    	if(a.f==b.f){
    		return {a.f,(a.s+b.s)%mod};
    	}
    	return max(a,b);
    }
     
    void update(ll pos,pair<ll,ll> val,ll k,ll l=1,ll r=MX,ll v=1){
    	if(l==r){
    		segtree[v][k]=comb(segtree[v][k],val);
    		return;
    	}
    	ll mid=(l+r)/2;
    	if(pos<=mid) update(pos,val,k,l,mid,v*2);
    	else update(pos,val,k,mid+1,r,v*2+1);
    	segtree[v][k]=comb(segtree[v*2][k],segtree[v*2+1][k]);
    }
     
    pair<ll,ll> query(ll tl,ll tr,ll k,ll l=1,ll r=MX,ll v=1){
    	if(tl>tr || tl>r || tr<l){
    		return {0,0};
    	}
    	else if(tl<=l and r<=tr){
    		return segtree[v][k];
    	}
    	ll mid=(l+r)/2;
    	return comb(query(tl,min(mid,tr),k,l,mid,v*2),query(max(tl,mid+1),tr,k,mid+1,r,v*2+1));
    }
     
     
    int main() {_
    	//setIO("wayne");
        ll n;
    	cin>>n;
    	for(ll i=1;i<=n;i++){
    		cin>>arr[i];
    		num[arr[i]]++;
    	}
    	for(auto &v:num){
    		MX++;
    		v.s=MX;
    	}
    	for(ll i=1;i<=n;i++){
    		id[i]=num[arr[i]];
    	}
    	for(ll i=n;i>=1;i--){
    		pair<ll,ll> tmp=query(id[i]+1,MX,0);
    		update(id[i],{tmp.f+1,tmp.s+(tmp.f==0)},0);
    	}
    	for(ll i=n;i>=1;i--){
    		pair<ll,ll> tmp=query(1,id[i]-1,1);
    		update(id[i],{tmp.f+1,tmp.s+(tmp.f==0)},1);
    	}
		/*for(int i=1;i<=n;i++){
			cout<<query(i,i,1).f<<','<<query(i,i,1).s<<' '<<query(i,i,0).f<<','<<query(i,i,0).s<<'\n';
		}*/
    	pair<ll,ll> best={0,0};
    	for(ll i=0;i<=MX;i++){
			pair<ll,ll> a,b;
			a=query(i,i,1);
			b=query(i+1,MX,0);
			if(b==make_pair((ll)0,(ll)0)){
				b={0,1};
			}
			if(a==make_pair((ll)0,(ll)0)){
				a={0,1};
			}
    		best=comb(best,{(a.f+b.f)%mod,(a.s*b.s)%mod});
    		//cout<<best.f<<' '<<best.s<<'\n';
    	}
    	ll inv2=fpow(2,mod-2);
    	cout<<best.f<<' '<<((best.s*fpow(2,n-best.f))%mod*inv2)%mod;
    	return 0;
    }
	//maybe its multiset not set

Compilation message

zoltan.cpp: In function 'void setIO(std::string)':
zoltan.cpp:10:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   10 |      freopen((s + ".in").c_str(), "r", stdin);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
zoltan.cpp:11:13: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 |      freopen((s + ".out").c_str(), "w", stdout);
      |      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 1 ms 468 KB Output is correct
11 Correct 286 ms 29108 KB Output is correct
12 Correct 245 ms 27444 KB Output is correct
13 Correct 222 ms 18716 KB Output is correct
14 Correct 345 ms 27444 KB Output is correct
15 Correct 449 ms 30144 KB Output is correct
16 Correct 531 ms 32180 KB Output is correct
17 Correct 309 ms 30216 KB Output is correct
18 Correct 301 ms 30320 KB Output is correct
19 Correct 323 ms 30448 KB Output is correct
20 Correct 317 ms 30332 KB Output is correct