結果

問題 No.335 門松宝くじ
ユーザー parukiparuki
提出日時 2016-06-21 08:34:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 3,486 bytes
コンパイル時間 1,860 ms
コンパイル使用メモリ 176,216 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 23:33:53
合計ジャッジ時間 2,867 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 15 ms
5,376 KB
testcase_06 AC 21 ms
5,376 KB
testcase_07 AC 35 ms
5,376 KB
testcase_08 AC 28 ms
5,376 KB
testcase_09 AC 48 ms
5,376 KB
testcase_10 AC 48 ms
5,376 KB
testcase_11 AC 47 ms
5,376 KB
testcase_12 AC 48 ms
6,940 KB
testcase_13 AC 46 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define FOR(i,j,k) for(int (i)=(j);(i)<(int)(k);++(i))
#define rep(i,j) FOR(i,0,j)
#define each(x,y) for(auto &(x):(y))
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define debug(x) cout<<#x<<": "<<(x)<<endl
#define smax(x,y) (x)=max((x),(y))
#define smin(x,y) (x)=min((x),(y))
#define MEM(x,y) memset((x),(y),sizeof (x))
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;

template<class Cmp=less_equal<int>>
class SparseTableRMQ{
public:
    SparseTableRMQ(){
    }
    SparseTableRMQ(int *ar, int n): n(n), data(n){
        for(int i = 0; i < n;++i) data[i] = ar[i];
        table = buildRMQ();
    }

    int queryPos(int l, int r){
        int k = lgInt(r - l - 1);
        int lid = table[l + n*k], rid = table[r + n*k - (1 << k)];
        return (cmp(data[lid], data[rid])) ? lid : rid;
    }
    
    int queryValue(int l, int r){
        return data[queryPos(l, r)];
    }
private:
    int n;
    vector<int> data, table;
    Cmp cmp;
    int lgInt(int v){
        static const unsigned b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000};
        static const unsigned S[] = {1, 2, 4, 8, 16};
        int res = 0;
        for (int i = 4; i >= 0; i--) {
            if (v & b[i]) {
                v >>= S[i];
                res |= S[i];
            } 
        }
        return res;
    }

    vector<int> buildRMQ() {
        int logn = 1;
        for (int k = 1; k < n; k *= 2) ++logn;
        vector<int> b(n*logn);
        for(int i = 0; i < n; ++i)b[i] = i;
        int cur = 0;
        for(int k = 1; k < n; k *= 2){
            copy(b.begin() + cur, b.begin() + cur + n, b.begin() + cur + n);
            cur += n;
            for(int i = 0; i < n - k; ++i){
                int idl = b[cur + i], idr = b[cur + i + k];
                if(cmp(data[idl], data[idr])){
                    b[cur + i] = idl;
                } else{
                    b[cur + i] = idr;
                }
            }
        }
        return b;
    }
};
typedef SparseTableRMQ<less_equal<int>> RMinQ;
typedef SparseTableRMQ<greater_equal<int>> RMaxQ;

RMinQ rmq;
RMaxQ rMq;
int N, M, E[800];

int pt(int a, int b, int c){
    a = E[a];
    b = E[b];
    c = E[c];
    if(a<b&&b>c)return b;
    if(a > b && b < c)return max(a, c);
    return 0;
}

int calc(int x, int y){
    assert(x < y);
    // x , z , y
    // x < z > y
    int z, res = 0;
    if(x + 1 < y){
        z = rMq.queryPos(x + 1, y);
        smax(res, pt(x, z, y));

        // x> z < y
        z = rmq.queryPos(x + 1, y);
        smax(res, pt(x, z, y));
    }

    // z, x, y
    // z < x > y
    if(x > 0){
        z = rmq.queryPos(0, x);
        smax(res, pt(z, x, y));

        // z > x < y
        z = rMq.queryPos(0, x);
        smax(res, pt(z, x, y));
    }

    // x, y, z
    // x < y > z
    if(y < N - 1){
        z = rmq.queryPos(y + 1, N);
        smax(res, pt(x, y, z));

        // x > y < z
        z = rMq.queryPos(y + 1, N);
        smax(res, pt(x, y, z));
    }
    return res;
}


int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> M;
    vector<pair<ll, int>> vp;
    rep(i, M){
        rep(j, N)cin >> E[j];
        rmq = RMinQ(E, N);
        rMq = RMaxQ(E, N);
        ll tot = 0;
        rep(x, N)FOR(y, x + 1, N){
            tot += calc(x, y);
        }
        vp.emplace_back(-tot, i);
    }
    sort(all(vp));
    cout << vp[0].second << endl;
}
0