Submission #93486

# Submission time Handle Problem Language Result Execution time Memory
93486 2019-01-08T18:44:51 Z jufreire Split the sequence (APIO14_sequence) C++11
100 / 100
1292 ms 85744 KB
#include <stdio.h>
#include <stdlib.h>

long long xf;

class solution
{
public:
  int lastcut; //"this" cut //**
  //int ncuts;
  //int cuts[200];
  //int prevcut;
  long long b; //valor anterior
  long long a; //soma do inicio ate o lastcut
               //  long long preva; //soma do zero ate o lastcut anterior
               //  long long melhor; //a partir de onde esta eh a melhor
  long long valueat(long long x) //valor no final se este fosse o ultimo corte
  {
    if (x < a) return -1;
    return a * (x - a) + b;
  }
  long long acima(solution nova)
  {
    long long num;
    long long den;
    num = a*a - nova.a*nova.a + nova.b - b;
    den = a - nova.a;

    if (den == 0) //retas paralelas
    {
      if (valueat(xf) >= nova.valueat(xf)) return xf+1;
      else return nova.a;
    }

    long long x = num/den;
    if (x<nova.a) return nova.a;
    if (valueat(x) < nova.valueat(x) ) return x;
    return x+1; //talvez tivesse arredondado para baixo
  }

  long long acima_bin(solution nova) // retorna A PARTIR DE ONDE a nova esta acima desta. Pode ser xf se nunca.
  {
    long long x0 = a;
    if (nova.a > x0) x0 = nova.a; //nao adianta comparar antes de comecar
    long long x1 = xf;
    long long m;
    if (valueat(xf)>nova.valueat(xf)) return xf + 1;
    if (valueat(x0)<nova.valueat(x0)) return x0; //solucao nova ja surge acima da antiga. o que retornar?
    do
    {
      m = (x0 + x1) / 2;
      if (valueat(m) > nova.valueat(m)) x0 = m;
      else x1 = m;
    } while (x1>x0 + 1);
    return x1;
  } //eh, aqui pode melhorar bastante. sao duas retas, nao preciso dessa busca binaria toda para saber onde se encontram.
};

int k;

/*
int compar(const void* p1, const void* p2) //dessa vez vou querer em ordem da menos para mais inclinada
{
solution * m1 = (solution *)p1;
solution * m2 = (solution *)p2;
long long a = m1->a - m2->a;
if (a>0) return 1;
if (a<0) return -1;
return 0;
}
*/

int tamanho[201];
solution  **s;

typedef struct anterior
{
  int cut;
  int prox;
} anterior;

//anterior ** pc; //isto vai guardar apenas o prevcut do solution

int ** pc2;

void tam(int vv)
{
  tamanho[vv] += 1000;
  s[vv] = (solution*)realloc(s[vv], tamanho[vv] * sizeof(solution));
//  pc[vv] = (anterior*)realloc(pc[vv], tamanho[vv] * sizeof(anterior));
}
void copysol(int vv, int to, int from)
{
  s[vv][to].lastcut = s[vv][from].lastcut; //**
//  s[vv][to].ncuts = s[vv][from].ncuts;
  s[vv][to].a = s[vv][from].a;
  s[vv][to].b = s[vv][from].b;
//  s[vv][to].prevcut = s[vv][from].prevcut;
//  pc[vv][to].cut = pc[vv][from].cut;
//  pc[vv][to].prox = pc[vv][from].prox;
}


