結果

問題 No.783 門松計画
ユーザー maimai
提出日時 2017-12-24 11:55:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 7,606 bytes
コンパイル時間 4,586 ms
コンパイル使用メモリ 246,464 KB
実行使用メモリ 10,012 KB
最終ジャッジ日時 2024-05-09 23:44:58
合計ジャッジ時間 8,452 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
10,012 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In member function '{anonymous}::MaiScanner& {anonymous}::MaiScanner::operator>>(std::string&)':
main.cpp:55:9: warning: no return statement in function returning non-void [-Wreturn-type]
   55 |         }
      |         ^

ソースコード

diff #

#pragma GCC optimize ("O3")
#pragma GCC target ("avx")
#include "bits/stdc++.h" // define macro "/D__MAI"

using namespace std;
typedef long long int ll;

#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}}
#define ALL(v) (v).begin(),(v).end()
#define repeat(cnt,l) for(auto cnt=0ll;(cnt)<(l);++(cnt))
#define rrepeat(cnt,l) for(auto cnt=(l)-1;0<=(cnt);--(cnt))
#define iterate(cnt,b,e) for(auto cnt=(b);(cnt)!=(e);++(cnt))
#define MD 1000000007ll
#define PI 3.1415926535897932384626433832795
template<typename T1, typename T2> ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << "(" << p.first << ":" << p.second << ")"; return o; }
template<typename T> T& maxset(T& to, const T& val) { return to = max(to, val); }
template<typename T> T& minset(T& to, const T& val) { return to = min(to, val); }
void bye(string s, int code = 0) { cout << s << endl; exit(0); }
mt19937_64 randdev(8901016);
inline ll rand_range(ll l, ll h) {
    return uniform_int_distribution<ll>(l, h)(randdev);
}

#if defined(_WIN32) || defined(_WIN64)
#define getchar_unlocked _getchar_nolock
#define putchar_unlocked _putchar_nolock
#elif __GNUC__
#else
#define getchar_unlocked getchar
#define putchar_unlocked putchar
#endif
namespace {
#define isvisiblechar(c) (0x21<=(c)&&(c)<=0x7E)
    class MaiScanner {
    public:
        template<typename T> void input_integer(T& var) {
            var = 0; T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc && cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var * sign;
        }
        inline int c() { return getchar_unlocked(); }
        inline MaiScanner& operator>>(int& var) { input_integer<int>(var); return *this; }
        inline MaiScanner& operator>>(long long& var) { input_integer<long long>(var); return *this; }
        inline MaiScanner& operator>>(string& var) {
            int cc = getchar_unlocked();
            for (; !isvisiblechar(cc); cc = getchar_unlocked());
            for (; isvisiblechar(cc); cc = getchar_unlocked())
                var.push_back(cc);
        }
        template<typename IT> void in(IT begin, IT end) { for (auto it = begin; it != end; ++it) *this >> *it; }
    };
    class MaiPrinter {
    public:
        template<typename T>
        void output_integer(T var) {
            if (var == 0) { putchar_unlocked('0'); return; }
            if (var < 0)
                putchar_unlocked('-'),
                var = -var;
            char stack[32]; int stack_p = 0;
            while (var)
                stack[stack_p++] = '0' + (var % 10),
                var /= 10;
            while (stack_p)
                putchar_unlocked(stack[--stack_p]);
        }
        inline MaiPrinter& operator<<(char c) { putchar_unlocked(c); return *this; }
        inline MaiPrinter& operator<<(int var) { output_integer<int>(var); return *this; }
        inline MaiPrinter& operator<<(long long var) { output_integer<long long>(var); return *this; }
        inline MaiPrinter& operator<<(char* str_p) { while (*str_p) putchar_unlocked(*(str_p++)); return *this; }
        inline MaiPrinter& operator<<(const string& str) {
            const char* p = str.c_str();
            const char* l = p + str.size();
            while (p < l) putchar_unlocked(*p++);
            return *this;
        }
        template<typename IT> void join(IT begin, IT end, char sep = '\n') { for (auto it = begin; it != end; ++it) *this << *it << sep; }
    };
}
MaiScanner scanner;
MaiPrinter printer;


