結果

問題 No.165 四角で囲え!
ユーザー codershifthcodershifth
提出日時 2015-12-29 12:11:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 683 ms / 5,000 ms
コード長 3,024 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 171,636 KB
実行使用メモリ 8,576 KB
最終ジャッジ日時 2023-08-26 23:46:07
合計ジャッジ時間 8,703 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 345 ms
6,596 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 458 ms
7,124 KB
testcase_06 AC 69 ms
4,704 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 186 ms
5,652 KB
testcase_12 AC 77 ms
4,716 KB
testcase_13 AC 69 ms
4,700 KB
testcase_14 AC 50 ms
4,380 KB
testcase_15 AC 57 ms
4,412 KB
testcase_16 AC 683 ms
8,268 KB
testcase_17 AC 637 ms
8,380 KB
testcase_18 AC 575 ms
8,576 KB
testcase_19 AC 676 ms
8,412 KB
testcase_20 AC 654 ms
8,488 KB
testcase_21 AC 651 ms
8,560 KB
testcase_22 AC 652 ms
8,496 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;

pair<ll,ll> operator+(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return make_pair(a.first+b.first, a.second+b.second);
}
pair<ll,ll> operator-(const pair<ll,ll> &a, const pair<ll,ll> &b)
{
    return make_pair(a.first-b.first, a.second-b.second);
}
class SurroundWithSquare {
public:
    void dump(const pair<ll,ll> &a) {
        cerr<<a.first<<","<<a.second<<endl;
    }
    void solve(void)
    {
        int N,B;
        cin>>N>>B;

        // 座標圧縮を使う
        vector<ll> xs;
        vector<ll> ys;
        vector<tuple<ll,ll,ll>> ps;
        REP(i,N)
        {
            ll x,y,p;
            cin>>x>>y>>p;
            ps.emplace_back(x,y,p);
            xs.push_back(x);
            ys.push_back(y);
        }
        sort(RANGE(xs));
        sort(RANGE(ys));

        map<ll,int> xmap;
        map<ll,int> ymap;
        REP(i,N)
        {
            xmap.emplace(xs[i],xmap.size());
            ymap.emplace(ys[i],ymap.size());
        }
        const int maxX = xmap.size();
        const int maxY = ymap.size();

        // 0<=x,y<400
        pair<ll,ll> field[maxY][maxX];
        REP(y,maxY) REP(x,maxX) field[y][x] = make_pair(0,0);

        REP(i,N)
        {
            ll x,y,p;
            tie(x,y,p) = ps[i];
            field[ymap[y]][xmap[x]] = make_pair(p,1);
        }
        pair<ll,ll> Sum[maxY+1][maxX+1];

        // 累積和を計算する(レンジは半開区間)
        REP(y,maxY)
        REP(x,maxX)
            Sum[y+1][x+1] = field[y][x];
        REP(y,maxY+1)
        REP(x,maxX)
        {
            Sum[y][x+1] = Sum[y][x+1] + Sum[y][x];
        }
        REP(y,maxY)
        REP(x,maxX+1)
        {
            Sum[y+1][x] = Sum[y+1][x] + Sum[y][x];
        }

        // 二分探索を使って高速化する O(N^3*log(N))
        ll maxN = 0;
        REP(uy,maxY+1)
        REP(ly,uy+1)
        REP(lx,maxX+1)
        {
            int low = 0;
            int high = maxX-lx+1;
            ll  n    = 0;

            // ly,uy,lx を固定したとき cnt は ux に対して単調増加なので二分探索できる
            while (low+1 < high)
            {
                int mid = (low+high)/2;
                int ux = lx + mid;
                pair<ll,ll> cnt(0,0);

                cnt = Sum[uy][ux] - Sum[uy][lx] - Sum[ly][ux] + Sum[ly][lx];
                if (cnt.first <= B)
                {
                    low = mid;
                    n = cnt.second;
                }
                else
                    high = mid;
            }
            maxN = max(maxN, n);
        }
        cout<<maxN<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new SurroundWithSquare();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0