結果

問題 No.335 門松宝くじ
ユーザー cormorancormoran
提出日時 2016-02-12 02:05:03
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 4,126 bytes
コンパイル時間 1,105 ms
コンパイル使用メモリ 82,020 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 02:25:38
合計ジャッジ時間 6,166 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 430 ms
4,348 KB
testcase_08 AC 417 ms
4,348 KB
testcase_09 WA -
testcase_10 AC 645 ms
4,348 KB
testcase_11 AC 644 ms
4,348 KB
testcase_12 AC 645 ms
4,348 KB
testcase_13 AC 646 ms
4,348 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘bool solve()’:
main.cpp:142:13: warning: ‘ans’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  142 |     cout << ans << endl;
      |             ^~~

ソースコード

diff #

#include<iostream>
#include<vector>
#include<cassert>
#include<algorithm>
using namespace std;

typedef pair<int,int> pii;
typedef long long ll;
typedef ll int__;
#define rep(i,j) for(int__ i=0;i<(int__)(j);i++)
#define repeat(i,j,k) for(int__ i=(j);i<(int__)(k);i++)
#define all(v) v.begin(),v.end()

template<typename T>
ostream& operator << (ostream &os , const vector<T> &v){
    rep(i,v.size()) os << v[i] << (i!=v.size()-1 ? " " : "\n"); return os;
}

template<typename T>
istream& operator >> (istream &is , vector<T> &v){
    rep(i,v.size()) is >> v[i]; return is;
}

#ifdef DEBUG
void debug(){ cerr << endl; }
#endif
template<class F,class...R>
void debug(const F &car,const R&... cdr){
#ifdef DEBUG
    cerr << car << " "; debug(cdr...);
#endif
}

#include<functional>
using Index = int;
const int INF = 1 << 30;
template<typename Data>
class RangeQuery{
  public:
    int n;
    vector<Data> data;
    function< Data(Data,Data) > selector;
    Data default_val;    
    RangeQuery(int n, function< Data(Data,Data) > selector,Data default_val)
            :selector(selector),default_val(default_val){
        this->n = 1;
        while(this->n < n) this->n *= 2;
        data.resize(2*(this->n)-1, default_val);
    }
    void update(const vector<Data> &v){
        assert(v.size() <= n);
        rep(i,v.size()) update(i,v[i]);
    }
    //index k(=0,1,2...)の値をaにする。
    void update(Index k,Data val){
        assert(0<=k and k<n);
        k += n-1;
        data[k] = val;
        while( k > 0 ){
            k = (k-1)/2; //親
            data[k] = selector(data[k*2+1], data[k*2+2]);
        }
    }
    // [a,b) のクエリ b<=n
    int query(Index a,Index b){
        assert(0<=a and b<=n);
        return search(a,b,0,0,n);
    }
  private:
    Data search(Index a,Index b,Index k,Index l,Index r){
        if( r <= a or b <= l ) return default_val;
        if( a <= l and r <= b ) return data[k];
        else {
            Data vl = search(a, b, k*2+1, l, (l+r)/2);
            Data vr = search(a, b, k*2+2, (l+r)/2, r);
            return selector(vl, vr);
        }
    }
};

bool is_matu(int a, int b, int c){
    if(a == b or a == c or b == c) return false;
    return not (a < b and b < c) and not (c < b and b < a);
}

int max(int a, int b, int c){
    return max(a, max(b,c));
}

ll value_sum(vector<int> E){
    int N = E.size();
    RangeQuery<int> rminq = RangeQuery<int>(N,[](int a,int b){return min(a,b);},INF);
    RangeQuery<int> rmaxq = RangeQuery<int>(N,[](int a,int b){return max(a,b);},-INF);
    rminq.update(E);
    rmaxq.update(E);
    
    ll sum = 0;
    rep(i,N){
        rep(j,i){
            int max_value = -1;
            if( 0 < j ){
                int b = E[j], c = E[i];
                int max_a = rmaxq.query(0, j);
                int min_a = rminq.query(0, j);
                if( is_matu(max_a, b, c) ) max_value = max(max_a, b, c);
                if( is_matu(min_a, b, c) ) max_value = max(min_a, b, c);
            }
            if( j +1 < i ){
                int a = E[j], c = E[i];
                int max_b = rmaxq.query(j+1, i);
                int min_b = rminq.query(j+1, i);
                if( is_matu(a, max_b, c) ) max_value = max(a, max_b, c);
                if( is_matu(a, min_b, c) ) max_value = max(a, min_b, c);
            }
            if(i + 1 < N){
                int a = E[j], b = E[i];
                int max_c = rmaxq.query(i+1, N);
                int min_c = rminq.query(i+1, N);
                if( is_matu(a, b, max_c) ) max_value = max(a, b, max_c);
                if( is_matu(a, b, min_c) ) max_value = max(a, b, min_c);
            }
            sum += max_value;
        }
    }
    return sum;
}

bool solve(){
    int N,M; cin >> N >> M;
    vector<vector<int>> E(M, vector<int>(N));
    rep(i,M) cin >> E[i];
    
    int ans;
    ll max_sum = -1;
    rep(i,M){
        ll tmp = value_sum(E[i]);
        if(tmp > max_sum){
            ans = i;
            max_sum = tmp;
        }
    }
    cout << ans << endl;
    return false;
}

int main(){
    ios::sync_with_stdio(false);
    while(solve());
    return 0;
}
0