int main()
{
  int n, i;
  long long * a;
  long long * b;

//  freopen("60","r",stdin);

  scanf("%d", &n);
  scanf("%d", &k);
  a = (long long *)malloc(n * sizeof(long long));
  for (i = 0; i<n; i++) scanf("%lld", &a[i]);
  b = (long long *)malloc(n * sizeof(long long));
  b[n - 1] = a[n - 1];
  for (i = n - 2; i >= 0; i--) b[i] = b[i + 1] + a[i];
  //b[i] guarda a soma de i ate o fim
  xf = b[0];

  s = (solution**)malloc((k + 1) * sizeof(solution*));
//  pc = (anterior**)malloc((k + 1) * sizeof(anterior*));
  pc2 = (int**)malloc((k + 1) * sizeof(int*));

  for (i = 0; i <= k; i++)
  {
//    pc[i] = 0;
    pc2[i] = (int*) malloc(n*sizeof(int));
    s[i] = 0;
    tamanho[i] = 1000;
    //tam(i);
  }
  tam(0);

  int nsols[201];
  nsols[0] = 1;
  for (i = 1; i <= k; i++) nsols[i] = 0;

//  s[0][0].lastcut = 0;
//  s[0][0].ncuts = 0;
  s[0][0].a = 0;
  s[0][0].b = 0;
//  s[0][0].prevcut = -1;
//  pc[0][0].prox = -1;
//  pc[0][0].cut = 0;

  int ki, si, ss, ci;
  long long x, t1, t2;
  for (ki = 1; ki <= k; ki++) //fazer o proximo corte
  {
    tam (ki);
    if (ki>1) free(s[ki-2]);
//    printf("debug k=%d\n",ki);
    si = 0; //primeira solucao anterior
    ss = nsols[ki - 1]; //quantidade delas
    for (ci = ki; ci<n; ci++) //tem que ter espaco no inicio para pelo menos ki-1 cortes
    {
      x = b[0] - b[ci]; //ponto onde tenho que analisar as solucoes anteriores
      while (si<ss - 1 && s[ki - 1][si + 1].lastcut < ci && s[ki - 1][si].valueat(x) <= s[ki - 1][si + 1].valueat(x)) si++; //se tudo der certo bastava um if, mas por via das duvidas
      //while (si<ss - 1 && pc[ki - 1][si + 1].cut < ci && s[ki - 1][si].valueat(x) <= s[ki - 1][si + 1].valueat(x)) si++; //se tudo der certo bastava um if, mas por via das duvidas
      s[ki][nsols[ki]].lastcut = ci; //**
//      s[ki][nsols[ki]].ncuts = ki;
      s[ki][nsols[ki]].a = x;
      s[ki][nsols[ki]].b = s[ki - 1][si].valueat(x);
      //for (i=0;i<ki-1;i++) s[ki][nsols[ki]].cuts[i] = s[ki - 1][si].cuts[i];
      //s[ki][nsols[ki]].cuts[i] = ci;
//      s[ki][nsols[ki]].prevcut = si; //na verdade nao eh o cut, eh a solucao que tem o cut, pra ler recursivamente.
//      pc[ki][nsols[ki]].prox = si;
//      pc[ki][nsols[ki]].cut = ci;
      pc2[ki][ci] = s[ki-1][si].lastcut;

      //esta eh a nova solucao proposta. agora tenho que decidir se ela sera incluida ou nao.
//      printf("debug: %lld x + %lld - ", s[ki][nsols[ki]].a, s[ki][nsols[ki]].b);
      if (nsols[ki] == 0 || s[ki][nsols[ki] - 1].acima(s[ki][nsols[ki]]) <= xf) //ta errado
      {
        //        printf("accepted\n");
        nsols[ki]++;
        if (nsols[ki] == tamanho[ki]) tam(ki);
        //foi inserida na ordem certa de coeficiente angular, nem preciso ordenar.
        //so preciso ver se tem alguem antes dela que foi toda coberta. 
        //e elas nao estao espalhadas. se alguem foi coberto, foram as ultimas.
        i = nsols[ki] - 1;
        do
        {
          if (i<2) break;
          t1 = s[ki][i - 1].acima(s[ki][i]); //i passa de i-1
          t2 = s[ki][i - 2].acima(s[ki][i - 1]); //i-1 passa de i-2
          if (t1>t2) break; //porque esse eh o certo, cruzar em ordem.
                            //          printf("  removing old solution %lld %lld\n", s[ki][i - 1].a, s[ki][i - 1].b);
          copysol(ki, i - 1, i);
          i--;
          nsols[ki]--;
        } while (1);
      }
      else
      {
        //        printf("rejected\n");
      }

    }
//    printf("%d\n",nsols[ki]);
  }

  //a que ganha eh a mais inclinada com k cortes no valor de xf
  printf("%lld\n", s[k][nsols[k] - 1].valueat(xf));
  //for (i=0;i<k;i++) printf("%d ",s[k][nsols[k] - 1].cuts[i]);
  int pr[201];
  int pr2[201];
  int p = nsols[k] - 1;
  
  /*
  for (i = k; i >= 1; i--)
  {
    pr[i] = s[i][p].lastcut;
    p = s[i][p].prevcut;
  }
  p = nsols[k] - 1;
  for (i = k; i >= 1; i--)
  {
    pr2[i] = pc[i][p].cut;
    p = pc[i][p].prox;
  }

  for (i = 1; i <= k; i++) printf("%d ", pr2[i]);
  printf("\n");
  */


  //tentando o novo
  p = s[k][nsols[k] - 1].lastcut;

  for (i = k; i >= 1; i--)
  {
    pr2[i] = p;
    p = pc2[i][p];
  }

  for (i = 1; i <= k; i++) printf("%d ", pr2[i]);
  printf("\n");



}

