結果

問題 No.74 貯金箱の退屈
ユーザー omoiomoi
提出日時 2024-06-02 00:21:23
言語 C++23(gcc13)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 17,632 bytes
コンパイル時間 7,273 ms
コンパイル使用メモリ 290,036 KB
実行使用メモリ 6,820 KB
最終ジャッジ日時 2024-12-22 15:51:03
合計ジャッジ時間 6,191 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 2 ms
6,820 KB
testcase_03 AC 2 ms
6,820 KB
testcase_04 AC 2 ms
6,820 KB
testcase_05 AC 3 ms
6,820 KB
testcase_06 AC 2 ms
6,816 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 2 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 2 ms
6,816 KB
testcase_11 AC 2 ms
6,816 KB
testcase_12 AC 2 ms
6,816 KB
testcase_13 AC 2 ms
6,816 KB
testcase_14 AC 2 ms
6,820 KB
testcase_15 AC 2 ms
6,816 KB
testcase_16 AC 2 ms
6,820 KB
testcase_17 AC 2 ms
6,816 KB
testcase_18 AC 2 ms
6,820 KB
testcase_19 AC 2 ms
6,816 KB
testcase_20 AC 2 ms
6,816 KB
testcase_21 AC 2 ms
6,816 KB
testcase_22 AC 2 ms
6,816 KB
testcase_23 AC 2 ms
6,816 KB
testcase_24 AC 2 ms
6,816 KB
testcase_25 AC 2 ms
6,820 KB
testcase_26 AC 2 ms
6,820 KB
testcase_27 AC 2 ms
6,816 KB
testcase_28 AC 2 ms
6,820 KB
testcase_29 AC 2 ms
6,816 KB
testcase_30 AC 3 ms
6,816 KB
testcase_31 AC 2 ms
6,820 KB
testcase_32 AC 2 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

namespace std {

    // 定数
    namespace ns_constant {

        //const long long MOD = 998244353;
        //const long long MOD = 1000000007;
        const long long MOD = 1000000009;

        const long long INF = 1LL << 60;
        const long long xorI = (1LL << 61) - 1;

        const long long mod = (1LL << 61) - 1;
        const long long MASK30 = (1LL << 30) - 1;
        const long long MASK31 = (1LL << 31) - 1;

        const long double PI = acos(-1);
        const long double eps = 1e-10;

        const long long dx[] = { 1, 0,-1, 0, 1,-1, 1,-1 };
        const long long dy[] = { 0, 1, 0,-1, 1,-1,-1, 1 };
        const pair<long long, long long> dxy[] = { {1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {-1, -1}, {1, -1}, {-1, 1} };

    }
    using namespace ns_constant;

    // 型定義
    namespace ns_type_define {

        template <typename T> using VT = vector<T>;

        typedef long long LI;
        typedef vector<LI> VI; typedef vector<VI> VVI; typedef vector<VVI> V3I; typedef vector<V3I> V4I;

        typedef long double LD;
        typedef vector<LD> VD; typedef vector<VD> VVD;

        typedef vector<bool> VB; typedef vector<VB> VVB;
        typedef vector<char> VC; typedef vector<VC> VVC;
        typedef vector<string> VS; typedef vector<VS> VVS;

        typedef pair<LI, LI> PII; typedef vector<PII> VPII; typedef vector<VPII> VVPII;
        typedef pair<LD, LD> PDD; typedef vector<PDD> VPDD; typedef vector<VPDD> VVPDD;
        typedef pair<string, LI> PSI; typedef vector<PSI> VPSI;

        typedef tuple<LI, LI, LI> TI; typedef vector<TI> VTI; typedef vector<VTI> VVTI;
        typedef tuple<LI, LI, LD> TD; typedef vector<TD> VTD; typedef vector<VTD> VVTD;

        typedef set<LI> SI; typedef set<PII> SPII; typedef multiset<LI> mSI;
        typedef vector<SI> VSI; typedef vector<VSI> VVSI;

        typedef map<LI, LI> MII; typedef map<PII, LI> MPIII; typedef multimap<LI, LI> mMII;
        typedef vector<MII> VMII; typedef vector<VMII> VVMII;
        typedef map<string, LI> MSI; typedef multimap<string, LI> mMSI; typedef vector<MSI> VMSI;

        typedef queue<LI> QI; typedef queue<PII> QPII;
        typedef priority_queue<PII, VPII, greater<PII>> PQPII;
        //優先順位付きqueue(昇順)

        template <typename T> using PQD = priority_queue<T>;
        template <typename T> using PQU = priority_queue<T, vector<T>, greater<T>>;
        template <typename T> using PQC = priority_queue<T, vector<T>, function<bool(T, T)>>;

    }
    using namespace ns_type_define;

    // マクロ
    namespace ns_macro {

        // ### 標準 ###
        namespace ns_macro_std {

#define mid(a, b, c)		    (a < b ? (a < c ? min(b, c) : a) : (b < c ? min(a, c) : b))

#define maxa(a, b)			    a = max(a, b);
#define mina(a, b)			    a = min(a, b);
#define mida(a, b, c)		    a = mid(a, b, c);

#define qpop(u, que)		    auto u = que.front(); que.pop();
#define qpop2(u, v, que)	    auto [u, v] = que.front(); que.pop();
#define qpop3(u, v, c, que)	    auto [u, v, c] = que.front(); que.pop();

#define pqpop(u, que)		    auto u = que.top(); que.pop();
#define pqpop2(u, v, que)	    auto [u, v] = que.top(); que.pop();
#define pqpop3(u, v, c, que)	auto [u, v, c] = que.top(); que.pop();

#define bits(x)				    (1LL << (x))
#define minf(x)				    ((x) != INF ? (x) : -1)

        }
        using namespace ns_macro_std;

        // ### ループ ###
        namespace ns_macro_loop {
            //
#define REP(i, j, k)			for (LI i = j; i < (LI)k; i++)
#define DREP(i, j, k)			for (LI i = k - 1; i >= (LI)j; i--)
#define rep(i, j)				for (int i = 0; i < (int)j; i++)
#define drep(i, j)				for (int i = (int)j - 1; i >= 0; i--)
#define repi(i, j)				for (int i = 1; i <= (int)j; i++)
#define drepi(i, j)				for (int i = (int)j; i > 0; i--)

#define vrep(a, v)				for (auto& a: v)
#define vrep2(a, b, v)			for (auto& [a, b]: v)
#define vrep3(a, b, c, v)		for (auto& [a, b, c]: v)
#define vrep4(a, b, c, d, v)	for (auto& [a, b, c, d]: v)

#define vcrep(a, v)				for (const auto& a: v)
#define vcrep2(a, b, v)			for (const auto& [a, b]: v)
#define vcrep3(a, b, c, v)		for (const auto& [a, b, c]: v)
#define vcrep4(a, b, c, d, v)	for (const auto& [a, b, c, d]: v)

// 集合 bit の部分集合を bit全探索
#define brep(subbit, bit)		for (LI subbit = bit; subbit; subbit = (subbit - 1) & bit)

// 要素数 R の部分集合を bit全探索
#define bcrep(bit, N, R)		for (LI bit = bits(R) - 1, x, y; bit < bits(N); x = bit & -bit, y = bit + x, bit = (((bit & ~y) / x) >> 1) | y)

        }
        using namespace ns_macro_loop;

        // ### 配列操作 ###
        namespace ns_macro_vector {

            void vsort(string& v) {
                sort(v.begin(), v.end());
            }
            template<typename T> void vsort(vector<T>& v) {
                sort(v.begin(), v.end());
            }
            void vsortr(string& v) {
                sort(v.begin(), v.end(), greater<char>());
            }
            template<typename T> void vsortr(vector<T>& v) {
                sort(v.begin(), v.end(), greater<T>());
            }

#define all(v)				        v.begin(), v.end()

#define vmax(v)				        (*max_element(all(v)))
#define vmin(v)				        (*min_element(all(v)))

#define miman(v, a)			        (lower_bound(all(v), a) - v.begin())
#define ika(v, a)			        (upper_bound(all(v), a) - v.begin())
#define ijo(v, a)			        (v.end() - lower_bound(all(v), a))
#define yorio(v, a)			        (v.end() - upper_bound(all(v), a))

#define iwa(W, A)			        VI W(1); vrep(a, A) W.push_back(W.back() + a);
#define dwa(W, A)			        VD W(1); vrep(a, A) W.push_back(W.back() + a);

#define vsorto(v, compare)          sort(all(v), compare);

#define vrev(v)				        reverse(all(v));

#define vcomp(v)				    v.erase(unique(all(v)), v.end());
#define vuni(v)			            vsort(v); vcomp(v);

        }
        using namespace ns_macro_vector;

        // ### 入力系 ###
        namespace ns_macro_input {

            namespace ns_macro_input_std {

#define scan(Type, a)						Type a; cin >> a;
#define scan2(Type, a, b)					scan(Type, a) scan(Type, b)
#define scan3(Type, a, b, c)				scan(Type, a) scan2(Type, b, c)
#define scan4(Type, a, b, c, d)				scan(Type, a) scan3(Type, b, c, d)

#define vscan(Type, A, N)					vector<Type> A((int)N);						                rep(mci, N) { cin >> A[mci]; }
#define vscan2(Type, A, B, N)				vector<Type> A((int)N), B((int)N);				            rep(mci, N) { cin >> A[mci] >> B[mci]; }
#define vscan3(Type, A, B, C, N)			vector<Type> A((int)N), B((int)N), C((int)N);			    rep(mci, N) { cin >> A[mci] >> B[mci] >> C[mci]; }
#define vscan4(Type, A, B, C, D, N)			vector<Type> A((int)N), B((int)N), C((int)N), D((int)N);	rep(mci, N) { cin >> A[mci] >> B[mci] >> C[mci] >> D[mci]; }

#define vvscan(Type, A, H, W)				vector A((int)H, vector<Type>((int)W));                     rep(mch, H) { rep(mcw, W) { cin >> A[(int)mch][(int)mcw]; } }


#define pscan(Type1, Type2, a)				pair<Type1, Type2> a; cin >> a.first >> a.second;
#define pscan2(Type1, Type2, a, b)			pscan(Type1, Type2, a) pscan(Type1, Type2, b);
#define pscan3(Type1, Type2, a, b, c)		pscan(Type1, Type2, a) pscan2(Type1, Type2, b, c);
#define pscan4(Type1, Type2, a, b, c, d)	pscan(Type1, Type2, a) pscan3(Type1, Type2, b, c, d);

#define vpscan(Type1, Type2, A, N)			vector<pair<Type1, Type2>> A((int)N);                      rep(mci, N) { cin >> A[mci].first >> A[mci].second; }


#define tscan(Type1, Type2, Type3, a)		Type1 mca; Type2 mcb; Type3 mcc;                            cin >> mca >> mcb >> mcc; tuple<Type1, Type2, Type3> a = { mca, mcb, mcc };

#define vtscan(A, N)						vector<tuple<Type1, Type2, Type3>> A((int)N);               rep(mci, N) { Type1 mca; Type2 mcb; Type3 mcc; cin >> mca >> mcb >> mcc; A[mci] = { mca, mcb, mcc }; }

            }
            using namespace ns_macro_input_std;

