提出 #61320459
ソースコード 拡げる
#include <iostream>
#include <cstring>
#include <algorithm>
#include <deque>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <bitset>
#include <iterator>
#include <random>
#include <iomanip>
#include <cctype>
#include <array>
using namespace std;
typedef long long ll;
const ll N=static_cast<ll>(1e5)+10;
ll n,T;
char s[N];
ll freq1[N],freq2[N];
vector<ll> pos; // /的位置
ll down=0,up=0;
ll ans;
//bool check(ll x){
// ll cnt1=freq1[pos[x]]-freq1[down-1];
// ll cnt2=freq2[up]-freq2[pos[x]]; //这里不用减1
// ans=max(min(cnt1,cnt2)*2+1,ans);
// if(cnt1<=cnt2)
// return true;
// return false;
//}
void solve(){
cin>>down>>up;
// 二分
ll l=lower_bound(pos.begin(), pos.end(), down)-pos.begin();
if(pos[l]>up || l==pos.size()){// 防止pos[l]超界(以这种方法找出来的l都超界,那必然没/在里面)
cout<<0<<endl;
return;
}
ll r=upper_bound(pos.begin(), pos.end(), up)-pos.begin()-1;
// if(pos[r]<down){// 防止pos[r]超界
// cout<<0<<endl;
// return;
// }
// 避免起始l==r导致WA
ans=min(freq1[pos[l+r>>1]]-freq1[down-1],freq2[up]-freq2[pos[l+r>>1]])*2+1;
while(l<r){
ll x=l+r+1>>1;
ll cnt1=freq1[pos[x]]-freq1[down-1];
ll cnt2=freq2[up]-freq2[pos[x]]; //这里不用减1
ans=max(min(cnt1,cnt2)*2+1,ans);
if(cnt1==cnt2){
cout<< ans <<endl;
return;
}
if(cnt1<cnt2){
l=x;
}else{
r=x-1;
}
}
ans=max(min(freq1[pos[l]]-freq1[down-1],freq2[up]-freq2[pos[l]]),ans);
l=lower_bound(pos.begin(), pos.end(), down)-pos.begin();
r=upper_bound(pos.begin(), pos.end(), up)-pos.begin()-1;
while(l<r){
ll x=l+r>>1;
ll cnt1=freq1[pos[x]]-freq1[down-1];
ll cnt2=freq2[up]-freq2[pos[x]]; //这里不用减1
ans=max(min(cnt1,cnt2)*2+1,ans);
if(cnt1==cnt2){
cout<< ans <<endl;
return;
}
if(cnt1<cnt2){
l=x+1;
}else{
r=x;
}
}
ans=max(min(freq1[pos[l]]-freq1[down-1],freq2[up]-freq2[pos[l]]),ans);
cout<< ans <<endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>T;
for(int i=1;i<=n;++i)
cin>>s[i];
ll cnt1l =0,cnt2l=0;
for(int i=1;i<=n;++i){
if(s[i]=='1')
cnt1l+=1;
else if(s[i]=='2')
cnt2l+=1;
else
pos.push_back(i);
freq1[i]=cnt1l;
freq2[i]=cnt2l;
}
while (T--) {
if(pos.empty()){
cout<<0<<endl;
continue;
}
solve();
}
return 0;
}
/*
50 10
/211//2///1212/212/12/12/1//11111/12121//22/122221
16 19
27 45
12 32
45 49
33 42
7 9
18 46
1 5
44 45
3 23
*/
提出情報
| 提出日時 |
|
| 問題 |
E - 11/22 Subsequence |
| ユーザ |
znzryb |
| 言語 |
C++ 17 (gcc 12.2) |
| 得点 |
500 |
| コード長 |
2588 Byte |
| 結果 |
AC |
| 実行時間 |
153 ms |
| メモリ |
5772 KiB |
コンパイルエラー
Main.cpp: In function ‘void solve()’:
Main.cpp:39:26: warning: comparison of integer expressions of different signedness: ‘ll’ {aka ‘long long int’} and ‘std::vector<long long int>::size_type’ {aka ‘long unsigned int’} [-Wsign-compare]
39 | if(pos[l]>up || l==pos.size()){// 防止pos[l]超界(以这种方法找出来的l都超界,那必然没/在里面)
| ~^~~~~~~~~~~~
Main.cpp:49:28: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
49 | ans=min(freq1[pos[l+r>>1]]-freq1[down-1],freq2[up]-freq2[pos[l+r>>1]])*2+1;
| ~^~
Main.cpp:49:71: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
49 | ans=min(freq1[pos[l+r>>1]]-freq1[down-1],freq2[up]-freq2[pos[l+r>>1]])*2+1;
| ~^~
Main.cpp:51:25: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
51 | ll x=l+r+1>>1;
| ~~~^~
Main.cpp:69:23: warning: suggest parentheses around ‘+’ inside ‘>>’ [-Wparentheses]
69 | ll x=l+r>>1;
| ~^~
ジャッジ結果
| セット名 |
Sample |
All |
after_contest |
| 得点 / 配点 |
0 / 0 |
500 / 500 |
0 / 0 |
| 結果 |
|
|
|
| セット名 |
テストケース |
| Sample |
00_sample_00.txt |
| All |
00_sample_00.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_corner_00.txt, 02_corner_01.txt, 02_corner_02.txt, 02_corner_03.txt |
| after_contest |
99_after_contest_00.txt, 99_after_contest_01.txt |
| ケース名 |
結果 |
実行時間 |
メモリ |
| 00_sample_00.txt |
AC |
1 ms |
3600 KiB |
| 01_random_00.txt |
AC |
153 ms |
5500 KiB |
| 01_random_01.txt |
AC |
143 ms |
4532 KiB |
| 01_random_02.txt |
AC |
140 ms |
5384 KiB |
| 01_random_03.txt |
AC |
137 ms |
4980 KiB |
| 01_random_04.txt |
AC |
153 ms |
5548 KiB |
| 01_random_05.txt |
AC |
139 ms |
4772 KiB |
| 01_random_06.txt |
AC |
133 ms |
5316 KiB |
| 01_random_07.txt |
AC |
87 ms |
4668 KiB |
| 01_random_08.txt |
AC |
148 ms |
5308 KiB |
| 01_random_09.txt |
AC |
144 ms |
4732 KiB |
| 01_random_10.txt |
AC |
93 ms |
5400 KiB |
| 01_random_11.txt |
AC |
112 ms |
4888 KiB |
| 01_random_12.txt |
AC |
143 ms |
5120 KiB |
| 01_random_13.txt |
AC |
144 ms |
4920 KiB |
| 01_random_14.txt |
AC |
125 ms |
5268 KiB |
| 01_random_15.txt |
AC |
125 ms |
4684 KiB |
| 01_random_16.txt |
AC |
148 ms |
5408 KiB |
| 01_random_17.txt |
AC |
142 ms |
4580 KiB |
| 01_random_18.txt |
AC |
111 ms |
5216 KiB |
| 01_random_19.txt |
AC |
73 ms |
5072 KiB |
| 02_corner_00.txt |
AC |
106 ms |
5124 KiB |
| 02_corner_01.txt |
AC |
100 ms |
3536 KiB |
| 02_corner_02.txt |
AC |
90 ms |
3540 KiB |
| 02_corner_03.txt |
AC |
91 ms |
3492 KiB |
| 99_after_contest_00.txt |
AC |
107 ms |
5772 KiB |
| 99_after_contest_01.txt |
AC |
110 ms |
5384 KiB |