Submission #366549

# Submission time Handle Problem Language Result Execution time Memory
366549 2021-02-14T12:58:04 Z BartolM Skyscraper (JOI16_skyscraper) C++17
100 / 100
332 ms 123276 KB
#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair <int, int> pii;
typedef pair <int, pii> pip;
typedef pair <pii, int> ppi;
typedef pair <ll, ll> pll;

const int INF=0x3f3f3f3f;
const int N=102;
const int M=1005;
const int MOD=1e9+7;

int n, m;
int p[N];
int dp[N][N][M][3];

int mul(int a, int b) {
    return (ll)a*b%MOD;
}

int add(int a, int b) {
    a+=b;
    if (a>=MOD) a-=MOD;
    return a;
}

int rek(int i, int komp, int br, int kr) {
    if (komp<=0) return 0;
    if (kr>2) return 0;
    if (br>m) return 0;
    if (i==n) return komp==1 && kr==2;

    int &ret=dp[i][komp][br][kr];
    if (ret!=-1) return ret;
    ret=0;

    int raz=p[i]-p[i-1];
    int nekr=komp-kr;
    ///1. slucaj -> kraj dodajem
    if (i!=n-1) {
        ///dodajem novu komponentu kraj
        ret=add(ret, mul(rek(i+1, komp+1, br+nekr*2*raz+kr*raz, kr+1), 1+!kr));
        ///kraj mrgam na neku komponentu
        ret=add(ret, mul(rek(i+1, komp, br+(nekr-1)*2*raz+(kr+1)*raz+raz, kr+1), nekr*(1+!kr)));
    }
    else if (i==n-1 && komp==1 && kr==1) ret=add(ret, rek(i+1, komp, br+raz, kr+1));

    ///2. slucaj -> nova komponenta
    ret=add(ret, rek(i+1, komp+1, br+nekr*2*raz+kr*raz, kr));

    ///3. slucaj -> dodajem na neku komponentu
    ret=add(ret, mul( rek(i+1, komp, br+(nekr-1)*2*raz+kr*raz+2*raz, kr) , nekr*2));
    ret=add(ret, mul( rek(i+1, komp, br+nekr*2*raz+kr*raz, kr), kr) );

    ///4.slucaj -> spajanje dvije komponente
    ///nijedna nije kraj
    if (nekr>=2) ret=add(ret, mul( rek(i+1, komp-1, br+(nekr-1)*2*raz+2*raz+kr*raz, kr) , nekr*(nekr-1)));

    ///jedna je kraj
    if (nekr>=1 && kr) ret=add(ret, mul( rek(i+1, komp-1, br+(nekr-1)*2*raz+2*raz+kr*raz, kr) , kr*nekr));

    ///obe su kraj
    if (kr==2 && i==n-1) ret=add(ret, rek(i+1, komp-1, br+nekr*2*raz+2*raz, kr));


//    printf("pos: %d, raz: %d, br: %d, komp: %d, kr: %d === %d\n", i, raz, br, komp, kr, ret);

    return ret;


}

void load() {
    scanf("%d %d", &n, &m);
    for (int i=0; i<n; ++i) scanf("%d", &p[i]);
    sort(p, p+n);
}

int main() {
    load();

    if (n==1) {
        printf("1\n");
        return 0;
    }

    memset(dp, -1, sizeof dp);

    int sol=rek(1, 1, 0, 0);
    sol=add(sol, mul(rek(1, 1, 0, 1), 2));

    printf("%d\n", sol);
    return 0;
}

/**
3 7
2 3 6

4 10
3 6 2 9

5 10000
1 2 3 4 5
**/

Compilation message

skyscraper.cpp: In function 'void load()':
skyscraper.cpp:81:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   81 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
skyscraper.cpp:82:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   82 |     for (int i=0; i<n; ++i) scanf("%d", &p[i]);
      |                             ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 67 ms 123116 KB Output is correct
3 Correct 67 ms 123244 KB Output is correct
4 Correct 67 ms 123028 KB Output is correct
5 Correct 66 ms 123160 KB Output is correct
6 Correct 77 ms 123116 KB Output is correct
7 Correct 66 ms 123116 KB Output is correct
8 Correct 66 ms 123116 KB Output is correct
9 Correct 78 ms 123116 KB Output is correct
10 Correct 68 ms 123116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 123148 KB Output is correct
2 Correct 66 ms 123092 KB Output is correct
3 Correct 69 ms 123116 KB Output is correct
4 Correct 69 ms 123116 KB Output is correct
5 Correct 67 ms 123116 KB Output is correct
6 Correct 67 ms 123116 KB Output is correct
7 Correct 66 ms 123136 KB Output is correct
8 Correct 69 ms 123116 KB Output is correct
9 Correct 69 ms 123116 KB Output is correct
10 Correct 75 ms 123032 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 67 ms 123116 KB Output is correct
3 Correct 67 ms 123244 KB Output is correct
4 Correct 67 ms 123028 KB Output is correct
5 Correct 66 ms 123160 KB Output is correct
6 Correct 77 ms 123116 KB Output is correct
7 Correct 66 ms 123116 KB Output is correct
8 Correct 66 ms 123116 KB Output is correct
9 Correct 78 ms 123116 KB Output is correct
10 Correct 68 ms 123116 KB Output is correct
11 Correct 66 ms 123148 KB Output is correct
12 Correct 66 ms 123092 KB Output is correct
13 Correct 69 ms 123116 KB Output is correct
14 Correct 69 ms 123116 KB Output is correct
15 Correct 67 ms 123116 KB Output is correct
16 Correct 67 ms 123116 KB Output is correct
17 Correct 66 ms 123136 KB Output is correct
18 Correct 69 ms 123116 KB Output is correct
19 Correct 69 ms 123116 KB Output is correct
20 Correct 75 ms 123032 KB Output is correct
21 Correct 67 ms 123116 KB Output is correct
22 Correct 332 ms 123244 KB Output is correct
23 Correct 167 ms 123116 KB Output is correct
24 Correct 173 ms 123244 KB Output is correct
25 Correct 178 ms 123276 KB Output is correct
26 Correct 158 ms 123116 KB Output is correct
27 Correct 113 ms 123116 KB Output is correct
28 Correct 138 ms 123116 KB Output is correct
29 Correct 210 ms 123140 KB Output is correct
30 Correct 169 ms 123116 KB Output is correct