            namespace ns_macro_input_longlong {

#define iscan(a)				scan(LI, a)
#define iscan2(a, b)			scan2(LI, a, b)
#define iscan3(a, b, c)			scan3(LI, a, b, c)
#define iscan4(a, b, c, d)		scan4(LI, a, b, c, d)

#define viscan(A, N)			vscan(LI, A, N)
#define viscan2(A, B, N)		vscan2(LI, A, B, N)
#define viscan3(A, B, C, N)		vscan3(LI, A, B, C, N)
#define viscan4(A, B, C, D, N)	vscan4(LI, A, B, C, D, N)

#define vviscan(A, H, W)		vvscan(LI, A, H, W)


#define piscan(a)				pscan(LI, LI, a)
#define piscan2(a, b)			pscan2(LI, LI, a, b)
#define piscan3(a, b, c)		pscan3(LI, LI, a, b, c)
#define piscan4(a, b, c, d)		pscan4(LI, LI, a, b, c, d)

#define vpiscan(A, N)			vpscan(LI, LI, A, N)


#define tiscan(a)				tscan(LI, LI, LI, a)

#define vtiscan(A, N)			VTI A((int)N); rep(mci, N) { LI mca, mcb, mcc; cin >> mca >> mcb >> mcc; A[mci] = { mca, mcb, mcc }; }

            }
            using namespace ns_macro_input_longlong;

            namespace ns_macro_input_longlong_decrease {

#define iscand(a)				iscan(a) a--;
#define iscand2(a, b)			iscand(a) iscand(b)
#define iscand3(a, b, c)		iscand(a) iscand2(b, c)
#define iscand4(a, b, c, d)		iscand(a) iscand3(b, c, d)

#define viscand(A, N)			viscan(A, N)			rep(mci, N) { A[mci]--; }
#define viscand2(A, B, N)		viscan2(A, B, N)		rep(mci, N) { A[mci]--; B[mci]--; }
#define viscand3(A, B, C, N)	viscan3(A, B, C, N)		rep(mci, N) { A[mci]--; B[mci]--; C[mci]--; }
#define viscand4(A, B, C, D, N)	viscan4(A, B, C, D, N)	rep(mci, N) { A[mci]--; B[mci]--; C[mci]--; D[mci]--; }

#define vviscand(A, H, W)		vviscan(A, H, W)		rep(mch, H) { rep(mcw, W) { A[mch][mcw]--; } }


#define piscand(a)				piscan(a); a.first--; a.second--;
#define piscand2(a, b)			piscand(a) piscand(b)
#define piscand3(a, b, c)		piscand(a) piscand2(b, c)
#define piscand4(a, b, c, d)	piscand(a) piscand3(b, c, d)

#define vpiscand(A, N)			vpiscan(A, N); rep(mci, N) { A[mci].first--; A[mci].second--; }


#define tiscand(a)				LI mca, mcb, mcc; cin >> mca >> mcb >> mcc; TI a = { mca, mcb, mcc }; 

#define vtiscand(A, N)			VTI A(N); rep(mci, N) { LI mca, mcb, mcc; cin >> mca >> mcb >> mcc; A[mci] = { mca - 1, mcb - 1, mcc }; }

            }
            using namespace ns_macro_input_longlong_decrease;

            namespace ns_macro_input_longdouble {

#define dscan(a)				scan(LD, a)
#define dscan2(a, b)			scan2(LD, a, b)
#define dscan3(a, b, c)			scan3(LD, a, b, c)
#define dscan4(a, b, c, d)		scan4(LD, a, b, c, d)

#define vdscan(A, N)			vscan(LD, A, N)
#define vdscan2(A, B, N)		vscan2(LD, A, B, N)
#define vdscan3(A, B, C, N)		vscan3(LD, A, B, C, N)
#define vdscan4(A, B, C, D, N)	vscan4(LD, A, B, C, D, N)

#define vvdscan(A, H, W)		vvscan(LD, A, H, W)


#define pdscan(a)				pscan(LD, LD, a)
#define pdscan2(a, b)			pscan2(LD, LD, a, b)
#define pdscan3(a, b, c)		pscan3(LD, LD, a, b, c)
#define pdscan4(a, b, c, d)		pscan4(LD, LD, a, b, c, d)

#define vpdscan(A, N)			vpscan(LD, LD, A, N)


#define tdscan(a)				tscan(LI, LI, LD, a)

#define vtdscan(A, N)			VTD A(N); rep(mci, N) { LI mca, mcb; LD mcc; cin >> mca >> mcb >> mcc; A[mci] = { mca, mcb, mcc }; }


#define tdscand(a)				tscand(LI, LI, LD, a)

#define vtdscand(A, N)			VTD A(N); rep(mci, N) { LI mca, mcb; LD mcc; cin >> mca >> mcb >> mcc; A[mci] = { mca - 1, mcb - 1, mcc }; }

            }
            using namespace ns_macro_input_longdouble;

