This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define DIMN 1000010
using namespace std;
int dp[DIMN];
int mp[DIMN];
int tt[DIMN];
struct idk{
int ech , ok , sz , fth;
} dp2[5010];
int w[DIMN] , poz[DIMN];
pair <int , int> v[DIMN];
FILE *fin = stdin;
FILE *fout = stdout;
int ok (int maxi , int n , int ok){
int now , a , b , st , dr , mid;
now = n;
while (now){ /// nu pare o idee buna ce fac....
a = max(0 , now - maxi); /// ma asigur ca v2 nu e -1
/// cea mai mica echipa posibila ar fi de v[now].first
b = now - v[now].first;
while (tt[b] == -1)
b--;
/// a <= b
if (a > b)
swap(a , b);
a = poz[a];
b = poz[b];
st = a;
dr = b;
while (st <= dr){
mid = (st + dr)/2;
if (dp[w[mid]] + 1 == dp[now])
dr = mid - 1;
else st = mid + 1;
}
if (ok){
fprintf (fout,"%d " , now - w[st]);
}
if (now - w[st] > maxi)
return 0;
while (now > w[st]){
if (ok){
fprintf (fout,"%d ",v[now].second);
//printf ("%d ",v[now].first);
}
now--;
}
if (ok){
fprintf (fout,"\n");
//printf ("\n");
}
}
return 1;
}
int main()
{
int n , i , st , dr , mid , elem , j , now;
fscanf (fin,"%d",&n);
for (i = 1 ; i <= n ; i++){
fscanf (fin,"%d",&v[i].first);
v[i].second = i;
}
sort (v + 1 , v + n + 1);
if (n <= 5000){
dp2[0].ok = 1;
for (i = 1 ; i <= n ; i++){
for (j = i - v[i].first ; j >= 0 ; j--){
if (!dp2[j].ok)
continue;
if (dp2[j].ech + 1 > dp2[i].ech){
dp2[i].ech = dp2[j].ech + 1;
dp2[i].ok = 1;
dp2[i].sz = max(i - j , dp2[j].sz);
dp2[i].fth = j;
}
else if (dp2[j].ech + 1 == dp2[i].ech && max(i - j , dp2[j].sz) < dp2[i].sz){
dp2[i].ech = dp2[j].ech + 1;
dp2[i].ok = 1;
dp2[i].sz = max(i - j , dp2[j].sz);
dp2[i].fth = j;
}
}
}
fprintf (fout,"%d\n" , dp2[n].ech);
now = n;
while (now){
fprintf (fout,"%d ", now - dp2[now].fth);
for (i = now ; i > dp2[now].fth ; i--)
fprintf (fout,"%d ",v[i].second);
fprintf (fout,"\n");
now = dp2[now].fth;
}
return 0;
}
elem = 0;
for (i = 1 ; i <= n ; i++){
if (i - v[i].first < 0){
dp[i] = -2000000000;
mp[i] = mp[i - 1];
tt[i] = -1;
}
else {
dp[i] = mp[i - v[i].first] + 1;
w[++elem] = i;
poz[i] = elem;
tt[i] = i - v[i].first;
mp[i] = max(mp[i - 1] , dp[i]);
}
}
fprintf (fout,"%d\n" , dp[n]);
st = v[n].first;
dr = n;
while (st <= dr){
mid = (st + dr)/2;
if (ok(mid , n , 0))
dr = mid - 1; /// merge, cauti mai mic
else st = mid + 1;
}
/// solutia e st (dim minima a echipei maxime)
//printf ("%d ",st);
ok(st , n , 1);
return 0;
}
Compilation message (stderr)
tea.cpp: In function 'int main()':
tea.cpp:84:12: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
84 | fscanf (fin,"%d",&n);
| ~~~~~~~^~~~~~~~~~~~~
tea.cpp:86:16: warning: ignoring return value of 'int fscanf(FILE*, const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
86 | fscanf (fin,"%d",&v[i].first);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |