Submission #680632

# Submission time Handle Problem Language Result Execution time Memory
680632 2023-01-11T11:40:16 Z vinnipuh01 JJOOII 2 (JOI20_ho_t2) C++17
100 / 100
23 ms 11620 KB
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
#define sqrt sqrtl

using namespace std;

const long long oo = 1000000000000000000;

long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;


/*
    ViHHiPuh

   (( `'-""``""-'` ))
     )-__-_.._-__-(
   / --- (o _ o) --- \
   \ .-* ( .0. ) *-. /
   _'-. ,_ '=' _, .-'_
  / `;#'#'# - #'#'#;` \
 \_)) -----'#'----- ((_/
      # --------- #
  '# ------- ------ #'
  /..-'# ------- #'-.\
  _\...-\'# -- #'/-.../_
  ((____)- '#' -(____))


    cout << fixed << setprecision(6) << x;

    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    freopen ( "sum.in", "r", stdin )
*/

int p[ 200001 ], sf[ 200001 ], mp[ 200001 ];
int pp[ 200001 ], sff[ 200001 ], mpp[ 200001 ];
int nxt[ 200001 ];

main () {
	int n, m;
	cin >> n >> m;
	string s;
	cin >> s;
	while ( s.size() && s.back() != 'I' )
		s.pop_back();
	reverse( s.begin(), s.end() );
	while ( s.size() && s.back() != 'J' )
		s.pop_back();
	reverse( s.begin(), s.end() );
	for ( int i = 0; i < s.size(); i ++ ) {
		if ( i )
			p[ i ] = p[ i - 1 ], sf[ i ] = sf[ i - 1 ], mp[ i ] = mp[ i - 1 ], pp[ i ] = pp[ i - 1 ], mpp[ i ] = mpp[ i - 1 ], sff[ i ] = sff[ i - 1 ];
		if ( s[ i ] == 'J' ) {
			p[ i ] ++;
			pp[  i] = i;
		}
		else if ( s[ i ] == 'O' )
			sf[ i ] ++, sff[ i ] = i;
		else
			mp[ i ] ++, mpp[ i ] = i;
	} 
	for ( int i = s.size() - 1; i >= 0; i -- ) {
		if ( s[ i ] == 'I' )
			nxt[ i ] = i;
		else
			nxt[ i ] = nxt[ i + 1 ];
	}
	int l, r, mid;
	ans = oo;
	for ( int i = 0; i < s.size(); i ++ ) {
		if ( s[ i ] != 'J' || p[ i ] < m )
			continue;
		l = 0;
		r = i;
		sum = 0;
		while ( r - l > 1 ) {
			mid = ( l + r ) / 2;
			if ( p[ i ] - p[ mid ] + 1 >= m )
				l = mid;
			else
				r = mid - 1;
		}
		if ( p[ i ] - p[ l ] + 1 < m )
			continue;
		if ( p[ i ] - p[ r ] + 1 >= m )
			l = r;
		l = pp[ l ];
		sum += i - l + 1 - m;
		l = i;
		r = s.size() - 1;
		while ( r > l ) {
			mid = ( l + r ) / 2;
			if ( sf[ mid ] - sf[ i ] >= m )
				r = mid;
			else
				l = mid + 1;
		}
		if ( sf[ l ] - sf[ i ] < m )
			continue;
		l = sff[ l ];
		sum += l - i - m;
		sum += nxt[ l ] - l - 1;
		pos = nxt[ l ];
		l = pos;
		r = s.size() - 1;
		while ( r > l ) {
			mid = ( l + r ) / 2;
			if ( mp[ mid ] - mp[ pos ] + 1 >= m )
				r = mid;
			else
				l = mid + 1; 
		}
		if ( mp[ l ] - mp[ pos ] + 1 < m )
			continue;
		l = mpp[ l ];
		sum += l - pos + 1 - m;
		ans = min( ans, sum );
	}
	if ( ans == oo )
		cout << "-1";
	else
		cout << ans;
}

Compilation message

ho_t2.cpp:51:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   51 | main () {
      | ^~~~
ho_t2.cpp: In function 'int main()':
ho_t2.cpp:62:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |  for ( int i = 0; i < s.size(); i ++ ) {
      |                   ~~^~~~~~~~~~
ho_t2.cpp:82:21: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |  for ( int i = 0; i < s.size(); i ++ ) {
      |                   ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 340 KB Output is correct
4 Correct 0 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 340 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 0 ms 468 KB Output is correct
16 Correct 1 ms 468 KB Output is correct
17 Correct 1 ms 468 KB Output is correct
18 Correct 0 ms 340 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 1 ms 468 KB Output is correct
21 Correct 1 ms 468 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 0 ms 340 KB Output is correct
24 Correct 1 ms 468 KB Output is correct
25 Correct 1 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 1 ms 468 KB Output is correct
28 Correct 1 ms 468 KB Output is correct
29 Correct 1 ms 468 KB Output is correct
30 Correct 1 ms 468 KB Output is correct
31 Correct 0 ms 340 KB Output is correct
32 Correct 1 ms 468 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 1 ms 468 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 16 ms 10724 KB Output is correct
37 Correct 17 ms 11524 KB Output is correct
38 Correct 18 ms 11588 KB Output is correct
39 Correct 21 ms 11492 KB Output is correct
40 Correct 18 ms 11492 KB Output is correct
41 Correct 21 ms 11504 KB Output is correct
42 Correct 23 ms 11600 KB Output is correct
43 Correct 10 ms 7312 KB Output is correct
44 Correct 13 ms 9088 KB Output is correct
45 Correct 17 ms 11492 KB Output is correct
46 Correct 20 ms 11512 KB Output is correct
47 Correct 19 ms 11492 KB Output is correct
48 Correct 16 ms 11516 KB Output is correct
49 Correct 11 ms 7780 KB Output is correct
50 Correct 17 ms 11492 KB Output is correct
51 Correct 19 ms 11620 KB Output is correct
52 Correct 14 ms 11108 KB Output is correct
53 Correct 12 ms 11492 KB Output is correct
54 Correct 18 ms 11492 KB Output is correct
55 Correct 14 ms 11496 KB Output is correct
56 Correct 10 ms 11516 KB Output is correct