            namespace ns_macro_input_string {

#define sscan(a)				scan(string, a)
#define sscan2(a, b)			scan2(string, a, b)
#define sscan3(a, b, c)			scan3(string, a, b, c)
#define sscan4(a, b, c, d)		scan4(string, a, b, c, d)

#define vsscan(S, N)			vscan(string, S, N)

            }
            using namespace ns_macro_input_string;

        }
        using namespace ns_macro_input;

        // ### 出力系 ###
        namespace ns_macro_output {

            namespace ns_macro_output_std {

#define el					cout << endl;

#define show(a)				cout << (a) << endl;
#define show2(a, b)			cout << (a) << " " << (b) << endl;
#define show3(a, b, c)		cout << (a) << " " << (b) << " " << (c) << endl;
#define show4(a, b, c, d)	cout << (a) << " " << (b) << " " << (c) << " " << (d) << endl;

#define shown(a)			cout << (a) << " ";
#define shown2(a, b)		cout << (a) << " " << (b) << " ";
#define shown3(a, b, c)		cout << (a) << " " << (b) << " " << (c) << " ";
#define shown4(a, b, c, d)	cout << (a) << " " << (b) << " " << (c) << " " << (d) << " ";

#define vshow(A)			vcrep(a, A) { show(a) }
#define vshown(A)			vcrep(a, A) { shown(a) } el
#define vvshow(A)			vcrep(a, A) { vshown(a) }


#define pshow(a)			show2((a).first, (a).second)
#define pshown(a)			shown2((a).first, (a).second)
#define pshow2(a, b)		pshown(a) pshow(b)

#define vpshow(A)			vcrep(a, A) { pshow(a) }


#define tshow(a)			show3(get<0>(a), get<1>(a), get<2>(a))
#define tshown(a)			shown3(get<0>(a), get<1>(a), get<2>(a))

#define vtshow(A)			vcrep(a, A) { tshow(a) }


#define showif(f, a, b)		if (f) { show(a) } else { show(b) }
#define shownif(f, a, b)	if (f) { shown(a) } else { shown(b) }

#define yes(f)				showif(f, "Yes", "No")

            }
            using namespace ns_macro_output_std;

            namespace ns_macro_output_by_type {

#define ishow(a)			show(minf(a))
#define ishown(a)			shown(minf(a))

#define vishow(A)			vcrep(a, A) { ishow(a) }
#define vishown(A)			vcrep(a, A) { ishown(a) } el
#define vvishow(A)			vcrep(a, A) { vishown(a) }


#define dnshow(x, n)		cout << fixed << setprecision(n) << (x) << endl;
#define dnshown(x, n)		cout << fixed << setprecision(n) << (x) << " ";

#define vdnshow(A, n)		vcrep(a, A) { dshow(a, n) }
#define vdnshown(A, n)		vcrep(a, A) { dshown(a, n) } el
#define vvdnshow(A, n)		vcrep(a, A) { vdshown(a, n) }


#define dshow(x)			cout << fixed << setprecision(10) << (x) << endl;
#define dshown(x)   		cout << fixed << setprecision(10) << (x) << " ";

#define vdshow(A)   		vcrep(a, A) { dshow(a) }
#define vdshown(A)	    	vcrep(a, A) { dshown(a) } el
#define vvdshow(A)  		vcrep(a, A) { vdshown(a) }


#define bshow(a, n)			show(bitset<n>(a))
#define bshown(a, n)		shown(bitset<n>(a))

#define vbshow(A, n)		vcrep(a, A) { show(bitset<n>(a)) }
#define vbshown(A, n)		vcrep(a, A) { shown(bitset<n>(a)) } el
#define vvbshow(A, n)		vcrep(a, A) { vbshow(a, n) }

            }
            using namespace ns_macro_output_by_type;