Compilation message

sequence.cpp: In function 'int main()':
sequence.cpp:208:7: warning: unused variable 'pr' [-Wunused-variable]
   int pr[201];
       ^~
sequence.cpp:112:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &n);
   ~~~~~^~~~~~~~~~
sequence.cpp:113:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &k);
   ~~~~~^~~~~~~~~~
sequence.cpp:115:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   for (i = 0; i<n; i++) scanf("%lld", &a[i]);
                         ~~~~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 380 KB contestant found the optimal answer: 108 == 108
2 Correct 1 ms 376 KB contestant found the optimal answer: 999 == 999
3 Correct 1 ms 256 KB contestant found the optimal answer: 0 == 0
4 Correct 1 ms 380 KB contestant found the optimal answer: 1542524 == 1542524
5 Correct 2 ms 380 KB contestant found the optimal answer: 4500000000 == 4500000000
6 Correct 1 ms 256 KB contestant found the optimal answer: 1 == 1
7 Correct 1 ms 256 KB contestant found the optimal answer: 1 == 1
8 Correct 2 ms 256 KB contestant found the optimal answer: 1 == 1
9 Correct 1 ms 376 KB contestant found the optimal answer: 100400096 == 100400096
10 Correct 1 ms 256 KB contestant found the optimal answer: 900320000 == 900320000
11 Correct 1 ms 376 KB contestant found the optimal answer: 3698080248 == 3698080248
12 Correct 1 ms 256 KB contestant found the optimal answer: 3200320000 == 3200320000
13 Correct 1 ms 256 KB contestant found the optimal answer: 140072 == 140072
14 Correct 2 ms 376 KB contestant found the optimal answer: 376041456 == 376041456
15 Correct 1 ms 256 KB contestant found the optimal answer: 805 == 805
16 Correct 1 ms 376 KB contestant found the optimal answer: 900189994 == 900189994
17 Correct 1 ms 376 KB contestant found the optimal answer: 999919994 == 999919994
# Verdict Execution time Memory Grader output
1 Correct 1 ms 380 KB contestant found the optimal answer: 1093956 == 1093956
2 Correct 2 ms 376 KB contestant found the optimal answer: 302460000 == 302460000
3 Correct 2 ms 376 KB contestant found the optimal answer: 122453454361 == 122453454361
4 Correct 2 ms 376 KB contestant found the optimal answer: 93663683509 == 93663683509
5 Correct 2 ms 376 KB contestant found the optimal answer: 1005304678 == 1005304678
6 Correct 2 ms 376 KB contestant found the optimal answer: 933702 == 933702
7 Correct 2 ms 376 KB contestant found the optimal answer: 25082842857 == 25082842857
8 Correct 2 ms 256 KB contestant found the optimal answer: 687136 == 687136
9 Correct 2 ms 376 KB contestant found the optimal answer: 27295930079 == 27295930079
10 Correct 2 ms 376 KB contestant found the optimal answer: 29000419931 == 29000419931
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 610590000 == 610590000
2 Correct 2 ms 256 KB contestant found the optimal answer: 311760000 == 311760000
3 Correct 3 ms 504 KB contestant found the optimal answer: 1989216017013 == 1989216017013
4 Correct 2 ms 256 KB contestant found the optimal answer: 1499437552673 == 1499437552673
5 Correct 3 ms 504 KB contestant found the optimal answer: 1019625819 == 1019625819
6 Correct 3 ms 504 KB contestant found the optimal answer: 107630884 == 107630884
7 Correct 3 ms 504 KB contestant found the optimal answer: 475357671774 == 475357671774
8 Correct 2 ms 376 KB contestant found the optimal answer: 193556962 == 193556962
9 Correct 2 ms 376 KB contestant found the optimal answer: 482389919803 == 482389919803
10 Correct 2 ms 376 KB contestant found the optimal answer: 490686959791 == 490686959791
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB contestant found the optimal answer: 21503404 == 21503404
2 Correct 2 ms 380 KB contestant found the optimal answer: 140412195 == 140412195
3 Correct 13 ms 1144 KB contestant found the optimal answer: 49729674225461 == 49729674225461
4 Correct 2 ms 376 KB contestant found the optimal answer: 37485571387523 == 37485571387523
5 Correct 12 ms 1144 KB contestant found the optimal answer: 679388326 == 679388326
6 Correct 11 ms 1016 KB contestant found the optimal answer: 4699030287 == 4699030287
7 Correct 14 ms 1144 KB contestant found the optimal answer: 12418819758185 == 12418819758185
8 Correct 12 ms 1144 KB contestant found the optimal answer: 31093317350 == 31093317350
9 Correct 4 ms 504 KB contestant found the optimal answer: 12194625429236 == 12194625429236
10 Correct 6 ms 632 KB contestant found the optimal answer: 12345131038664 == 12345131038664
# Verdict Execution time Memory Grader output
1 Correct 4 ms 1048 KB contestant found the optimal answer: 1818678304 == 1818678304
2 Correct 3 ms 636 KB contestant found the optimal answer: 1326260195 == 1326260195
3 Correct 127 ms 8912 KB contestant found the optimal answer: 4973126687469639 == 4973126687469639
4 Correct 4 ms 1016 KB contestant found the optimal answer: 3748491676694116 == 3748491676694116
5 Correct 78 ms 5628 KB contestant found the optimal answer: 1085432199 == 1085432199
6 Correct 93 ms 6384 KB contestant found the optimal answer: 514790755404 == 514790755404
7 Correct 106 ms 7024 KB contestant found the optimal answer: 1256105310476641 == 1256105310476641
8 Correct 81 ms 5876 KB contestant found the optimal answer: 3099592898816 == 3099592898816
9 Correct 87 ms 6436 KB contestant found the optimal answer: 1241131419367412 == 1241131419367412
10 Correct 111 ms 7960 KB contestant found the optimal answer: 1243084101967798 == 1243084101967798
# Verdict Execution time Memory Grader output
1 Correct 29 ms 6172 KB contestant found the optimal answer: 19795776960 == 19795776960
2 Correct 27 ms 6588 KB contestant found the optimal answer: 19874432173 == 19874432173
3 Correct 1292 ms 85744 KB contestant found the optimal answer: 497313449256899208 == 497313449256899208
4 Correct 30 ms 6864 KB contestant found the optimal answer: 374850090734572421 == 374850090734572421
5 Correct 1277 ms 84684 KB contestant found the optimal answer: 36183271951 == 36183271951
6 Correct 951 ms 60836 KB contestant found the optimal answer: 51629847150471 == 51629847150471
7 Correct 1105 ms 65944 KB contestant found the optimal answer: 124074747024496432 == 124074747024496432
8 Correct 878 ms 54704 KB contestant found the optimal answer: 309959349080800 == 309959349080800
9 Correct 896 ms 61996 KB contestant found the optimal answer: 124113525649823701 == 124113525649823701
10 Correct 1120 ms 77848 KB contestant found the optimal answer: 124309619349406845 == 124309619349406845