#define TIME chrono::system_clock::now()
#define MILLISEC(t) (chrono::duration_cast<chrono::milliseconds>(t).count())

// =================================================================
// =================================================================


struct bamboo {
    bamboo() { }
    bamboo(int l, int w) {}
    int length;
    int weight;
};
ostream& operator <<(ostream &o, const bamboo p) { o << make_pair(p.length, p.weight); return o; }


inline bool iskadomatsu(int a, int b, int c) {
    return a!=c && ((a<b && b>c)||(a>b && b<c));
}


int n, capacity;
vector<bamboo> bb;

// 最も効率が良い竹3つ
int lightest_weight[3];
// 最も長い竹3つの長さ
int longest_length[3];

auto stopwatch = TIME;
int best = 0;
void dfs(vector<bamboo> selected) {
    
    //debugv(selected)

    // 現在の重さと長さを計算しておく.
    int cw = 0, cl = 0;
    for (auto b : selected)
        cw += b.weight,
        cl += b.length;

    if (selected.size() >= 3)
        best = max(best, cl);
    if (capacity == cw) return;

    // 近似解を求める.
    int approximation_best = cl;
    {
        int w = cw;
        for (int i = 0;; i = ++i%3) {
            w += lightest_weight[i];
            if (w > capacity) break;
            approximation_best += longest_length[i];
        }
    }
    // double approximation_best = cl;
    // approximation_best += (double)bb[0].length/(double)bb[0].weight*(double)(capacity - cw);

    // 近似解でもbestを超えないならば,枝を刈る
    if (approximation_best <= best)
        return;
    
    // 2つ前
    bamboo prepre = selected[selected.size() - 2];
    // 1つ前
    bamboo pre = selected[selected.size() - 1];

    //if (MILLISEC(TIME-stopwatch) > 1900) return;
    repeat(i, n) {
        if (bb[i].weight + cw > capacity) continue;
        if (iskadomatsu(prepre.length, pre.length, bb[i].length)) {
            selected.push_back(bb[i]);
            dfs(selected);
            selected.pop_back();
        }
    }

    return;
}



int main() {

    scanner >> n >> capacity;
    bb.resize(n);
    repeat(i, n) {
        scanner >> bb[i].length;
    }
    repeat(i, n) {
        scanner >> bb[i].weight;
    }


    // 同じ長さの竹が複数あっても意味が無いので,減らす.
    {
        map<int, int> zip;
        for (auto b : bb)
            if (zip.count(b.length) == 0)
                zip[b.length] = b.weight;
            else
                zip[b.length] = min(zip[b.length], b.weight);

        bb.clear();
        for (auto z : zip)
            bb.emplace_back(z.first, z.second);
    }

    n = bb.size();

    if (n <= 2)
        bye("0");


    // 最も軽い竹3種を選んでおき,その重さを記憶しておく
    // 同様に,最も長い竹3種を選んでおき,その長さを記憶しておく
    {
        vector<int> ws;
        vector<int> ls;
        for (auto b : bb)
            ws.push_back(b.weight),
            ls.push_back(b.length);
        sort(ALL(ws));
        sort(ALL(ls), greater<int>());
        repeat(i, 3)
            lightest_weight[i] = ws[i],
            longest_length[i] = ls[i];
    }


    // 効率(L/W)降順にソート
    sort(ALL(bb), [](bamboo l, bamboo r) {return l.length > r.length; });
    stable_sort(ALL(bb), [](bamboo l, bamboo r) {
        return l.length*r.weight > r.length*l.weight;
    });


    // 最初の2つの竹を決めてから,dfs
    repeat(first, n) {
        repeat(second, n) {
            if (bb[first].length != bb[second].length)
                dfs({ bb[first], bb[second] });
        }
    }

    cout << best << endl;

    return 0;
}
0