            namespace ns_macro_output_debug {

#define debughead			shown(">>")

#define debugm(a)			shown3(#a, "=", a)
#define debugn(a)			debugm(a) shown(",")

#define debug(a)			debughead debugm(a) el
#define debug2(a, b)		debughead debugn(a) debugm(b) el
#define debug3(a, b, c)		debughead debugn(a) debugn(b) debugm(c) el
#define debug4(a, b, c, d)	debughead debugn(a) debugn(b) debugn(c) debugm(d) el

            }
            using namespace ns_macro_output_debug;

        }
        using namespace ns_macro_output;

    }
    using namespace ns_macro;

}

// UnionFind
template <typename T> class union_find {
private:
    // 頂点数
    int N;

    // 無限遠の距離
    T dinf;

    // parent[i]:頂点i の親/頂点i が属する木のサイズ(頂点i が根ではない/頂点i が根)
    vector<int> parent;

    // dist[i]:親から頂点i までの距離
    vector<T> temporarily_distance;

    // 木の根から頂点i までの距離
    T calc_distance(LI i) {
        root(i);
        return temporarily_distance[(int)i];
    }

public:
    union_find(LI N = 0, T dinf = INF)
        : N((int)N), dinf(dinf), parent((int)N, -1), temporarily_distance((int)N) {}

    // 頂点i の根
    int root(LI i) {
        if (parent[(int)i] < 0) return (int)i;
        int root_index = root(parent[(int)i]);
        temporarily_distance[(int)i] += temporarily_distance[parent[(int)i]];
        return parent[(int)i] = root_index;
    }

    // 頂点i が根かの判定(true/false : 頂点i が根/根じゃない)
    bool ifroot(LI i) {
        return i == root(i);
    }

    // 頂点i が属する木のサイズ
    int size(LI i) {
        return -parent[root(i)];
    }

    // 同じ木に属するかの判定(true/false:頂点a と 頂点b が同じ木に属する/属しない)
    bool same(LI a, LI b) {
        return root(a) == root(b);
    }

    // 辺の追加(from:始点、to:到達点、distance:距離、戻り値=true/false:追加の成功/失敗)
    bool add_edge(LI from, LI to, T distance = 0) {
        if (same(from, to)) return false;

        distance -= calc_distance(to) - calc_distance(from);
        from = root(from);
        to = root(to);
        if (size(from) < size(to)) {
            swap(from, to);
            distance = -distance;
        }

        parent[(int)from] += parent[(int)to];
        parent[(int)to] = (int)from;
        temporarily_distance[(int)to] = distance;

        return true;
    }
    bool merge(LI from, LI to, T distance = 0) {
        return add_edge(from, to, distance);
    }

    // start から target までの最短距離(到達できない場合はINF)
    T dist(LI start, LI target) {
        return same(start, target) ? calc_distance(target) - calc_distance(start) : dinf;
    }
};

int main() {
    iscan(N);
    union_find<LI> uf(N);
    viscan(D, N);
    viscan(W, N);
    SI ok;
    rep(i, N) {
        LI l = i + D[i], r = i - D[i];
        l %= N, r %= N;
        if (r < 0) r += N;
        if (l == r) ok.insert(l);
        uf.merge(l, r);
    }
    VI cnt(N);
    rep(i, N) if (W[i]) cnt[uf.root(i)]++;
    vrep(o, ok) cnt[uf.root(o)] = -1;
    bool ans = true;
    rep(i, N) {
        if (!uf.ifroot(i)) continue;
        if (cnt[i] < 0) continue;
        if ((uf.size(i) - cnt[i]) % 2) ans = false;
    }
    yes(ans);
}
0