import java.io.*;
import java.util.*;
class tea{
static int INF=(int)1e9;
static Integer[]indices;
static int []cand;
static int n,a[];
static int ceiling(int x,int lo,int hi) {
int ans=-1;
while(lo<=hi) {
int mid=lo+hi>>1;
int idx=cand[mid];
if(idx>=x) {
ans=idx;
lo=mid+1;
}
else
hi=mid-1;
}
return ans;
}
static int[][] check(int limit) {
int []dp=new int [n+1]; //number of teams
int []nxt=new int [n+1];
Deque<Integer> queue=new LinkedList();
int lo=0;
int size=0;
cand[size++]=n;
queue.add(n);
for(int i=n-1;i>=0;i--) {
int x=a[indices[i]];
if(i+x>n)
continue;
while(!queue.isEmpty()) {
int j=queue.peekFirst();
if(j-i>limit) {
queue.removeFirst();
lo++;
}
else
break;
}
int first=ceiling(x+i,lo,size-1);
if(first==-1)
continue;
dp[i]=dp[first]+1;
nxt[i]=first;
if(dp[i]>=dp[queue.peekLast()]) {
queue.addLast(i);
cand[size++]=i;
}
}
int [][]ans=new int [2][n];
ans[0]=dp;
ans[1]=nxt;
return ans;
}
public static void main(String[] args) throws IOException {
InputReader sc=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
n=sc.nextInt();
indices=new Integer[n];
a=new int [n];
cand=new int [n+1];
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
indices[i]=i;
}
Arrays.sort(indices,Comparator.comparingInt(i->-a[i]));
int [][]x;
x=check(n);
int maxTeams=x[0][0];
int best=-1;
int lo=1,hi=n;
while(lo<=hi) {
int mid=lo+hi>>1;
x=check(mid);
if(x[0][0]==maxTeams) {
hi=mid-1;
best=mid;
}
else
lo=mid+1;
}
x=check(best);
int []dp=x[0];
int []nxt=x[1];
out.println(dp[0]);
Queue list=new Queue();
for(int i=0;i<n;) {
//
int j=nxt[i];
while(i<j) {
list.add(indices[i]+1);
i++;
}
out.print(list.size);
while(!list.isEmpty())
out.print(" "+list.poll());
out.println();
}
out.close();
}
static class Queue
{
int back,front;
int []a;
int size=0;
public Queue()
{
a=new int [n];
}
public int poll()
{
size--;
return a[back++];
}
public void add(int x)
{
size++;
a[front++]=x;
}
public boolean isEmpty()
{
return size==0;
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
Compilation message
Note: tea.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
15336 KB |
Output is correct |
2 |
Correct |
181 ms |
15532 KB |
Output is correct |
3 |
Correct |
277 ms |
15132 KB |
Output is correct |
4 |
Correct |
178 ms |
15296 KB |
Output is correct |
5 |
Correct |
169 ms |
15380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
15524 KB |
Output is correct |
2 |
Correct |
169 ms |
15048 KB |
Output is correct |
3 |
Correct |
173 ms |
15200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
15252 KB |
Output is correct |
2 |
Correct |
182 ms |
15536 KB |
Output is correct |
3 |
Correct |
185 ms |
15744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
19236 KB |
Output is correct |
2 |
Correct |
264 ms |
20376 KB |
Output is correct |
3 |
Correct |
261 ms |
20756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
20056 KB |
Output is correct |
2 |
Correct |
242 ms |
18968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
645 ms |
53144 KB |
Output is correct |
2 |
Correct |
650 ms |
57992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
698 ms |
56836 KB |
Output is correct |
2 |
Correct |
676 ms |
59120 KB |
Output is correct |
3 |
Correct |
808 ms |
58372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2609 ms |
130192 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2561 ms |
122576 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2534 ms |
117324 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |