結果

問題 No.335 門松宝くじ
ユーザー ctyl_0ctyl_0
提出日時 2016-01-28 19:56:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 421 ms / 2,000 ms
コード長 4,064 bytes
コンパイル時間 1,199 ms
コンパイル使用メモリ 100,980 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:31:54
合計ジャッジ時間 4,823 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 103 ms
4,348 KB
testcase_06 AC 154 ms
4,348 KB
testcase_07 AC 281 ms
4,348 KB
testcase_08 AC 277 ms
4,348 KB
testcase_09 AC 421 ms
4,348 KB
testcase_10 AC 419 ms
4,348 KB
testcase_11 AC 420 ms
4,348 KB
testcase_12 AC 421 ms
4,348 KB
testcase_13 AC 421 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define var auto
#define mod 1000000007
#define inf 2147483647
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;

template <typename T>
inline void output(T a, int p) {
    if(p){
        cout << fixed << setprecision(p)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

bool isKadomatsu(int a, int b, int c){
    if ((a < b && b > c) || (a > b && b < c)) return true;
    return false;
}

class rmq {
private:
    int n;
    int sz;
public:
    vector<ll> v, w; // v: min w: max
    rmq(int a){
        n = 1;
        for(; n * 2 < a;){
            n *= 2;
        }
        sz = n * 2;
        v.resize(4 * n - 1, inf);
        w.resize(4 * n - 1, -1);
    }
    
    void update(int i, ll x){
        i += sz - 1;
        v[i] = x;
        w[i] = x;
        while(i > 0){
            i = (i - 1) / 2;
            v[i] = min(v[2 * i + 1], v[2 * i + 2]);
            w[i] = max(w[2 * i + 1], w[2 * i + 2]);
        }
    }
    
    ll query_min_(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return inf;
        if(a <= l && r <= b) return v[k];
        else{
            ll vl = query_min_(a, b, k * 2 + 1, l, (l + r) / 2);
            ll vr = query_min_(a, b, k * 2 + 2, (l + r) / 2, r);
            return min(vl, vr);
        }
    }
    
    ll query_max_(int a, int b, int k, int l, int r){
        if(r <= a || b <= l) return -1;
        if(a <= l && r <= b) return w[k];
        else{
            ll wl = query_max_(a, b, k * 2 + 1, l, (l + r) / 2);
            ll wr = query_max_(a, b, k * 2 + 2, (l + r) / 2, r);
            return max(wl, wr);
        }
    }

    
    ll query_min(int a, int b){
        return query_min_(a, b, 0, 0, sz);
    }
    
    ll query_max(int a, int b){
        return query_max_(a, b, 0, 0, sz);
    }
};

// end of template

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    
    int N, M;
    cin >> N >> M;
    vector<int> ret(M, 0);
    rep(i, M){
        vector<int> E(N);
        rmq r(N);
        rep(j, N){
            int e;
            cin >> e;
            r.update(j, e);
            E[j] = e;
        }
        
        rep(j, N){
            repd(k, j + 1, N){
                int a = E[j], b = E[k];
                if (a == b) continue;
                int r1max = -1, r1min = inf, r2max = -1, r2min = inf, r3max = -1, r3min = inf;
                if (j != 0) {
                    r1max = (int)r.query_max(0, j);
                    r1min = (int)r.query_min(0, j);
                }
                if (j + 1 != k) {
                    r2max = (int)r.query_max(j + 1, k);
                    r2min = (int)r.query_min(j + 1, k);
                }
                if (k != N - 1) {
                    r3max = (int)r.query_max(k + 1, N);
                    r3min = (int)r.query_min(k + 1, N);
                }
                
                int tmp = 0;
                
                if (r2max > a && r2max > b) tmp = max(tmp, r2max);
                if (r2min < a && r2min < b) tmp = max(tmp, max(a, b));
                
                if (a > b) {
                    if (r1min < a) tmp = max(tmp, a);
                    if (r3max > b) tmp = max(tmp, max(r3max, a));
                }
                if (a < b) {
                    if (r1max > a) tmp = max(tmp, max(r1max, b));
                    if (r3min < b) tmp = max(tmp, b);
                }
                ret[i] += tmp;
                //cout << j << ", " << k << ": " << tmp << endl;
                
            }
        }
    }
    
    int r = 0;
    int sum = 0;
    rep(i, M){
        if (ret[i] > sum) {
            sum = ret[i];
            r = i;
        }
    }
    
    output(r, 0);
    
    return 0;
}
0