QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#111153 | #3236. Hills And Valleys | Sorting | 100 ✓ | 35ms | 11468kb | C++20 | 2.4kb | 2023-06-06 06:03:09 | 2023-06-06 06:03:12 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MAXN = 1e5 + 7 ;
int n ;
string a ;
int pref[ MAXN ][ 10 ] , suff[ MAXN ][ 10 ] ;
pii dp[ 10 ][ 10 ][ 10 ] ;
void solve ( ) {
cin >> n ;
cin >> a ;
a = '#' + a ;
for ( int i = 1 ; i <= n ; ++ i ) {
for ( int j = 0 ; j < 10 ; ++ j ) {
pref[ i ][ j ] = pref[ i - 1 ][ j ] ;
}
++ pref[ i ][ ( a[ i ] - '0' ) ] ;
for ( int j = 1 ; j < 10 ; ++ j ) {
pref[ i ][ j ] = max ( pref[ i ][ j ] , pref[ i ][ j - 1 ] ) ;
}
}
for ( int i = 0 ; i < 10 ; ++ i ) {
suff[ n + 1 ][ i ] = 0 ;
}
for ( int i = n ; i >= 1 ; -- i ) {
for ( int j = 0 ; j < 10 ; ++ j ) {
suff[ i ][ j ] = suff[ i + 1 ][ j ] ;
}
++ suff[ i ][ ( a[ i ] - '0' ) ] ;
for ( int j = 8 ; j >= 0 ; -- j ) {
suff[ i ][ j ] = max ( suff[ i ][ j ] , suff[ i ][ j + 1 ] ) ;
}
}
for ( int i = 0 ; i < 10 ; ++ i ) {
for ( int j = 0 ; j < 10 ; ++ j ) {
for ( int t = 0 ; t < 10 ; ++ t ) {
dp[ i ][ j ][ t ] = { 0 , 0 } ;
}
}
}
int ans = pref[ n ][ 9 ] , l = 1 , r = 1 ;
for ( int i = n ; i >= 1 ; -- i ) {
int x = ( a[ i ] - '0' ) ;
for ( int st = 0 ; st <= x ; ++ st ) {
for ( int en = x ; en < 10 ; ++ en ) {
pii mx ;
for ( int mid = st ; mid <= x ; ++ mid ) {
mx = max ( mx , dp[ st ][ en ][ mid ] ) ;
}
if ( mx.fi == 0 ) { continue ; }
dp[ st ][ en ][ x ] = { mx.fi + 1 , mx.se } ;
if ( ans < mx.fi + 1 + pref[ i - 1 ][ st ] ) {
ans = mx.fi + 1 + pref[ i - 1 ][ st ] ;
l = i , r = mx.se ;
}
}
}
for ( int en = x ; en < 10 ; ++ en ) {
dp[ x ][ en ][ x ] = max ( dp[ x ][ en ][ x ] , make_pair ( suff[ i + 1 ][ en ] + 1 , i ) ) ;
}
}
cout << ans << " " << l << " " << r << "\n" ;
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
详细
Test #1:
score: 100
Accepted
time: 35ms
memory: 11468kb
input:
100 564 3348010339062339968164170543732424036101480063385567796871300633327190066610177797479597931706239956680908434521050648311220502482402268964497684451036156162125014743550622955579737715237323508714467463047351532756224409691528642332476831673220319724437158730801472447651264708417183934709815...
output:
108 78 492 296 760 1295 1419 1 1585 92 46 403 888 745 1073 262 626 1239 338 190 982 258 950 1736 848 3 382 1804 43 1416 102 2 533 103 11 420 99 16 497 263 375 1196 1306 1422 1450 301 113 851 100 56 239 107 177 471 362 28 288 1108 1 1778 96 293 482 1094 538 1098 1039 1151 1156 706 533 1340 573 186 13...
result:
ok correct answer! (100 test cases)