Submission #1295429

#TimeUsernameProblemLanguageResultExecution timeMemory
1295429luattapcodeSkyscraper (JOI16_skyscraper)C++20
20 / 100
184 ms190456 KiB
/*The best way to get something done is to begin*/
#include     <bits/stdc++.h>
#define     ll long long
#define     int long long
#define     fr(i,a,b) for(int i=a;i<=b;i++)
#define     frd(i,a,b) for(int i=a;i>=b;i--)
#define     pb push_back
#define     fi first
#define     se second
#define     el '\n'
#define     BIT(x,i) (((x) >> (i)) & 1)
using namespace std;
template <class X, class Y> bool minimize(X &x, const Y &y) {if (x > y) {x = y;return true;} else return false;}
template <class X, class Y> bool maximize(X &x, const Y &y) {if (x < y) {x = y;return true;} else return false;}
/* Author: Huynh Quoc Luat */
/*Khai Bao*/
const long long inf=1e18;
const int oo=1e9;
const int LO=21;
const int CH=27;
const int sm=1e9+7;
void add(int &x,const int &y){x+=y;if(x>=sm)x-=sm;}
//void sub(int &x,const int &y){x-=y;if(x<0)x+=sm;}

const int N = 105;
int a[N];
int idx[N];
int n,L;

void read()
{
    cin >> n >> L;
    fr(i,1,n) cin >> a[i],idx[i] = a[i];
}

namespace sub1
{
    int p[N];
    void process()
    {   
        int res = 0;
        fr(i,1,n) p[i] = i;
        do{
            int cur = 0;
            fr(i,2,n) cur += abs(idx[p[i]] - idx[p[i-1]]);
            add(res,(cur <= L));
        }while(next_permutation(p+1,p+n+1));
        cout << res;
    }
}  

namespace sub2
{
    const int MASK = (1 << 14) + 1;
    const int N = 15;
    const int MAX = 101;
    int dp[MASK][N][MAX];
    void process(){
        int tt = (1<<n) - 1;
        fr(i,1,n) dp[1 << (i-1)][i][0] = 1;
        fr(mask,1,tt){
            fr(i,1,n){
                if(BIT(mask,(i-1))){
                    fr(j,1,n){
                        if(!BIT(mask,(j-1))){
                            int new_mask = mask ^ (1 << (j-1));
                            fr(cost,0,L){
                                if(cost + abs(a[i] - a[j]) > L) break;
                                add(dp[new_mask][j][cost + abs(a[i] - a[j])], dp[mask][i][cost]);
                            }
                        }
                    }
                }
            }
        }

        int res = 0;
        fr(i,1,n){
            fr(cost,0,L) add(res,dp[tt][i][cost]);
        }

        cout << res;
    }
} 
void time()    {cerr<< endl<<"Luattapcode: "<<clock()<<"ms"<<endl;}
main()
{
    ios_base::sync_with_stdio(NULL);cin.tie(NULL);cout.tie(NULL);
    #define TASK "qn"
    if(fopen(TASK".INP","r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".OUT", "w", stdout);
    }
    int mtt=0;
    int test=1;
    if(mtt) cin>>test;
    while(test--)
    {
        read();
        if(n <= 8 ) sub1::process();
        else sub2 :: process();
    }
    time();
    return 0;
}

Compilation message (stderr)

skyscraper.cpp:86:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   86 | main()
      | ^~~~
skyscraper.cpp: In function 'int main()':
skyscraper.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   92 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
skyscraper.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   93 |         freopen(TASK".OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...