QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#567652 | #9313. Make Max | ksc | TL | 57ms | 52812kb | Java11 | 3.3kb | 2024-09-16 13:18:40 | 2024-09-16 13:18:41 |
Judging History
answer
import java.util.*;
import java.io.*;
public class Main
{
// static Scanner sc = new Scanner( System.in );
public static void main( String[] ksc ) throws Exception
{
int t = nextInt();
while( t-- > 0 ) solve();
}
static long max, sum;
static ArrayList< r > arr;
static ArrayList< Integer > max_q;
private static void solve() throws Exception
{
int n = nextInt();
arr = new ArrayList<>();
max_q = new ArrayList<>();
long cnt = 0, val = 0;
sum = 0;
max = 0;
int t = 0;
for( int i = 0; i < n; i++ )
{
long key = nextLong();
if( val != key )
{
if( cnt != 0 )
{
get_max( val, t );
arr.add( new r( val, cnt, t++ ) );
}
val = key;
cnt = 1;
} else cnt++;
}
get_max( val, t );
arr.add( new r( val, cnt, t ) );
Collections.sort( arr );
for( int i = 0; i < max_q.size(); i++ )
{
int l = i == 0 ? 0 : max_q.get( i - 1 );
int r = i == max_q.size() - 1 ? arr.size() - 1 : max_q.get( i );
find( 1, l, r, 0, max );
}
pw.println( sum );
pw.flush();
}
private static void get_max( long val, int t )
{
if( val > max )
{
max = val;
max_q = new ArrayList<>();
}
if( val == max ) max_q.add( t );
}
private static void find( int cnt, int l, int r, int maxi_now, long max_now )
{
if( l > r || maxi_now >= arr.size() ) return;
if( l < 0 || r >= arr.size() ) return;
while( arr.get( maxi_now ).v == max_now )
{
maxi_now++;
if( maxi_now >= arr.size() ) return;
}
while( !( arr.get( maxi_now ).i <= r && arr.get( maxi_now ).i >= l ) )
{
maxi_now++;
if( maxi_now >= arr.size() ) return;
}
sum += cnt * arr.get( maxi_now ).c;
find( cnt + 1, l, arr.get( maxi_now ).i - 1, maxi_now + 1, arr.get( maxi_now ).v );
find( cnt + 1, arr.get( maxi_now ).i + 1, r, maxi_now + 1, arr.get( maxi_now ).v );
}
static class r implements Comparable< r >
{
long v, c;
int i;
r( long v, long c, int i )
{
this.c = c;
this.v = v;
this.i = i;
}
@Override
public int compareTo( r o )
{
return (int) ( ( o.v - this.v ) % 195329 );
}
}
static BufferedReader bf = new BufferedReader( new InputStreamReader( System.in ) );
static StreamTokenizer st = new StreamTokenizer( bf );
static PrintWriter pw = new PrintWriter( new OutputStreamWriter( System.out ) );
private static int nextInt() throws IOException
{
st.nextToken();
return (int) st.nval;
}
private static long nextLong() throws IOException
{
st.nextToken();
return (long) st.nval;
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 57ms
memory: 52812kb
input:
4 2 1 2 2 2 2 7 1 1 1 2 2 2 2 3 1 2 3
output:
1 0 3 3
result:
ok 4 number(s): "1 0 3 3"
Test #2:
score: -100
Time Limit Exceeded
input:
2 198018 875421126 585870339 471894633 383529988 625397685 944061047 704695631 105113224 459022561 760848605 980735314 847376362 980571959 329939331 644635272 326439858 752879510 837384394 175179068 182094523 397239381 1199016 185143405 279638454 252374970 822030887 860312140 137248166 993229443 164...