diff --git a/SavingTopUsersSubmissions.py b/SavingTopUsersSubmissions.py new file mode 100644 index 0000000..36f5df7 --- /dev/null +++ b/SavingTopUsersSubmissions.py @@ -0,0 +1,38 @@ +import urllib.request, json, os +import main + +only_active = "false" +query = "https://codeforces.com/api/user.ratedList?activeOnly=" + only_active + +def listOfUsers(number_of_users, parent_dir): + + with urllib.request.urlopen(query) as url: + data = json.loads(url.read().decode()) + + users = data["result"][:number_of_users] + + result = [] + for i in range(len(users)): + result.append([users[i]['handle'], users[i]['maxRating']]) + + result = sorted(result, key=lambda x: -x[1]) + + for user in result: + print(user[0]) + + directory = user[0] + path = os.path.join(parent_dir, directory) + os.mkdir(path) + os.chdir(path) + + main.returnAcceptedSubmissions(user[0], 2) + os.chdir("..") + print(user[0]) + + +if __name__ == "__main__": + listOfUsers(10, "C:/Users/HP/Documents/GitHub/code-submission/code") + + + + diff --git a/code/Benq/1020B b/code/Benq/1020B new file mode 100644 index 0000000..43bffe4 --- /dev/null +++ b/code/Benq/1020B @@ -0,0 +1,228 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int N; +vi P,ans; + +int solve(int x) { + // dbg("SOLVING",x); + ans[x] = -1; + if (ans[P[x]] >= 0) return ans[x] = ans[P[x]]; + if (ans[P[x]] == -1) { + if (x == P[x]) return ans[x] = x; + return ans[x] = x, ans[P[x]] = P[x], -1; + } + int nex = solve(P[x]); + if (ans[x] == -1) ans[x] = nex; + if (ans[x] != -1) return ans[x]; + return ans[x] = x, -1; +} + +int main() { + setIO(); rv(N,P); trav(t,P) --t; + ans = vi(N,-2); + F0R(i,N) if (ans[i] < 0) solve(i); + trav(t,ans) pr(t+1,' '); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1354D b/code/Benq/1354D new file mode 100644 index 0000000..12afcdf --- /dev/null +++ b/code/Benq/1354D @@ -0,0 +1,245 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} +/** + * Description: range sum queries and point updates for $D$ dimensions + * Source: https://codeforces.com/blog/entry/64914 + * Verification: SPOJ matsum + * Usage: \texttt{BIT} gives 2D BIT + * Time: O((\log N)^D) + */ + +template struct BIT { + T val = 0; void upd(T v) { val += v; } + T query() { return val; } +}; +template struct BIT { + BIT bit[N+1]; + template void upd(int pos, Args... args) { assert(pos > 0); + for (; pos<=N; pos+=pos&-pos) bit[pos].upd(args...); } + template T sum(int r, Args... args) { + T res=0; for (;r;r-=r&-r) res += bit[r].query(args...); + return res; } + template T query(int l, int r, Args... + args) { return sum(r,args...)-sum(l-1,args...); } +}; + +BIT S; +int n,q; + +int main() { + setIO(); re(n,q); + vi A(n); re(A); + trav(t,A) S.upd(t,1); + F0R(i,q) { + int k; re(k); + if (k > 0) S.upd(k,1); + else { + int ind = fstTrue(1,n,[&](int x) { return S.sum(x) >= -k; }); + S.upd(ind,-1); + } + } + int ind = fstTrue(1,n,[&](int x) { return S.sum(x) >= 1; }); + ps(ind == n+1 ? 0 : ind); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1428F b/code/Benq/1428F new file mode 100644 index 0000000..ece049b --- /dev/null +++ b/code/Benq/1428F @@ -0,0 +1,243 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +#define tcTU tcT, class U +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int N; +str S; +ll ans, sum; +vpi st; + +ll val(pi p) { return (ll)p.f*p.s; } +ll up_to(ll x) { return x*(x+1)/2; } + +int main() { + setIO(); re(N,S); + for (int i = N-1; i >= 0; ) { + if (S[i] == '0') { + ans += sum; + st.pb({0,1}); + i --; + continue; + } + int len = 0; + while (i >= 0 && S[i] == '1') { + i --; + len ++; + ll num = 0; + while (sz(st) && st.bk.f <= len) { + num += st.bk.s; sum -= val(st.bk); + st.pop_back(); + } + st.pb({len,num}); + sum += val(st.bk); + ans += up_to(len)+sum; + // dbg("WUT",i,ans); + // max of all with len + // 1 to len + } + ROF(z,1,len+1) { + st.pb({z,1}); + sum += val(st.bk); + } + // dbg("???",i,ans,st,sum); + } + ps(ans); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1428G1 b/code/Benq/1428G1 new file mode 100644 index 0000000..3f70fa8 --- /dev/null +++ b/code/Benq/1428G1 @@ -0,0 +1,257 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 1e6; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +#define tcTU tcT, class U +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int K; +ll ans[MX]; +vl F(6); + +void NINE(int val, int num, ll cost) { + val *= 9; cost *= 3; + dbg("NINE",val,num,cost); + auto ind = [&](int x) { + return x/val; + }; + auto get = [&](int x) { + return ans[x]-ind(x)*cost; + }; + // R0F(i,MX) if (i >= val) ckmax(ans[i],ans[i-val]+3*cost); + F0R(off,val) { + deque> d; + for (int i = off; i < MX; i += val) { + ll cur = get(i); + while (sz(d) && d.bk.f < cur) d.pop_back(); + d.pb({cur,i}); + while (ind(d.ft.s)+num < ind(i)) d.pop_front(); + assert(sz(d)); + ans[i] = d.ft.f+ind(i)*cost; + } + } +} + +void ANY(int val, ll cost) { + R0F(i,MX) for (int j = 1; j < 10 && i+j*val < MX; ++j) { + if (j%3 == 0) { + ckmax(ans[i+j*val],ans[i]+j/3*cost); + } else { + ckmax(ans[i+j*val],ans[i]); + } + } +} + +int main() { + setIO(); re(K,F); + FOR(i,1,MX) ans[i] = -INF; + dbg(F); + F0R(dig,6) { + int val = pow(10,dig); + int any = min(2,K); + NINE(val,K-any,F[dig]); + F0R(i,any) ANY(val,F[dig]); + // F0R(i,K)ANY(val,F[dig]); + } + // F0R(i,100) dbg(i,ans[i]); + int Q; re(Q); + F0R(i,Q) { + int n; re(n); + ps(ans[n]); + } + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1428G2 b/code/Benq/1428G2 new file mode 100644 index 0000000..3f70fa8 --- /dev/null +++ b/code/Benq/1428G2 @@ -0,0 +1,257 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 1e6; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +#define tcTU tcT, class U +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int K; +ll ans[MX]; +vl F(6); + +void NINE(int val, int num, ll cost) { + val *= 9; cost *= 3; + dbg("NINE",val,num,cost); + auto ind = [&](int x) { + return x/val; + }; + auto get = [&](int x) { + return ans[x]-ind(x)*cost; + }; + // R0F(i,MX) if (i >= val) ckmax(ans[i],ans[i-val]+3*cost); + F0R(off,val) { + deque> d; + for (int i = off; i < MX; i += val) { + ll cur = get(i); + while (sz(d) && d.bk.f < cur) d.pop_back(); + d.pb({cur,i}); + while (ind(d.ft.s)+num < ind(i)) d.pop_front(); + assert(sz(d)); + ans[i] = d.ft.f+ind(i)*cost; + } + } +} + +void ANY(int val, ll cost) { + R0F(i,MX) for (int j = 1; j < 10 && i+j*val < MX; ++j) { + if (j%3 == 0) { + ckmax(ans[i+j*val],ans[i]+j/3*cost); + } else { + ckmax(ans[i+j*val],ans[i]); + } + } +} + +int main() { + setIO(); re(K,F); + FOR(i,1,MX) ans[i] = -INF; + dbg(F); + F0R(dig,6) { + int val = pow(10,dig); + int any = min(2,K); + NINE(val,K-any,F[dig]); + F0R(i,any) ANY(val,F[dig]); + // F0R(i,K)ANY(val,F[dig]); + } + // F0R(i,100) dbg(i,ans[i]); + int Q; re(Q); + F0R(i,Q) { + int n; re(n); + ps(ans[n]); + } + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1428H b/code/Benq/1428H new file mode 100644 index 0000000..ef2e248 --- /dev/null +++ b/code/Benq/1428H @@ -0,0 +1,367 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +#define tcTU tcT, class U +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { cout << endl; } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int N,M; +vi lst; +vi rig; +vi actual_rig = {0,1,7}; + +int nor(int x) { + x %= N*M; + if (x < 0) x += N*M; + return x; +} + +int calc() { + int ans = 0; + vb cover(N*M); + F0R(i,N) F0R(j,M) cover[(actual_rig[i]-j+N*M)%(N*M)] = 1; + F0R(i,N*M) ans += !cover[i]; + // dbg(actual_rig,ans); + return ans; +} + +int query; + +int rot(int x, int d) { + assert(0 <= x && x < N && abs(d) == 1); + query ++; assert(query <= 15000); + ps("?",x,d); + if (rig[x] != -1) rig[x] = nor(rig[x]+d); + actual_rig[x] = nor(actual_rig[x]+d); + int res; re(res); + // int res = calc(); + // dbg(actual_rig); + // dbg(res); + lst.pb(res); + return lst.bk; +} + +void finish() { + dbg(query); + trav(t,rig) assert(t != -1); + pr('!'); + FOR(i,1,N) pr(' ',(rig[i]-rig[0]+N*M)%(N*M)); + ps(); +} + +void start() { + re(N,M); rig = vi(N,-1); + actual_rig.rsz(N); + FOR(i,1,N) actual_rig[i] = rand()%(N*M); + rig[0] = 0; +} + +bool contains(pi p, int x) { + assert(0 <= p.f && p.s < N*M); + return p.f <= x && x <= p.s; +} + +// bool covered(int x) { +// FOR(i,1,N) if (rig[i] != -1) { +// pi p = {rig[i]-(M-1),rig[i]}; +// if (contains(p,x)) return 1; +// } +// return 0; +// } + +// bool moveExpected() { +// int expected = lst.bk+!covered(rig[0]-(M-1))-1; +// int actual = rot(0,1); +// dbg(expected,actual); +// if (expected == actual) return 1; +// return 0; +// } + +bool existsStart() { + rot(0,-1); + rot(0,1); rot(0,1); + bool res = lst[sz(lst)-3] < lst[sz(lst)-2] && lst[sz(lst)-2] >= lst.bk; + rot(0,-1); + return res; +} + +void deal() { + // dbg(rig,actual_rig); + lst.clear(); + rot(0,-1); rot(0,1); rot(0,1); + while (1) { + if (lst[sz(lst)-3] < lst[sz(lst)-2] && lst[sz(lst)-2] >= lst.bk) { + rot(0,-1); + break; + } + rot(0,1); + } + // while (!existsStart()) { + // rot(0,1); + // } + // dbg("AFTER START",rig,actual_rig,lst); + vi cand; FOR(i,1,N) if (rig[i] == -1) cand.pb(i); + // shuffle(all(cand),rng); + int actual = -1; + bool flag = 0; + while (1) { + assert(sz(cand)); + int t = cand.bk; rot(t,-1); + if (flag == 0) { + rot(0,-1); + if (lst.bk >= lst[sz(lst)-2]) { + rig[actual = cand.bk] = rig[0]; + rot(0,1); + break; + } + flag = 1; + } else { + rot(0,1); + if (lst.bk <= lst[sz(lst)-2]) { + rig[actual = cand.bk] = nor(rig[0]-1); + break; + } + flag = 0; + } + cand.pop_back(); + } + F0R(_,M) rot(actual,-1); + // dbg("RESULT",rig,actual_rig); + // dbg("----"); +} + +int main() { + start(); + F0R(_,N-1) deal(); + // dbg("START",rig,actual_rig); + // while (1) { + // int ori = rig[0]; + // vi bin; + // FOR(i,1,N) if (rig[i] != -1) { + // while (rig[i] > rig[0]) rot(i,-1); + // } else bin.pb(i); + // if (!sz(bin)) break; + // // shuffle(all(bin),rng); + // if (moveExpected()) continue; + // rot(0,-1); + // int ind = 0; + // while (ind < sz(bin)) { + // int t = bin[ind]; + // dbg("TESTING",bin,ind,t); + // assert(rig[0] == ori); + // rot(t,1); + // // assert(rig[0] == ori+1); + // if (moveExpected()) { + // dbg("COOL",t); + // rig[t] = rig[0]+M; + // rot(0,-1); + // break; + // } + // dbg("NOT EXPECTED"); + // assert(rig[0] == ori+1); + // ind ++; + // rot(0,-1); + // assert(rig[0] == ori); + // } + // dbg(rig); + // assert(ind < sz(bin)); + // F0R(i,ind+1) rot(bin[i],-1); + // } + finish(); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + + diff --git a/code/Benq/1434A b/code/Benq/1434A new file mode 100644 index 0000000..c8f3822 --- /dev/null +++ b/code/Benq/1434A @@ -0,0 +1,231 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +vi A(6),B; +int N; +priority_queue cur; + +int mn = MOD; + +void ins(pi p) { + ckmin(mn,p.f); + cur.push(p); +} + +int main() { + setIO(); re(A); + sort(all(A)); + rv(N,B); + trav(t,B) ins({t-A[0],0}); + int ans = MOD; + while (sz(cur)) { + ckmin(ans,cur.top().f-mn); + pi p = cur.top(); cur.pop(); + if (p.s == 5) break; + p.f += A[p.s]; p.f -= A[++p.s]; + ins(p); + } + ps(ans); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1434B b/code/Benq/1434B new file mode 100644 index 0000000..2375a46 --- /dev/null +++ b/code/Benq/1434B @@ -0,0 +1,257 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int N; +vpi st; +vi loc[MX]; + +int cnt = 0; +void go(vi& a, vi& b) { + if (sz(a) < sz(b)) swap(a,b); + while (sz(b)) { + a.pb(b.bk); + // cnt ++; + b.pop_back(); + } +} + +int main() { + clock_t beg = clock(); + setIO(); N = 100000; re(N); + vi price(N,-1); + F0R(i,2*N) { + char c; re(c); + // if (i < N) c = '+'; + // else c = '-'; + if (c == '+') { + loc[i].pb(cnt); + cnt ++; + st.pb({0,i}); + } else { + int x = i-N+1; re(x); + while (sz(st) && !sz(loc[st.bk.s])) st.pop_back(); + // dbg("DOING",x,cnt); + // dbg("HA",x,st); + if (sz(st) && st.bk.f < x) { + // dbg("AH",loc[st.bk.s].bk); + price[loc[st.bk.s].bk] = x; + loc[st.bk.s].pop_back(); + } else { + ps("NO"); + exit(0); + } + while (sz(st) && st.bk.f < x) { + go(loc[i],loc[st.bk.s]); + st.pop_back(); + } + st.pb({x,i}); + // dbg("HA",sz(al)); + } + } + dbg((db)(clock()-beg)/CLOCKS_PER_SEC); + ps("YES"); + trav(t,price) pr(t,' '); + ps(); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1434C b/code/Benq/1434C new file mode 100644 index 0000000..0c13655 --- /dev/null +++ b/code/Benq/1434C @@ -0,0 +1,226 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// make sure to intialize ALL GLOBAL VARS between tcs! + +ll A,B,C,D; + +void solve(int tc) { + // dbg("HA",tc); + re(A,B,C,D); + if (A > B*C) { + ps(-1); + return; + } + ll hi = A/B; + ll times = hi/D; + ps(A*(times+1)-D*B*times*(times+1)/2); + // D*? <= hi; +} + +int main() { + setIO(); + int TC; re(TC); + FOR(i,1,TC+1) solve(i); +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1434D b/code/Benq/1434D new file mode 100644 index 0000000..37f68f4 --- /dev/null +++ b/code/Benq/1434D @@ -0,0 +1,368 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 1<<19; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: Calculates longest path in tree. The vertex furthest + * from 0 must be an endpoint of the diameter. + * Source: own + * Verification: + * http://www.spoj.com/problems/PT07Z/ + * https://codeforces.com/contest/1182/problem/D + */ + +template struct TreeDiameter { + int N, par[SZ], dist[SZ], diaLen; + vi adj[SZ], dia, center; + void ae(int a, int b) { adj[a].pb(b), adj[b].pb(a); } + void dfs(int x) { + trav(y,adj[x]) if (y != par[x]) { + par[y] = x; dist[y] = dist[x]+1; + dfs(y); } + } + void genDist(int x) { par[x] = -1; dist[x] = 0; dfs(x); } + void init(int _N) { + N = _N; dia = {0,0}; + // dbg("HA",N); + genDist(0); F0R(i,N) if (dist[i]>dist[dia[0]]) dia[0] = i; + genDist(dia[0]); F0R(i,N) if (dist[i]>dist[dia[1]]) dia[1] = i; + diaLen = dist[dia[1]]; + int cen = dia[1]; F0R(i,diaLen/2) cen = par[cen]; + center = {cen}; if (diaLen&1) center.pb(par[cen]); + } +}; + +TreeDiameter T; + +/** + * Description: 1D range increment and sum query. + * Source: USACO Counting Haybales + * Verification: SPOJ Horrible + */ + +template struct LazySeg { + static_assert(pct(SZ) == 1); // SZ must be power of 2 + T comb(T a, T b) { return {max(a.f,b.f),max(a.s,b.s)}; } + T seg[2*SZ]; + bool lazy[2*SZ]; + LazySeg() { F0R(i,2*SZ) seg[i] = {0,0}, lazy[i] = 0; } + void push(int ind, int L, int R) { /// modify values for current node + if (!lazy[ind]) return; + seg[ind] = {seg[ind].s,seg[ind].f}; + if (L != R) F0R(i,2) lazy[2*ind+i] ^= lazy[ind]; /// prop to children + lazy[ind] = 0; + } // recalc values for current node + void pull(int ind) { seg[ind] = comb(seg[2*ind],seg[2*ind+1]); } + void build(vi v) { + F0R(i,sz(v)) seg[SZ+i] = {v[i],0}; + ROF(i,1,SZ) pull(i); + } + void upd(int lo,int hi,bool inc,int ind=1,int L=0, int R=SZ-1) { + push(ind,L,R); if (hi < L || R < lo) return; + if (lo <= L && R <= hi) { + lazy[ind] = inc; push(ind,L,R); return; } + int M = (L+R)/2; upd(lo,hi,inc,2*ind,L,M); + upd(lo,hi,inc,2*ind+1,M+1,R); pull(ind); + } + // T query(int lo, int hi, int ind=1, int L=0, int R=SZ-1) { + // push(ind,L,R); if (lo > R || L > hi) return ; + // if (lo <= L && R <= hi) return seg[ind]; + // int M = (L+R)/2; + // return comb(query(lo,hi,2*ind,L,M),query(lo,hi,2*ind+1,M+1,R)); + // } +}; + +LazySeg LS; + +/** + * Description: Euler Tour LCA. Compress takes a subset $S$ of nodes + * and computes the minimal subtree that contains all the nodes + * pairwise LCAs and compressing edges. Returns a list of + * \texttt{(par, orig\_index)} representing a tree rooted at 0. + * The root points to itself. + * Time: O(N\log N) build, O(1) LCA, O(|S|\log |S|) compress + * Source: USACO, Simon Lindholm (KACTL) + * Verification: USACO Debug the Bugs + * https://codeforces.com/contest/1320/problem/E + */ + +// #include "../../data-structures/Static Range Queries (9.1)/RMQ (9.1).h" + +struct LCA { + int N; + vpi adj[MX]; + vi depth, pos, par, rev; // rev is for compress + vi tmp; // RMQ r; + vi st, en; + void init(int _N) { N = _N; + depth = pos = par = rev = vi(N); + st = en = vi(N); + } + void ae(int x, int y, int label) { + adj[x].pb({y,label}); + adj[y].pb({x,label}); + } + void dfs(int x) { + pos[x] = sz(tmp); tmp.pb(depth[x]); + trav(y,adj[x]) if (y.f != par[x]) { + depth[y.f] = depth[par[y.f]=x]+1; + st[y.s] = sz(tmp); + dfs(y.f); + en[y.s] = sz(tmp)-1; + } + } + void gen(int R = 0) { par[R] = R; dfs(R); } +}; +LCA L; + +int N,M; +V> ed; +vi roads; +vi ans; + +void flip(int ind) { + LS.upd(L.st[ind],L.en[ind],1); +} + +void go(int x) { + LS = LazySeg(); + L = LCA(); L.init(N); + F0R(i,N-1) L.ae(ed[i].f.f,ed[i].f.s,i); + L.gen(x); + LS.build(L.tmp); + F0R(i,N-1) if (ed[i].s) { + // dbg("BEF",i,LS.seg[1].f); + flip(i); + // dbg("AFT",i,LS.seg[1].f); + } + F0R(i,M) { + // dbg("WUT",x,LS.seg[1]); + flip(roads[i]); + ckmax(ans[i],LS.seg[1].f); + } + dbg(ans); +} + +int main() { + setIO(); re(N); // OK + F0R(i,N-1) { + int u,v,t; re(u,v,t); + --u,--v; + ed.pb({{u,v},t}); + T.ae(u,v); + } + T.init(N); + rv(M,roads); + trav(t,roads) --t; + ans = vi(M,-1); + // swap(T.dia[0],T.dia[1]); + dbg("???",T.dia,T.diaLen); + go(T.dia[0]); + go(T.dia[1]); + trav(t,ans) ps(t); + // roads.pb(); + // ps(T.dia,T.diaLen); + // exit(0); + + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1434E b/code/Benq/1434E new file mode 100644 index 0000000..fb9798c --- /dev/null +++ b/code/Benq/1434E @@ -0,0 +1,465 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +int N,M; +vi game; + +int mex(set s) { + int cur = 0; while (s.count(cur)) cur ++; + return cur; +} + +int go(vi a, vi b) { + while (sz(a) > 2) a.erase(begin(a)); + set vals; + F0R(i,sz(b)) { + if (sz(a) == 2) { + if (!(a[1]-a[0] < b[i]-a[1])) { + continue; + } + } + vi A = a; A.pb(b[i]); + vals.ins(go(A,vi(begin(b)+i+1,end(b)))); + } + return mex(vals); +} + +int val() { + // dbg(game); + return go({},game); + // return 1; +} + +// int grundy(vi v) { +// set mov; +// F0R(i,sz(v)) mov.insert(go(v[i],vi(begin(v)+1+i,end(v)))); +// return mex(mov); +// } + +int find_max(vi a, vi b) { + F0R(i,sz(a)) FOR(j,i+1,sz(a)) FOR(k,j+1,sz(a)) + if (a[j]-a[i] >= a[k]-a[j]) return 0; + int res = sz(a); + // dbg("WHOOPS",a,b); + F0R(i,sz(b)) { + // if (sz(a) > 1) { + // if (!(a.bk-a[sz(a)-2] < b[i]-a.bk)) { + // continue; + // } + // } + vi A = a; A.pb(b[i]); + ckmax(res,find_max(A,vi(begin(b)+i+1,end(b)))); + } + return res; +} + +int brute() { + // longest seq+1 + return find_max({},game); +} + +int get_brute(vi v) { + N = sz(v); + game = v; + return brute(); +} + +int get_val(vi v) { + N = sz(v); + game = v; + return val(); +} + +void verify(vi v) { + int ans = get_brute(v); + // dbg(get_brute(v),get_val(v)); + if (ans < sz(v)) { + F0R(i,sz(v)) { + vi V = v; V.erase(begin(V)+i); + dbg(ans,V,get_brute(V),get_val(V)); + if (ans == get_brute(V)) return; + } + } + // dbg("BAD",v,ans); exit(0); +} + +/**template T kruskal(int N, vector> ed) { + sort(all(ed)); + T ans = 0; DSU D; D.init(N); // edges that unite are in MST + trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f; + return ans; +}*/ + +/** + * Description: Disjoint Set Union with path compression + * and union by size. Add edges and test connectivity. + * Use for Kruskal's or Boruvka's minimum spanning tree. + * Time: O(\alpha(N)) + * Source: CSAcademy, KACTL + * Verification: * + */ + +struct DSU { + vi e; void init(int N) { e = vi(N,-1); } + int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } + bool sameSet(int a, int b) { return get(a) == get(b); } + int size(int x) { return -e[get(x)]; } + void unite(int x, int y) { // union by size + x = get(x), y = get(y); assert(x < y); + e[x] = y; + } +}; + +DSU D; + +/**template T kruskal(int N, vector> ed) { + sort(all(ed)); + T ans = 0; DSU D; D.init(N); // edges that unite are in MST + trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f; + return ans; +}*/ + +int at_least[MX], rkey[MX]; + +int actual() { + // store max slope such that answer will be x + FOR(i,1,100005) at_least[i] = 100004; + trav(t,game) at_least[t] = t; + F0R(i,sz(game)) rkey[game[i]] = i; + ROF(i,1,100004) ckmin(at_least[i],at_least[i+1]); + vi mx(sz(game),MOD); + set all_vals; + for (int val = 0;;++val) { + bool not_done = 0; + trav(t,mx) if (t > -MOD) not_done = 1; + if (!not_done) break; + vi new_mx = mx; + // map lef; F0R(i,sz(game)) lef[game[i]] = i; + vi tmp(sz(game),-MOD); + trav(t,game) D.e[t] = -1; + R0F(i,sz(game)) { + ckmin(new_mx[i],tmp[i]-game[i]-1); + if (mx[i] > -MOD && new_mx[i] <= -MOD) all_vals.ins(val); + int lo = game[i]-mx[i], hi = game[i]-new_mx[i]; + ckmax(lo,1); ckmin(hi,game[i]); + if (lo >= hi) continue; + int huh = at_least[lo]; + while (1) { + huh = D.get(huh); + if (huh >= hi) break; + tmp[rkey[huh]] = game[i]; + D.unite(huh,at_least[huh+1]); + // auto it = lef.lb(lo); + // if (it != end(lef) && it->f < hi) { + // tmp[it->s] = game[i]; + // lef.erase(it); + // } else break; + } + } + swap(new_mx,mx); + // dbg("WHOOPS",val,mx); + } + dbg(all_vals); + return mex(all_vals); + // F0R(i,sz(game)) F0R(j,500) tmp_max[i][j] = -MOD; + // set vals; + + // R0F(i,sz(game)) { + // vi mx{MOD}; + // for (int val = 1;;++val) { + // mx.pb(mx.bk); + // ckmin(mx.bk,tmp_max[i][val-1]-game[i]-1); + // if (mx.bk <= -MOD) break; + // } + // // if game[i]-x is in range (mx[i][1],mx[i][0]] -> result is 0 + // // x is in range [game[i]-mx[i][0],game[i]-mx[i][1]) + // // if game[i]-x is in range (mx[i][2],mx[i][1]] -> result is 1 + + // // game[x] is in range [game[i]-mx[i][0],game[i]-mx[i][1]) + // // [game[i]-mx[i][val-1],game[i]-mx[i][val]) -> tmp_max[x][val-1] + // int pos = 0; + // F0R(ind,i) { + // while (game[i]-game[ind] <= mx[pos+1]) pos ++; + // if (tmp_max[ind][pos] == -MOD) tmp_max[ind][pos] = game[i]; + // } + // vals.ins(sz(mx)-2); + // } + // dbg("WUT",vals); + // return mex(vals); + // for (int val = 1;;++val) { + // bool flag = 0; + // F0R(a,sz(game)) { + // int bes = -MOD; + // FOR(b,a+1,sz(game)) + // if (game[b]-game[a] <= mx[b]) ckmax(bes,game[b]-game[a]-1); + // assert(bes <= mx[a]); + // mx[a] = bes; + // if (mx[a] != -MOD) flag = 1; + // } + // dbg("HUH",mx); + // if (!flag) return val; + // } +} + +int dumb() { + V ans(sz(game),vi(sz(game))); + set al; + R0F(j,sz(game)) { + R0F(i,j) { + set bad; + FOR(k,j+1,sz(game)) if (game[k]-game[j] > game[j]-game[i]) + bad.ins(ans[j][k]); + ans[i][j] = mex(bad); + } + set wut; + FOR(k,j+1,sz(game)) wut.insert(ans[j][k]); + al.ins(mex(wut)); + // start[j] = mex(ans[j][?]); + } + // dbg(ans) + // dbg("???",al); + return mex(al); +} + +int main() { + setIO(); + // N = 4; + // game = {14,8,3,2}; + // ps(actual()); + // ps(dumb()); + // ps(find_max({},{15, 10, 6, 7, 5})); + // N = 5; game = {15, 10, 6, 7, 5}; + // dbg(brute()); + // dbg(val()); + // dbg(get_val({1,2,4})); + // dbg(get_val({1,2,0})); + // verify({15, 10, 6, 7, 5}); + // dbg(brute()); + D.init(100005); + // F0R(_,100000) { + // dbg(_); + // N = rng()%10+1; + // game = vi(N); + // trav(t,game) t = rng()%20+1; + // sort(all(game)); game.erase(unique(all(game)),end(game)); + // N = sz(game); + // if (actual() != val()) { + // dbg(game,actual(),val()); + // exit(0); + // } + // dbg("OK"); + // } + // exit(0); + re(N); + int ans = 0; + F0R(i,N) { + rv(M,game); + ans ^= actual(); + } + // dbg(ans); + if (ans) ps("YES"); + else ps("NO"); + + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442A b/code/Benq/1442A new file mode 100644 index 0000000..df840b2 --- /dev/null +++ b/code/Benq/1442A @@ -0,0 +1,233 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// make sure to intialize ALL GLOBAL VARS between tcs! + +int N; +vl A; + +void solve(int tc) { + re(N); A.rsz(N); re(A); + vl fst(N), lst(N); + F0R(i,N-1) { + ll dif = A[i]-A[i+1]; + if (dif >= 0) { + // i+1 on + fst[i] += dif; + } else { + lst[i+1] -= dif; + } + } + ll sum = 0; F0R(i,N) sum += fst[i]; + dbg(fst,lst,sum); + if (sum > A[0]) ps("NO"); + else ps("YES"); + +} + +int main() { + setIO(); + int TC; re(TC); + FOR(i,1,TC+1) solve(i); +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442B b/code/Benq/1442B new file mode 100644 index 0000000..9fa6fc4 --- /dev/null +++ b/code/Benq/1442B @@ -0,0 +1,342 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// make sure to intialize ALL GLOBAL VARS between tcs! + +/** + * Description: modular arithmetic operations + * Source: + * KACTL + * https://codeforces.com/blog/entry/63903 + * https://codeforces.com/contest/1261/submission/65632855 (tourist) + * https://codeforces.com/contest/1264/submission/66344993 (ksun) + * also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal) + * Verification: + * https://open.kattis.com/problems/modulararithmetic + */ + +template struct mint { + static const int mod = MOD; + static constexpr mint rt() { return RT; } // primitive root for FFT + int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int + mint() { v = 0; } + mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); + if (v < 0) v += MOD; } + friend bool operator==(const mint& a, const mint& b) { + return a.v == b.v; } + friend bool operator!=(const mint& a, const mint& b) { + return !(a == b); } + friend bool operator<(const mint& a, const mint& b) { + return a.v < b.v; } + friend void re(mint& a) { ll x; re(x); a = mint(x); } + friend str ts(mint a) { return ts(a.v); } + + mint& operator+=(const mint& m) { + if ((v += m.v) >= MOD) v -= MOD; + return *this; } + mint& operator-=(const mint& m) { + if ((v -= m.v) < 0) v += MOD; + return *this; } + mint& operator*=(const mint& m) { + v = (ll)v*m.v%MOD; return *this; } + mint& operator/=(const mint& m) { return (*this) *= inv(m); } + friend mint pow(mint a, ll p) { + mint ans = 1; assert(p >= 0); + for (; p; p /= 2, a *= a) if (p&1) ans *= a; + return ans; } + friend mint inv(const mint& a) { assert(a.v != 0); + return pow(a,MOD-2); } + + mint operator-() const { return mint(-v); } + mint& operator++() { return *this += 1; } + mint& operator--() { return *this -= 1; } + friend mint operator+(mint a, const mint& b) { return a += b; } + friend mint operator-(mint a, const mint& b) { return a -= b; } + friend mint operator*(mint a, const mint& b) { return a *= b; } + friend mint operator/(mint a, const mint& b) { return a /= b; } +}; + +typedef mint mi; // 5 is primitive root for both common mods +typedef vector vmi; +typedef pair pmi; +typedef vector vpmi; + +vector scmb; // small combinations +void genComb(int SZ) { + scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; + FOR(i,1,SZ) F0R(j,i+1) + scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); +} + +/** + * Description: A set (not multiset!) with support for finding the $n$'th + * element, and finding the index of an element. Change \texttt{null\_type} for map. + * Time: O(\log N) + * Source: KACTL + * https://codeforces.com/blog/entry/11080 + * Verification: many + */ + +#include +#include +using namespace __gnu_pbds; +template using Tree = tree, + rb_tree_tag, tree_order_statistics_node_update>; +#define ook order_of_key +#define fbo find_by_order + +void treeExample() { + Tree t, t2; t.insert(8); + auto it = t.insert(10).f; assert(it == t.lb(9)); + assert(t.ook(10) == 1 && t.ook(11) == 2 && *t.fbo(0) == 8); + t.join(t2); // assuming T < T2 or T > T2, merge t2 into t +} + +// int atMost(Tree& T, int r) { +// return T.ook(r+1); } +// int getSum(Tree& T, int l, int r) { +// return atMost(T,r)-atMost(T,l-1); } + +int getSum(vi& v, int l, int r) { + return v[r]-v[l-1]; +} + +int N,K; +vi A,B; + +void solve(int tc) { + re(N,K); + A.rsz(N), B.rsz(K); re(A,B); + map m; + F0R(i,N) m[A[i]] = i+1; + trav(t,B) { + if (!m.count(t)) { + ps(0); + return; + } + t = m[t]; + } + set inds; + inds.ins(0), inds.ins(N+1); + trav(t,B) inds.ins(t); + vi emp(N+5); + FOR(i,1,N+1) if (!inds.count(i)) emp[i] ++; + FOR(i,1,sz(emp)) emp[i] += emp[i-1]; + mi ans = 1; + // dbg("WHOOPS",B); + trav(t,B) { + auto it = inds.find(t); + int x = *prev(it), y = *next(it); + // dbg("HA",getSum(emp,x+1,y-1)); + ans *= (getSum(emp,x+1,*it-1) > 0) + (getSum(emp,*it+1,y-1) > 0); + inds.erase(it); + } + ps(ans); +} + +int main() { + setIO(); + int TC; re(TC); + FOR(i,1,TC+1) solve(i); +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442C b/code/Benq/1442C new file mode 100644 index 0000000..9135fac --- /dev/null +++ b/code/Benq/1442C @@ -0,0 +1,388 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: modular arithmetic operations + * Source: + * KACTL + * https://codeforces.com/blog/entry/63903 + * https://codeforces.com/contest/1261/submission/65632855 (tourist) + * https://codeforces.com/contest/1264/submission/66344993 (ksun) + * also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal) + * Verification: + * https://open.kattis.com/problems/modulararithmetic + */ + +template struct mint { + static const int mod = MOD; + static constexpr mint rt() { return RT; } // primitive root for FFT + int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int + mint() { v = 0; } + mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); + if (v < 0) v += MOD; } + friend bool operator==(const mint& a, const mint& b) { + return a.v == b.v; } + friend bool operator!=(const mint& a, const mint& b) { + return !(a == b); } + friend bool operator<(const mint& a, const mint& b) { + return a.v < b.v; } + friend void re(mint& a) { ll x; re(x); a = mint(x); } + friend str ts(mint a) { return ts(a.v); } + + mint& operator+=(const mint& m) { + if ((v += m.v) >= MOD) v -= MOD; + return *this; } + mint& operator-=(const mint& m) { + if ((v -= m.v) < 0) v += MOD; + return *this; } + mint& operator*=(const mint& m) { + v = (ll)v*m.v%MOD; return *this; } + mint& operator/=(const mint& m) { return (*this) *= inv(m); } + friend mint pow(mint a, ll p) { + mint ans = 1; assert(p >= 0); + for (; p; p /= 2, a *= a) if (p&1) ans *= a; + return ans; } + friend mint inv(const mint& a) { assert(a.v != 0); + return pow(a,MOD-2); } + + mint operator-() const { return mint(-v); } + mint& operator++() { return *this += 1; } + mint& operator--() { return *this -= 1; } + friend mint operator+(mint a, const mint& b) { return a += b; } + friend mint operator-(mint a, const mint& b) { return a -= b; } + friend mint operator*(mint a, const mint& b) { return a *= b; } + friend mint operator/(mint a, const mint& b) { return a /= b; } +}; + +typedef mint mi; // 5 is primitive root for both common mods +typedef vector vmi; +typedef pair pmi; +typedef vector vpmi; + +vector scmb; // small combinations +void genComb(int SZ) { + scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; + FOR(i,1,SZ) F0R(j,i+1) + scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); +} + +int N,M; +int dp[MX][2][21]; +pi mn[MX][2]; +vi adj[MX][2]; +pi bes[MX]; + +using T = pair; // {transpositions, dist} and {vertex, color} ?? +// priority_queue,greater> pq; + +// BFS minimizing dist for transpositions < 21 +// Dijkstra minimizing {transpositions,dist} + +// void ad(pi loc, pi dist) { +// if (dist.f < 21) { + +// } else { +// if () +// } +// } + +void bfs() { + FOR(i,1,N+1) F0R(j,2) F0R(k,21) dp[i][j][k] = MOD; + deque todo; + auto upd = [&](pi vertColor, pi info) { // OK + if (dp[vertColor.f][vertColor.s][info.f] <= info.s) { + return 0; + } + dp[vertColor.f][vertColor.s][info.f] = info.s; + return 1; + }; + auto ad_front = [&](pi vertColor, pi info) { + if (!upd(vertColor,info)) return; + todo.push_front({info,vertColor}); + }; + auto ad_back = [&](pi vertColor, pi info) { + if (!upd(vertColor,info)) return; + todo.pb({info,vertColor}); + }; + ad_front({1,0},{0,0}); + while (sz(todo)) { // {transpositions,dist}, {vert,color} + T t = todo.ft; todo.pop_front(); + if (t.f.s > dp[t.s.f][t.s.s][t.f.f]) continue; + // dbg("TRANSPOSITIONS","DIST","VERT","COLOR",t,adj[t.s.f][t.s.s]); + if (t.f.f+1 < 21) ad_front({t.s.f,t.s.s^1},{t.f.f+1,t.f.s}); + trav(e,adj[t.s.f][t.s.s]) ad_back({e,t.s.s},{t.f.f,t.f.s+1}); + } +} + +pi dist[MX][2]; + +void dijk() { + dbg("DIJK"); + FOR(i,1,N+1) F0R(j,2) dist[i][j] = {MOD,MOD}; // transpositions dist + priority_queue,greater> todo; + auto ad = [&](pi vertColor, pi info) { + if (ckmin(dist[vertColor.f][vertColor.s],info)) { + todo.push({info,vertColor}); + } + }; + ad({1,0},{0,0}); + while (sz(todo)) { + auto t = todo.top(); todo.pop(); + if (t.f > dist[t.s.f][t.s.s]) continue; + ad({t.s.f,t.s.s^1},{t.f.f+1,t.f.s}); + trav(e,adj[t.s.f][t.s.s]) ad({e,t.s.s},{t.f.f,t.f.s+1}); + } + pi ans = min(dist[N][0],dist[N][1]); + dbg("HA",ans); + ps(pow(mi(2),ans.f)-1+ans.s); + exit(0); + // pi p = {}; +} + +int main() { + setIO(); re(N,M); + F0R(i,M) { + int u,v; re(u,v); + // dbg("ED",u,v); + adj[u][0].pb(v); + adj[v][1].pb(u); + } + bfs(); + int ans = MOD; + F0R(j,2) F0R(k,21) if (dp[N][j][k] != MOD) { + // dbg("HA",j,k,dp[N][j][k]); + ckmin(ans,(1<= 21) continue; + // trav(t,adj[][]) { + + // } + // } + // FOR(i,1,N+1) F0R(j,21) dp[i][j] = MOD; + // dp[1][0] = 1; + // F0R(mov,21) { + + // } + // first get min for each vert + // if some are still not reached + + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442D b/code/Benq/1442D new file mode 100644 index 0000000..3128aa6 --- /dev/null +++ b/code/Benq/1442D @@ -0,0 +1,246 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// some # of whole arrays, then part of an array + +int N,K; // n^2logn ... +vl A[3000]; +ll ans = 0; + +void ad(vl& tmp, int num, ll sum) { + R0F(i,sz(tmp)-num) ckmax(tmp[i+num],tmp[i]+sum); +} + +void divi(int L, int R, const vl& bes) { + if (L == R) { + F0R(i,K+1) if (K-i < sz(A[L])) ckmax(ans,A[L][K-i]+bes[i]); + return; + } + { + vl tmp = bes; + int M = (L+R)/2; + FOR(i,L,M+1) ad(tmp,sz(A[i])-1,A[i].bk); + divi(M+1,R,tmp); + } + { + vl tmp = bes; + int M = (L+R)/2; + FOR(i,M+1,R+1) ad(tmp,sz(A[i])-1,A[i].bk); + divi(L,M,tmp); + } +} + +int main() { + setIO(); re(N,K); + F0R(i,N) { + int t; re(t); + A[i].rsz(t); re(A[i]); + A[i].ins(begin(A[i]),0); + FOR(j,1,t+1) A[i][j] += A[i][j-1]; + } + vl ori(K+1,-INF); ori[0] = 0; + divi(0,N-1,ori); + ps(ans); + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442E b/code/Benq/1442E new file mode 100644 index 0000000..f98cc37 --- /dev/null +++ b/code/Benq/1442E @@ -0,0 +1,286 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// make sure to intialize ALL GLOBAL VARS between tcs! + +int N; +vi A; +V adj; +// V> dp; + +// AR operator+(AR a, AR b) { +// AR res{MOD,MOD,MOD}; +// F0R(x,3) F0R(y,3) { +// int z = x|y; +// if (z < 3) ckmin(res[z],a[x]+b[y]); +// } +// return res; +// } + +// AR dfs(int x, int y) { +// AR res{}; +// if (A[x]) { +// F0R(i,3) res[i] = MOD; +// res[A[x]] = 0; +// } +// trav(t,adj[x]) if (t != y) { +// auto tmp = dfs(t,x); +// res = res+tmp; +// } +// FOR(j,1,3) ckmin(res[0],res[j]+1); +// return res; +// } + +int go(int x) { + vi leaf[3]; + vi deg(N+1); + FOR(i,1,N+1) { + deg[i] = sz(adj[i]); + if (deg[i] == 1) leaf[A[i]].pb(i); + } + int cnt = 0; + while (1) { + cnt ++; + while (sz(leaf[x]) || sz(leaf[0])) { + int ind = sz(leaf[x])?x:0; + int q = leaf[ind].bk; leaf[ind].pop_back(); + trav(t,adj[q]) { + deg[t] --; + if (deg[t] == 1) leaf[A[t]].pb(t); + } + continue; + } + if (sz(leaf[x^3]) == 0) return cnt; + x ^= 3; + } +} + +void solve(int tc) { + re(N); + A = vi(N+1); + adj = V(N+1); + // dp = V>(N+1); + FOR(i,1,N+1) re(A[i]); + F0R(i,N-1) { + int u,v; re(u,v); + adj[u].pb(v), adj[v].pb(u); + } + // dbg("READ",N); + // if (N == 1) { + // ps(1); + // return; + // } + int a = go(1); + int b = go(2); + ps(min(a,b)); + // auto huh = dfs(1,0); + // ps(min(min(huh[0],huh[1]),huh[2])+1); +} + +int main() { + // if all grey -> print 1 + setIO(); + int TC; re(TC); + FOR(i,1,TC+1) solve(i); +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1442F b/code/Benq/1442F new file mode 100644 index 0000000..4426276 --- /dev/null +++ b/code/Benq/1442F @@ -0,0 +1,312 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { cout << endl; } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: sorts vertices such that if there exists an edge x->y, then x goes before y + * Source: KACTL + * Verification: https://open.kattis.com/problems/quantumsuperposition + */ + +template struct TopoSort { + int N, in[SZ]; + vi res, adj[SZ]; + void ae(int x, int y) { adj[x].pb(y), in[y] ++; } + bool sort(int _N) { + N = _N; queue todo; + FOR(i,1,N+1) if (!in[i]) todo.push(i); + while (sz(todo)) { + int x = todo.front(); todo.pop(); res.pb(x); + trav(i,adj[x]) if (!(--in[i])) todo.push(i); + } + return sz(res) == N; + } +}; + +TopoSort TS; +int N,M,T; +int ind; +set adj[MX]; + + +V> eds; + +void de(int a, int b) { + eds.pb({'-',{a,b}}); +} + +void ae(int a, int b) { + eds.pb({'+',{a,b}}); +} + +map done; + +void rem_edges() { + int num = sz(TS.res)-ind; + auto ins = [&](int _, int mask, int xo) { + mask ^= xo; + if (done.count(mask)) return 0; + done[mask] = _; + mask ^= xo; + F0R(i,num) if (xo&(1< +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: Factors integers. + * Time: O(\sqrt N) + * Source: Own + * Verification: https://csacademy.com/contest/ceoi-2018-day-2/task/toys-big/ + */ + +namespace factorBasic { + template vector> factor(T x) { + vector> pri; + for (T i = 2; i*i <= x; ++i) if (x % i == 0) { + int t = 0; + while (x % i == 0) x /= i, t ++; + pri.pb({i,t}); + } + if (x > 1) pri.pb({x,1}); + return pri; + } + /* Note: + * number of operations needed s.t. + * phi(phi(...phi(n)...))=1 + * is O(log n). + * Euler's theorem: a^{\phi(p)}\equiv 1 (mod p), gcd(a,p)=1 + */ + ll phi(ll x) { + trav(a,factor(x)) x -= x/a.f; + return x; + } + template void tour(vector>& v, + vector& V, int ind, T cur) { + if (ind == sz(v)) V.pb(cur); + else { + T mul = 1; + F0R(i,v[ind].s+1) { + tour(v,V,ind+1,cur*mul); + mul *= v[ind].f; + } + } + } + template vector getDivi(T x) { + auto v = factor(x); + vector V; tour(v,V,0,(T)1); sort(all(V)); + return V; + } +} + +using namespace factorBasic; + +int main() { + setIO(); + int T; re(T); + F0R(i,T) { + ll p,q; re(p,q); + if (p%q == 0) { + V> v = factor(q); + ll ans = 0; + trav(t,v) { + ll P = p; + while (P%q == 0) P /= t.f; + ckmax(ans,P); + } + ps(ans); + } else { + ps(p); + } + } + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1444B b/code/Benq/1444B new file mode 100644 index 0000000..f758075 --- /dev/null +++ b/code/Benq/1444B @@ -0,0 +1,313 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: modular arithmetic operations + * Source: + * KACTL + * https://codeforces.com/blog/entry/63903 + * https://codeforces.com/contest/1261/submission/65632855 (tourist) + * https://codeforces.com/contest/1264/submission/66344993 (ksun) + * also see https://github.com/ecnerwala/cp-book/blob/master/src/modnum.hpp (ecnerwal) + * Verification: + * https://open.kattis.com/problems/modulararithmetic + */ + +template struct mint { + static const int mod = MOD; + static constexpr mint rt() { return RT; } // primitive root for FFT + int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int + mint() { v = 0; } + mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD); + if (v < 0) v += MOD; } + friend bool operator==(const mint& a, const mint& b) { + return a.v == b.v; } + friend bool operator!=(const mint& a, const mint& b) { + return !(a == b); } + friend bool operator<(const mint& a, const mint& b) { + return a.v < b.v; } + friend void re(mint& a) { ll x; re(x); a = mint(x); } + friend str ts(mint a) { return ts(a.v); } + + mint& operator+=(const mint& m) { + if ((v += m.v) >= MOD) v -= MOD; + return *this; } + mint& operator-=(const mint& m) { + if ((v -= m.v) < 0) v += MOD; + return *this; } + mint& operator*=(const mint& m) { + v = (ll)v*m.v%MOD; return *this; } + mint& operator/=(const mint& m) { return (*this) *= inv(m); } + friend mint pow(mint a, ll p) { + mint ans = 1; assert(p >= 0); + for (; p; p /= 2, a *= a) if (p&1) ans *= a; + return ans; } + friend mint inv(const mint& a) { assert(a.v != 0); + return pow(a,MOD-2); } + + mint operator-() const { return mint(-v); } + mint& operator++() { return *this += 1; } + mint& operator--() { return *this -= 1; } + friend mint operator+(mint a, const mint& b) { return a += b; } + friend mint operator-(mint a, const mint& b) { return a -= b; } + friend mint operator*(mint a, const mint& b) { return a *= b; } + friend mint operator/(mint a, const mint& b) { return a /= b; } +}; + +typedef mint mi; // 5 is primitive root for both common mods +typedef vector vmi; +typedef pair pmi; +typedef vector vpmi; + +vector scmb; // small combinations +void genComb(int SZ) { + scmb.assign(SZ,vmi(SZ)); scmb[0][0] = 1; + FOR(i,1,SZ) F0R(j,i+1) + scmb[i][j] = scmb[i-1][j]+(j?scmb[i-1][j-1]:0); +} + +/** + * Description: pre-compute factorial mod inverses, + * assumes $MOD$ is prime and $SZ < MOD$. + * Time: O(SZ) + * Source: KACTL + * Verification: https://dmoj.ca/problem/tle17c4p5 + */ + +vi invs, fac, ifac; // make sure to convert to LL before doing any multiplications ... +void genFac(int SZ) { + invs.rsz(SZ), fac.rsz(SZ), ifac.rsz(SZ); + invs[1] = fac[0] = ifac[0] = 1; + FOR(i,2,SZ) invs[i] = int(MOD-(ll)MOD/i*invs[MOD%i]%MOD); + FOR(i,1,SZ) { + fac[i] = int((ll)fac[i-1]*i%MOD); + ifac[i] = int((ll)ifac[i-1]*invs[i]%MOD); + } +} +ll comb(int a, int b) { + if (a < b || b < 0) return 0; + return (ll)fac[a]*ifac[b]%MOD*ifac[a-b]%MOD; +} + +int N; +vi A; +vmi contrib; + +int main() { + setIO(); re(N); A.rsz(2*N); re(A); + genFac(2*N+5); + sort(all(A)); + // contrib.rsz(2*N+1); + // F0R(i,N+1) F0R(j,N+1) contrib[i+j] += mi(fac[i+j])*fac[2*N-i-j]*comb(N,i)*comb(N,j)*abs(i-j); + mi ans = 0; + F0R(i,N) ans -= A[i]; + FOR(i,N,2*N) ans += A[i]; + ans *= comb(2*N,N); + // F0R(i,2*N-1) { + // ans += contrib[i+1]*(A[i+1]-A[i]); + // } + ps(ans); + + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1444C b/code/Benq/1444C new file mode 100644 index 0000000..9d9cd51 --- /dev/null +++ b/code/Benq/1444C @@ -0,0 +1,312 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +vi C; +vb bad; + +bool BAD = 0; + +/** + * Description: Disjoint Set Union with path compression + * and union by size. Add edges and test connectivity. + * Use for Kruskal's or Boruvka's minimum spanning tree. + * Time: O(\alpha(N)) + * Source: CSAcademy, KACTL + * Verification: * + */ + +template struct DSU { + vi e, par_edge; + void init(int N) { + e = par_edge = vi(N,-1); + F0R(i,N) e[i] = i; + } + pi get(int x) { + if (e[x] == x) return {x,0}; + pi q = get(e[x]); + e[x] = q.f; par_edge[x] ^= q.s; + return {e[x],par_edge[x]}; + } + // bool sameSet(int a, int b) { return get(a) == get(b); } + // int size(int x) { return -e[get(x)]; } + bool unite(int x, int y, int z) { // union by size + pi a = get(x); + pi b = get(y); + if (a.f == b.f) { + if (a.s^b.s^z) { + if (mode == 0) bad[C[a.f]] = 1; + else BAD = 1; + } + return 0; + } + e[a.f] = b.f; par_edge[a.f] = a.s^b.s^z; + return 1; + } +}; + +DSU D; +DSU DD; + +/**template T kruskal(int N, vector> ed) { + sort(all(ed)); + T ans = 0; DSU D; D.init(N); // edges that unite are in MST + trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f; + return ans; +}*/ + +vi verts; + +void ad(pi p) { + pi a = D.get(p.f); + pi b = D.get(p.s); + // dbg(p,a,b); + DD.unite(a.f,b.f,a.s^b.s^1); + verts.pb(a.f), verts.pb(b.f); + // dbg("AD",p,a,b,DD.get(a.f),DD.get(b.f)); +} + +void clr() { + trav(t,verts) { + DD.par_edge[t] = -1; + DD.e[t] = t; + } + verts.clear(); +} + +int N,M,K; + +int main() { + setIO(); re(N,M,K); bad.rsz(K+1); + C.rsz(N); re(C); + V> todo; + D.init(N); DD.init(N); + F0R(i,M) { + int a,b; re(a,b); --a,--b; + if (C[a] == C[b]) { + D.unite(a,b,1); + } else { + pi p = {C[a],C[b]}; if (p.f > p.s) swap(p.f,p.s); + todo.pb({p,{a,b}}); + } + } + ll ok = 0; + FOR(i,1,K+1) if (!bad[i]) ok ++; + ll ans = ok*(ok-1)/2; + sort(all(todo)); + // dbg("HA",ans,todo,ok); + for (int i = 0; i < sz(todo); ) { + int I = i; + BAD = 0; + clr(); + while (i < sz(todo) && todo[i].f == todo[I].f) { + ad(todo[i].s); + i ++; + } + // dbg("DONE",todo[I],(int)bad[todo[I].f.f],(int)bad[todo[I].f.s]); + if (!bad[todo[I].f.f] && !bad[todo[I].f.s]) ans -= BAD; + } + ps(ans); + // how man + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1444D b/code/Benq/1444D new file mode 100644 index 0000000..55539a1 --- /dev/null +++ b/code/Benq/1444D @@ -0,0 +1,336 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { pr("\n"); } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +// make sure to intialize ALL GLOBAL VARS between tcs! + +int hor,ver; + +using BB = bitset<1000001>; +V rec; + +PR knapsack(vi v) { + rec = {BB()}; rec.bk[0] = 1; + int sum = 0; + trav(t,v) { + rec.pb(rec.bk|(rec.bk< x = knapsack(L); + PR y = knapsack(P); + if (!sz(x.f) || !sz(y.f)) { + ps("No"); + return; + } + ps("Yes"); + if (sz(x.f) > sz(x.s)) swap(x.f,x.s); + if (sz(y.f) < sz(y.s)) swap(y.f,y.s); + assert(sz(x.f) <= sz(y.f)); + sort(rall(x.f)); sort(all(x.s)); + sort(all(y.f)); sort(rall(y.s)); + vpi tran; + F0R(i,sz(x.f)) { + tran.pb({x.f[i],0}); // smaller + tran.pb({0,y.f[i]}); // larger + } + FOR(i,sz(x.f),sz(y.f)) { + int ind = i-sz(x.f); + tran.pb({-x.s[sz(x.s)-1-ind],0}); // smaller as you go left + tran.pb({0,y.f[i]}); // larger + } + R0F(i,sz(x.s)-(sz(y.f)-sz(x.f))) { + tran.pb({-x.s[i],0}); // smaller as you go left + tran.pb({0,-y.s[i]}); // larger as you go down + } + pi p{0,0}; + trav(t,tran) { + ps(p.f,p.s); + p.f += t.f, p.s += t.s; + } + assert(p == mp(0,0)); + // for (int a = 1; a <= sz(x.f);++a) { + // int b = sz(y.f)-(a-1); + // int c = sz(x.s)-(b-1); + // int d = sz(y.s)-(c-1); + // if (b >= 1 && c >= 1 && d >= 1) { + // sort(all(x.f)); + // sort(all(y.f)); + // sort(all(x.s)); + // sort(all(y.s)); + // vpi tran; + // { + // vi A = last(x.f,a); + // vi B = first(y.f,a-1); + // F0R(i,sz(A)) { + // tran.pb({A[i],0}); + // if (i < sz(B)) tran.pb({0,B[i]}); + // } + // } + // { + // vi A = last(y.f,b); + // vi B = first(x.s,b-1); + // F0R(i,sz(A)) { + // tran.pb({0,A[i]}); + // if (i < sz(B)) tran.pb({-B[i],0}); + // } + // } + // { + // vi A = last(x.s,c); // x.s -> ok + // vi B = first(y.s,c-1); + // F0R(i,sz(A)) { + // tran.pb({-A[i],0}); + // if (i < sz(B)) tran.pb({0,-B[i]}); + // } + // } + // { + // vi A = last(y.s,d); // y.s -> ok + // vi B = first(x.f,d-1); // x.f -> ok + // F0R(i,sz(A)) { + // tran.pb({0,-A[i]}); + // if (i < sz(B)) tran.pb({B[i],0}); + // } + // } + // pi p = {0,0}; + // trav(t,tran) { + // ps(p.f,p.s); + // p.f += t.f, p.s += t.s; + // } + // assert(p == mp(0,0)); + // return; + // } + // } + // dbg(x,y); +} + +int main() { + setIO(); + int TC; re(TC); + FOR(i,1,TC+1) solve(i); +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Benq/1444E b/code/Benq/1444E new file mode 100644 index 0000000..a4ee5bb --- /dev/null +++ b/code/Benq/1444E @@ -0,0 +1,354 @@ +#include +using namespace std; + +using ll = long long; +using ld = long double; +using db = double; +using str = string; // yay python! + +using pi = pair; +using pl = pair; +using pd = pair; + +using vi = vector; +using vb = vector; +using vl = vector; +using vd = vector; +using vs = vector; +using vpi = vector; +using vpl = vector; +using vpd = vector; + +#define tcT template using V = vector; +tcT, size_t SZ> using AR = array; +tcT> using PR = pair; + +// pairs +#define mp make_pair +#define f first +#define s second + +// vectors +#define sz(x) (int)(x).size() +#define all(x) begin(x), end(x) +#define rall(x) (x).rbegin(), (x).rend() +#define sor(x) sort(all(x)) +#define rsz resize +#define ins insert +#define ft front() +#define bk back() +#define pf push_front +#define pb push_back +#define eb emplace_back +#define lb lower_bound +#define ub upper_bound + +// loops +#define FOR(i,a,b) for (int i = (a); i < (b); ++i) +#define F0R(i,a) FOR(i,0,a) +#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) +#define R0F(i,a) ROF(i,0,a) +#define trav(a,x) for (auto& a: x) + +const int MOD = 1e9+7; // 998244353; +const int MX = 2e5+5; +const ll INF = 1e18; // not too close to LLONG_MAX +const ld PI = acos((ld)-1); +const int dx[4] = {1,0,-1,0}, dy[4] = {0,1,0,-1}; // for every grid problem!! +mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); +template using pqg = priority_queue,greater>; + +// helper funcs +constexpr int pct(int x) { return __builtin_popcount(x); } // # of bits set +constexpr int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) +ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up +ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down + +tcT> bool ckmin(T& a, const T& b) { + return b < a ? a = b, 1 : 0; } // set a = min(a,b) +tcT> bool ckmax(T& a, const T& b) { + return a < b ? a = b, 1 : 0; } + +tcTU> T fstTrue(T lo, T hi, U f) { + hi ++; assert(lo <= hi); // assuming f is increasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo)/2; + f(mid) ? hi = mid : lo = mid+1; + } + return lo; +} +tcTU> T lstTrue(T lo, T hi, U f) { + lo --; assert(lo <= hi); // assuming f is decreasing + while (lo < hi) { // find first index such that f is true + T mid = lo+(hi-lo+1)/2; + f(mid) ? lo = mid : hi = mid-1; + } + return lo; +} +tcT> void remDup(vector& v) { // sort and remove duplicates + sort(all(v)); v.erase(unique(all(v)),end(v)); } +tcTU> void erase(T& t, const U& u) { // don't erase + auto it = t.find(u); assert(it != end(t)); + t.erase(u); } // element that doesn't exist from (multi)set + +// INPUT +#define tcTUU tcT, class ...U +tcT> void re(complex& c); +tcTU> void re(pair& p); +tcT> void re(vector& v); +tcT, size_t SZ> void re(AR& a); + +tcT> void re(T& x) { cin >> x; } +void re(db& d) { str t; re(t); d = stod(t); } +void re(ld& d) { str t; re(t); d = stold(t); } +tcTUU> void re(T& t, U&... u) { re(t); re(u...); } + +tcT> void re(complex& c) { T a,b; re(a,b); c = {a,b}; } +tcTU> void re(pair& p) { re(p.f,p.s); } +tcT> void re(vector& x) { trav(a,x) re(a); } +tcT, size_t SZ> void re(AR& x) { trav(a,x) re(a); } +tcT> void rv(int& n, vector& x) { re(n); x.rsz(n); trav(a,x) re(a); } + +// TO_STRING +#define ts to_string +str ts(char c) { return str(1,c); } +str ts(const char* s) { return (str)s; } +str ts(str s) { return s; } +str ts(bool b) { + #ifdef LOCAL + return b ? "true" : "false"; + #else + return ts((int)b); + #endif +} +tcT> str ts(complex c) { + stringstream ss; ss << c; return ss.str(); } +str ts(vector v) { + str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); + res += "}"; return res; } +template str ts(bitset b) { + str res = ""; F0R(i,SZ) res += char('0'+b[i]); + return res; } +tcTU> str ts(pair p); +tcT> str ts(T v) { // containers with begin(), end() + #ifdef LOCAL + bool fst = 1; str res = "{"; + for (const auto& x: v) { + if (!fst) res += ", "; + fst = 0; res += ts(x); + } + res += "}"; return res; + #else + bool fst = 1; str res = ""; + for (const auto& x: v) { + if (!fst) res += " "; + fst = 0; res += ts(x); + } + return res; + + #endif +} +tcTU> str ts(pair p) { + #ifdef LOCAL + return "("+ts(p.f)+", "+ts(p.s)+")"; + #else + return ts(p.f)+" "+ts(p.s); + #endif +} + +// OUTPUT +tcT> void pr(T x) { cout << ts(x); } +tcTUU> void pr(const T& t, const U&... u) { + pr(t); pr(u...); } +void ps() { cout << endl; } // print w/ spaces +tcTUU> void ps(const T& t, const U&... u) { + pr(t); if (sizeof...(u)) pr(" "); ps(u...); } + +// DEBUG +void DBG() { cerr << "]" << endl; } +tcTUU> void DBG(const T& t, const U&... u) { + cerr << ts(t); if (sizeof...(u)) cerr << ", "; + DBG(u...); } +#ifdef LOCAL // compile with -DLOCAL, chk -> fake assert + #define dbg(...) cerr << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) + #define chk(...) if (!(__VA_ARGS__)) cerr << "Line(" << __LINE__ << ") -> function(" \ + << __FUNCTION__ << ") -> CHK FAILED: (" << #__VA_ARGS__ << ")" << "\n", exit(0); +#else + #define dbg(...) 0 + #define chk(...) 0 +#endif + +// FILE I/O +void setIn(str s) { freopen(s.c_str(),"r",stdin); } +void setOut(str s) { freopen(s.c_str(),"w",stdout); } +void unsyncIO() { cin.tie(0)->sync_with_stdio(0); } +void setIO(str s = "") { + unsyncIO(); + // cin.exceptions(cin.failbit); + // throws exception when do smth illegal + // ex. try to read letter into int + if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO +} + +/** + * Description: Disjoint Set Union with path compression + * and union by size. Add edges and test connectivity. + * Use for Kruskal's or Boruvka's minimum spanning tree. + * Time: O(\alpha(N)) + * Source: CSAcademy, KACTL + * Verification: * + */ + +struct DSU { + vi e; void init(int N) { e = vi(N,-1); } + int get(int x) { return e[x] < 0 ? x : e[x] = get(e[x]); } + bool sameSet(int a, int b) { return get(a) == get(b); } + int size(int x) { return -e[get(x)]; } + bool unite(int x, int y) { // union by size + x = get(x), y = get(y); if (x == y) return 0; + if (e[x] > e[y]) swap(x,y); + e[x] += e[y]; e[y] = x; return 1; + } +}; + +/**template T kruskal(int N, vector> ed) { + sort(all(ed)); + T ans = 0; DSU D; D.init(N); // edges that unite are in MST + trav(a,ed) if (D.unite(a.s.f,a.s.s)) ans += a.f; + return ans; +}*/ + +int N, label[101]; +vi adj[101]; +vb used; +int dist[101][101]; +V> ed; + +// void dfs(int x, int pre = -1) { +// x = label[x]; +// trav(t,ADJ[x]) if (label[t.f] != pre && label[t.f] != x) { +// // dbg("DFS",x,label[t.f]); +// dfs(t.f,x); +// if (!used[x] && !used[label[t.f]]) { +// used[x] = used[label[t.f]] = 1; +// ed.pb(t); +// } +// } +// } + +vb active; + +void query(int u, int v) { + ps("?",u,v); + int res; re(res); + FOR(i,1,N+1) { + if (dist[i][res] > dist[i][u^v^res]) active[i] = 0; + } +} + +using BIG = __int128; + +BIG po(int i) { return BIG(1)<> res; + trav(t,adj[x]) if (t != pre) res.pb({go(t,x),t}); + sort(rall(res)); + V en; + auto ok = [&](BIG pref) { + en = V(sz(res)); + V tmp; trav(t,res) tmp.pb(t.f); + R0F(i,N) if (pref&po(i)) { + BIG mx = 0; trav(t,tmp) ckmax(mx,t); + F0R(j,sz(tmp)) if (tmp[j] == mx) { + tmp[j] -= po(i); en[j] ^= po(i); + break; + } + } + trav(t,tmp) if (t >= 0) return 0; + return 1; + }; + BIG cur = po(N)-1; + R0F(i,N) { + cur -= po(i); + if (!ok(cur)) cur += po(i); + } + assert(ok(cur)); + F0R(i,sz(res)) { + int j = N-1; + while (((res[i].f>>j)&1) == ((en[i]>>j)&1)) j --; + ed.pb({j,{x,res[i].s}}); + } + return cur; +} + +int main() { + re(N); + FOR(i,1,N+1) FOR(j,1,N+1) if (i != j) dist[i][j] = MOD; + F0R(i,N-1) { + int u,v; re(u,v); + adj[u].pb(v); adj[v].pb(u); + dist[u][v] = dist[v][u] = 1; + } + FOR(k,1,N+1) FOR(i,1,N+1) FOR(j,1,N+1) ckmin(dist[i][j],dist[i][k]+dist[k][j]); + // dbg(dist[1][5]); + // FOR(i,1,N+1) label[i] = i; + // vpi all_eds; + // while (1) { + // set labs; + // FOR(i,1,N+1) ADJ[i].clear(); + // FOR(i,1,N+1) dbg(i,label[i]); + // FOR(i,1,N+1) { + // labs.ins(label[i]); + // ADJ[label[i]].ins(end(ADJ[label[i]]),all(adj[i])); + // } + // dbg(labs); // exit(0); + // if (sz(labs) == 1) break; + // ed.clear(); + // used = vb(N+1); + // dfs(1); + // // dbg(ed); + // trav(t,ed) { + // int pre = label[t.f]; + // // dbg("HA",t,label[t.f],label[t.s]); + // FOR(i,1,N+1) if (label[i] == pre) label[i] = label[t.s]; + // } + // all_eds.ins(end(all_eds),all(ed)); + // } + // reverse(all(all_eds)); + BIG b = go(1); + sort(rall(ed)); + active = vb(N+1,1); + dbg(ed,(ll)b); + while (1) { + vi res; + FOR(i,1,N+1) if (active[i]) res.pb(i); + if (sz(res) == 1) { + ps('!',res[0]); + exit(0); + } + trav(t,ed) if (active[t.s.f] && active[t.s.s]) { + query(t.s.f,t.s.s); + break; + } + } + // dbg(all_eds); exit(0); + + + // what is the worst case #? + // centroid decomp, but what if star? + + // you should actually read the stuff at the bottom +} + +/* stuff you should look for + * int overflow, array bounds + * special cases (n=1?) + * do smth instead of nothing and stay organized + * WRITE STUFF DOWN + * DON'T GET STUCK ON ONE APPROACH +*/ + diff --git a/code/Radewoosh/1408B b/code/Radewoosh/1408B new file mode 100644 index 0000000..8a30ddd --- /dev/null +++ b/code/Radewoosh/1408B @@ -0,0 +1,97 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n, k; + +int tab[nax]; + +void test() +{ + scanf("%d%d", &n, &k); + int roz=0; + for (int i=1; i<=n; i++) + { + scanf("%d", &tab[i]); + if (i>1) + roz+=(tab[i]!=tab[i-1]); + } + if (roz && k==1) + { + printf("-1\n"); + } + else + { + if (k==1) + { + printf("1\n"); + } + else + { + k--; + printf("%d\n", max((roz+k-1)/k, 1)); + } + } +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1408G b/code/Radewoosh/1408G new file mode 100644 index 0000000..08359ae --- /dev/null +++ b/code/Radewoosh/1408G @@ -0,0 +1,138 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1507; +const ll mod=998244353; + +int n; + +int tab[nax][nax]; + +void dod(ll &a, ll b) +{ + a=(a+b)%mod; +} + +vll multi(const vll &a, const vll &b) +{ + int ra=a.size(); + int rb=b.size(); + vll ret(ra+rb-1); + for (int i=0; i> kra; + for (int i=1; i<=n; i++) + for (int j=1; j +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=(1<<16); +const int vax=20; +const ll mod=998244353; +const ll o2=(mod+1)/2; + +int n, k, c; +int tab[nax]; + +ll ruch[vax][nax]; + +ll inv(ll a) +{ + if (a<=1) + return a; + return inv(mod%a)*(mod-mod/a)%mod; +} + +ll pom[nax]; + +ll dp[vax][nax]; + +ll newt[vax][vax]; + +void dod(ll &a, ll b) +{ + a=(a+b)%mod; +} + +void cofnij(ll *x) +{ + for (int i=0; i mapa[vax]; + +int zm[nax]; + +int main() +{ + for (int i=0; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n, m; + +vector graf[nax]; + +int wagwie[nax]; + +pii kra[nax]; +int wagkra[nax]; + +int suma[nax]; + +int juz[nax]; + +void wypisz() +{ + vi wek; + for (int i=1; i<=n; i++) + if (wagwie[i]) + wek.push_back(i); + printf("%d\n", (int)wek.size()); + for (int i : wek) + printf("%d ", i); + printf("\n"); + for (int i=1; i<=m; i++) + printf("%d %d %d\n", kra[i].first, kra[i].second, wagkra[i]); + exit(0); +} + +int spoko(int v, int waga) +{ + for (pii i : graf[v]) + if (juz[i.first] && suma[i.first]==waga) + return 0; + return 1; +} + +int spoko(int v, int waga, const vi &tylko) +{ + for (int i : tylko) + if (suma[i]==waga) + return 0; + return 1; +} + +int los(int a, int b) +{ + return a+rand()%(b-a+1); +} + +void zmienwie(int v, int waga) +{ + if (wagwie[v]==waga) + return; + suma[v]-=wagwie[v]; + wagwie[v]=waga; + suma[v]+=wagwie[v]; +} + +void zmienkra(int v, int waga) +{ + if (wagkra[v]==waga) + return; + suma[kra[v].first]-=wagkra[v]; + suma[kra[v].second]-=wagkra[v]; + wagkra[v]=waga; + suma[kra[v].first]+=wagkra[v]; + suma[kra[v].second]+=wagkra[v]; +} + +int bylo[nax]; + +void dfs(int v, vi &kol) +{ + if (bylo[v]) + return; + bylo[v]=1; + for (pii i : graf[v]) + dfs(i.first, kol); + kol.push_back(v); +} + +void proba() +{ + debug() << "proba"; + for (int i=1; i<=n; i++) + wagwie[i]=suma[i]=juz[i]=0; + for (int i=1; i<=m; i++) + wagkra[i]=0; + vi kolp(n); + iota(kolp.begin(), kolp.end(), 1); + shandom_ruffle(kolp.begin(), kolp.end()); + for (int i=1; i<=n; i++) + shandom_ruffle(graf[i].begin(), graf[i].end()); + vi kol; + for (int i=1; i<=n; i++) + bylo[i]=0; + for (int i : kolp) + dfs(i, kol); + + static int pier=0; + if (!pier) + { + pier=1; + for (int i=1; i<=m; i++) + zmienkra(i, (kra[i].first+kra[i].second<=n)*2); + zmienwie(n/2, 1); + } + else + { + for (int i=1; i<=n; i++) + zmienwie(i, los(0, 1)); + for (int i=1; i<=m; i++) + zmienkra(i, los(0, 2)); + } + int ile=0; + for (int v : kol) + { + vi poki; + for (pii i : graf[v]) + if (juz[i.first]) + poki.push_back(i.first); + int wdol=0; + int wgor=0; + for (pii i : graf[v]) + { + if (!juz[i.first]) + { + wdol+=wagkra[i.second]; + wgor+=2-wagkra[i.second]; + } + } + if (wagwie[v]) + wdol++; + else + wgor++; + vector poki2; + for (pii i : graf[v]) + if (juz[i.first]) + poki2.push_back(i); + for (int h=0; h<20 && !spoko(v, suma[v], poki); h++) + { + set jest; + for (int i : poki) + jest.insert(suma[i]); + for (int w=suma[v]-wdol; w<=suma[v]+wgor; w++) + { + if (!jest.count(w)) + { + if (wsuma[v] && !wagwie[v]) + zmienwie(v, 1); + for (pii i : graf[v]) + { + if (!juz[i.first]) + { + while(wagkra[i.second] && suma[v]>w) + zmienkra(i.second, wagkra[i.second]-1); + while(wagkra[i.second]<2 && suma[v] +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n; + +int tab[nax]; + +void test() +{ + scanf("%d", &n); + int sum=0; + for (int i=1; i<=n; i++) + { + scanf("%d", &tab[i]); + sum+=tab[i]; + } + if (!sum) + { + printf("NO\n"); + return; + } + sort(tab+1, tab+1+n); + if (sum>0) + reverse(tab+1, tab+1+n); + printf("YES\n"); + for (int i=1; i<=n; i++) + printf("%d ", tab[i]); + printf("\n"); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1427B b/code/Radewoosh/1427B new file mode 100644 index 0000000..6e40f5a --- /dev/null +++ b/code/Radewoosh/1427B @@ -0,0 +1,118 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n, k; + +char wcz[nax]; + +void test() +{ + scanf("%d%d", &n, &k); + scanf("%s", wcz+1); + int wyn=0; + vi wek{nax}; + int dob=0; + for (int i=1; i<=n; i++) + { + if (wcz[i]=='W') + { + wyn++; + dob++; + if (wcz[i-1]=='W') + wyn++; + for (int j=i+1; j<=n; j++) + { + if (wcz[j]=='W') + { + if (j!=i+1) + wek.push_back({j-i-1}); + break; + } + } + } + } + if (!wyn) + { + wyn=max(0, 2*k-1); + printf("%d\n", wyn); + return; + } + k=min(k, n-dob); + sort(wek.begin(), wek.end()); + debug() << imie(wyn); + for (int i : wek) + { + while(k && i) + { + k--; + i--; + wyn+=2; + if (!i) + wyn++; + } + } + printf("%d\n", wyn); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1427C b/code/Radewoosh/1427C new file mode 100644 index 0000000..0789b3f --- /dev/null +++ b/code/Radewoosh/1427C @@ -0,0 +1,121 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const int vax=507; + +int r, n; + +int czas[nax]; +pii gdz[nax]; + +int dp[vax][vax]; + +int dpon[nax]; + +void maxi(int &a, int b) +{ + a=max(a, b); +} + +int main() +{ + scanf("%d%d", &r, &n); + for (int i=1; i<=n; i++) + scanf("%d%d%d", &czas[i], &gdz[i].first, &gdz[i].second); + gdz[0]={1, 1}; + for (int i=1; i<=n; i++) + { + dpon[i]=-nax; + } + for (int i=1; i<=r; i++) + for (int j=1; j<=r; j++) + dp[i][j]=-nax; + int w=0; + int wyn=0; + dp[1][1]=0; + for (int h=1; h<=czas[n]; h++) + { + for (int i=w; i>=0 && czas[i]>=h-r; i--) + { + int roz=h-czas[i]; + if (gdz[i].first-roz>=1) + maxi(dp[gdz[i].first-roz][gdz[i].second], dpon[i]); + if (gdz[i].first+roz<=r) + maxi(dp[gdz[i].first+roz][gdz[i].second], dpon[i]); + } + if (w=1) + maxi(dpon[i], dp[gdz[i].first][gdz[i].second-roz]+1); + if (gdz[i].second+roz<=r) + maxi(dpon[i], dp[gdz[i].first][gdz[i].second+roz]+1); + } + } + //~ debug() << range(dpon+1, dpon+1+n); + for (int i=1; i<=n; i++) + maxi(wyn, dpon[i]); + printf("%d\n", wyn); + return 0; +} + diff --git a/code/Radewoosh/1427D b/code/Radewoosh/1427D new file mode 100644 index 0000000..e69fb75 --- /dev/null +++ b/code/Radewoosh/1427D @@ -0,0 +1,151 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=107; + +int n; +int tab[nax]; + +vector wyn; + +vi oczysc(vi wek) +{ + vi ret; + for (int i : wek) + if (i) + ret.push_back(i); + return ret; +} + +void zrob(vi wek) +{ + wek=oczysc(wek); + vector podz; + int v=0; + for (int i : wek) + { + vi ter; + for (int j=0; jtab[i+1]) + return 0; + return 1; +} + +int main() +{ + scanf("%d", &n); + for (int i=1; i<=n; i++) + scanf("%d", &tab[i]); + + while(!sor()) + { + debug() << range(tab+1, tab+1+n); + int moze=1; + int gm, gn; + while(1) + { + for (int i=1; i<=n; i++) + { + if (tab[i]==moze) + gm=i; + if (tab[i]==moze+1) + gn=i; + } + if (gn>gm) + { + moze++; + continue; + } + break; + } + debug() << imie(moze); + int tnij=gn; + while(tab[tnij+1]==tab[tnij]+1) + tnij++; + zrob({gn-1, tnij-gn+1, gm-tnij, n-gm}); + } + + printf("%d\n", (int)wyn.size()); + for (vi i : wyn) + { + printf("%d", (int)i.size()); + for (int j : i) + printf(" %d", j); + printf("\n"); + } + return 0; +} + diff --git a/code/Radewoosh/1427E b/code/Radewoosh/1427E new file mode 100644 index 0000000..0809ef2 --- /dev/null +++ b/code/Radewoosh/1427E @@ -0,0 +1,182 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=107; +const ll lim=1e15; +const int d=50; + +ll n; + +int najw(ll v) +{ + if (!v) + return 0; + return 64-__builtin_clzll(v); +} + +unordered_set setel[nax]; +vll wek[nax]; + +int znam(ll v) +{ + int g=najw(v); + return setel[g].count(v); +} + +vector> wyn; + +void poznaj(ll x) +{ + setel[najw(x)].insert(x); + wek[najw(x)].push_back(x); +} + +void dodaj(ll a, ll b) +{ + wyn.push_back({0, {a, b}}); + poznaj(a+b); +} + +void xoruj(ll a, ll b) +{ + wyn.push_back({1, {a, b}}); + poznaj(a^b); +} + +ll los() +{ + return rand()*1000000LL+rand(); +} + +int los(int a, int b) +{ + return a+los()%(b-a+1); +} + +ll wylosuj(int v) +{ + if (wek[v].empty()) + return -1; + int r=wek[v].size(); + return wek[v][los(0, r-1)]; +} + +void randomowo() +{ + int jeszcze=40*1000; + while(jeszcze) + { + int a=los(1, d); + int b=los(1, d); + ll x=wylosuj(a); + ll y=wylosuj(b); + if (x<0 || y<0) + continue; + if (los(0, 1)) + { + if (x+y<=lim && !znam(x+y)) + { + dodaj(x, y); + jeszcze--; + } + } + else + { + if ((x^y)<=lim && !znam(x^y)) + { + xoruj(x, y); + jeszcze--; + } + } + } +} + +void losowe(int v) +{ + if (wek[v].empty()) + return; + int jeszcze=1000; + int supjeszcze=1000*30; + while(jeszcze>0 && supjeszcze>0) + { + supjeszcze--; + ll x=wylosuj(v); + ll y=wylosuj(v); + if ((x^y)<=lim && !znam(x^y)) + { + xoruj(x, y); + jeszcze--; + } + } +} + +int main() +{ + scanf("%lld", &n); + poznaj(n); + randomowo(); + for (int i=d; i; i--) + losowe(i); + + //~ for (int i=0; i<=d; i++) + //~ debug() << imie(i) << imie(wek[i].size()); + + assert(!wek[1].empty()); + + printf("%d\n", (int)wyn.size()); + for (auto i : wyn) + printf("%lld %c %lld\n", i.second.first, "+^"[i.first], i.second.second); + return 0; +} + diff --git a/code/Radewoosh/1427F b/code/Radewoosh/1427F new file mode 100644 index 0000000..4b46738 --- /dev/null +++ b/code/Radewoosh/1427F @@ -0,0 +1,116 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1207; + +int n; +int tab[nax]; + +vi graf[nax]; + +vi kto[nax]; +int typ[nax]; + +vector wyn; + +int main() +{ + scanf("%d", &n); + for (int i=1; i<=3*n; i++) + { + int x; + scanf("%d", &x); + tab[x]=1; + } + vector stos; + stos.push_back({0, -1}); + int v=0; + for (int i=1; i<=6*n; i++) + { + if (stos.back().second!=tab[i]) + { + v++; + typ[v]=tab[i]; + stos.push_back({v, tab[i]}); + } + kto[stos.back().first].push_back(i); + if ((int)kto[stos.back().first].size()==3) + { + int x=stos.back().first; + stos.pop_back(); + graf[stos.back().first].push_back(x); + } + } + assert(stos.size()==1); + vi korz=graf[0]; + for (int h=0; !korz.empty(); h^=1) + { + int g=-1; + for (int i=0; i<(int)korz.size(); i++) + if (typ[korz[i]]==h && (g==-1 || graf[korz[g]].size()=0); + int x=korz[g]; + swap(korz[g], korz.back()); + korz.pop_back(); + wyn.push_back(kto[x]); + for (int i : graf[x]) + korz.push_back(i); + } + reverse(wyn.begin(), wyn.end()); + for (vi i : wyn) + printf("%d %d %d\n", i[0], i[1], i[2]); + return 0; +} + diff --git a/code/Radewoosh/1427G b/code/Radewoosh/1427G new file mode 100644 index 0000000..478e23c --- /dev/null +++ b/code/Radewoosh/1427G @@ -0,0 +1,266 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=207; +const ll inf=10; + +int n; +ll tab[nax][nax]; + +int k; +vll wary; + +ll wyn; + +using T = int; +struct Flow { + struct E { + int dest; + T orig, *lim, *rev; + }; + int zr, uj, n = 0; + vector> ts; + vector> graf; + vector ptr, odl; + vi bylo; + void vert(int v) { + n = max(n, v + 1); + graf.resize(n); + ptr.resize(n); + odl.resize(n); + bylo.resize(n); + } + bool iszero(T v) { + return !v; // Zmieni\u0107 dla doubli. + } + void bfs() { + fill(odl.begin(), odl.end(), 0); + vector kol = {zr}; + odl[zr] = 1; + for (int i = 0; i < (int) kol.size(); i++) { + for (E& e : graf[kol[i]]) { + if (!odl[e.dest] and !iszero(*e.lim)) { + odl[e.dest] = odl[kol[i]] + 1; + kol.push_back(e.dest); + } + } + } + } + T dfs(int v, T lim) { + if (v == uj) return lim; + T ret = 0, wez; + bylo[v]=1; + for (int& i = ptr[v]; i < (int) graf[v].size(); i++) { + E& e = graf[v][i]; + //~ if (odl[e.dest] == odl[v] + 1 and !iszero(*e.lim) and + if (!iszero(*e.lim) and !bylo[e.dest] and + !iszero(wez = dfs(e.dest, min(*e.lim, lim)))) { + ret += wez; + *e.lim -= wez; + *e.rev += wez; + lim -= wez; + if (iszero(lim)) break; + } + } + return ret; + } + void add_edge(int u, int v, T lim, bool bi = false /* bidirectional? */) { + vert(max(u, v)); + T *a = new T(lim), *b = new T(lim * bi); + ts.emplace_back(a); + ts.emplace_back(b); + graf[u].push_back(E{v, lim, a, b}); + graf[v].push_back(E{u, lim * bi, b, a}); + } + T dinic(int zr_, int uj_) { + zr = zr_; uj = uj_; + vert(max(zr, uj)); + T ret = 0; + while (true) { + //~ bfs(); + fill(bylo.begin(), bylo.end(), 0); + fill(ptr.begin(), ptr.end(), 0); + const T sta = dfs(zr, numeric_limits::max()); // Dla doubli mo\u017cna da\u0107 + if (iszero(sta)) break; // infinity() zamiast + ret += sta; // max(). + } + return ret; + } + vector cut() { + vector ret; + bfs(); + for (int i = 0; i < n; i++) + if (odl[i]) + ret.push_back(i); + return ret; + } + map, T> get_flowing() { // Tam gdzie plynie 0 mo\u017ce nie by\u0107 + map, T> ret; // kraw\u0119dzi. + for (int i = 0; i < n; i++) + for (E& e : graf[i]) + if (*e.lim < e.orig) + ret[make_pair(i, e.dest)] += e.orig - *e.lim; + for (auto& i : ret) { + const pair rev{i.first.second, i.first.first}; + const T x = min(i.second, ret[rev]); + i.second -= x; + ret[rev] -= x; + } + return ret; + } +}; + +Flow janusz; + +int tonum(int a, int b) +{ + a--; + b--; + return a*n+b+1; +} + +int tonum(pii v) +{ + return tonum(v.first, v.second); +} + +int stary; + +void unikra(int a, int b, ll w) +{ + if (!w) + return; + janusz.add_edge(a, b, w, 0); +} + +void bikra(int a, int b, ll w) +{ + if (!w) + return; + janusz.add_edge(a, b, w, 1); +} + +void solve(ll limit, ll zm) +{ + int teraz=0; + for (int i=1; i<=n; i++) + { + for (int j=1; j<=n; j++) + { + if (tab[i][j]==limit-zm) + { + teraz++; + unikra(tonum(i, j), n*n+1, 2*inf); + } + } + } + int wez=janusz.dinic(0, n*n+1); + wez-=teraz*inf; + //~ debug() << imie(limit) << imie(wez); + stary+=wez; + wyn+=stary*zm; + //~ debug() << imie(limit) << imie(stary) << imie(teraz) << imie(zm); +} + +int main() +{ + scanf("%d", &n); + for (int i=1; i<=n; i++) + { + for (int j=1; j<=n; j++) + { + scanf("%lld", &tab[i][j]); + if (tab[i][j]>0) + wary.push_back(tab[i][j]); + } + } + for (int i=1; i<=n; i++) + { + for (int j=1; j<=n; j++) + { + int g=tonum(i, j); + if (tab[i][j]==-1) + continue; + ll dol=0; + ll gor=0; + if (tab[i][j]) + dol=inf; + unikra(0, g, dol); + unikra(g, n*n+1, gor); + } + } + for (int i=1; i<=n; i++) + { + for (int j=1; j<=n; j++) + { + if (i+1<=n && tab[i][j]>=0 && tab[i+1][j]>=0) + bikra(tonum(i, j), tonum(i+1, j), 1); + if (j+1<=n && tab[i][j]>=0 && tab[i][j+1]>=0) + bikra(tonum(i, j), tonum(i, j+1), 1); + } + } + sort(wary.begin(), wary.end()); + wary.resize(unique(wary.begin(), wary.end())-wary.begin()); + //~ debug() << imie(wary); + k=wary.size(); + ll ost=wary[0]; + for (int i=1; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +ll a, b, c, d; + +void test() +{ + scanf("%lld%lld%lld%lld", &a, &b, &c, &d); + ll wyn=abs(a-c)+abs(b-d)+(a!=c && b!=d)*2; + printf("%lld\n", wyn); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1428B b/code/Radewoosh/1428B new file mode 100644 index 0000000..43f75ad --- /dev/null +++ b/code/Radewoosh/1428B @@ -0,0 +1,93 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n; +char wcz[nax]; + +int ile; + +int lew, pra; + +void test() +{ + scanf("%d", &n); + scanf("%s", wcz); + ile=0; + for (int i=0; i') + pra=1; + } + if (!lew || !pra) + ile=n; + printf("%d\n", ile); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1428C b/code/Radewoosh/1428C new file mode 100644 index 0000000..2d9e5ac --- /dev/null +++ b/code/Radewoosh/1428C @@ -0,0 +1,87 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +char wcz[nax]; + +int ile; + +void test() +{ + scanf("%s", wcz+1); + ile=0; + for (int i=1; wcz[i]; i++) + { + if (wcz[i]=='B' && ile>0) + { + ile--; + } + else + { + ile++; + } + } + printf("%d\n", ile); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1428D b/code/Radewoosh/1428D new file mode 100644 index 0000000..4bccb31 --- /dev/null +++ b/code/Radewoosh/1428D @@ -0,0 +1,134 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n; + +int tab[nax]; + +vi wek[nax]; +vector wyn; + +void buduj(int kol, int wys) +{ + wyn.push_back({n+1-wys, kol}); + wek[kol].push_back(wys); +} + +void nope() +{ + printf("-1\n"); + exit(0); +} + +vi jedy[4]; + +int daj(int v) +{ + if (v==2) + { + if (jedy[1].empty()) + nope(); + int x=jedy[1].back(); + jedy[1].pop_back(); + return x; + } + for (int i=3; i; i--) + { + if (!jedy[i].empty()) + { + int x=jedy[i].back(); + jedy[i].pop_back(); + return x; + } + } + nope(); +} + +int main() +{ + scanf("%d", &n); + for (int i=1; i<=n; i++) + scanf("%d", &tab[i]); + for (int i=n; i; i--) + { + if (!tab[i]) + continue; + if (tab[i]==1) + { + buduj(i, n+1-i); + jedy[1].push_back(i); + continue; + } + int x=daj(tab[i]); + if (tab[i]==2) + { + buduj(i, n+1-x); + jedy[2].push_back(i); + } + else + { + buduj(i, n+1-i); + buduj(x, n+1-i); + jedy[3].push_back(i); + } + } + + printf("%d\n", (int)wyn.size()); + for (pii i : wyn) + printf("%d %d\n", i.first, i.second); + return 0; +} + diff --git a/code/Radewoosh/1428E b/code/Radewoosh/1428E new file mode 100644 index 0000000..2a99484 --- /dev/null +++ b/code/Radewoosh/1428E @@ -0,0 +1,102 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const ll inf=1e18; + +int n, k; + +ll tab[nax]; + +ll kwa(ll v) +{ + return v*v; +} + +ll koszt(int v, ll cz) +{ + if (cz>tab[v]) + return inf; + ll dz=tab[v]/cz; + ll resz=tab[v]%cz; + return kwa(dz+1)*resz+kwa(dz)*(cz-resz); +} + +ll tu[nax]; + +int main() +{ + scanf("%d%d", &n, &k); + for (int i=1; i<=n; i++) + { + tu[i]=1; + scanf("%lld", &tab[i]); + } + priority_queue>kol; + for (int i=1; i<=n; i++) + kol.push({koszt(i, 1)-koszt(i, 2), i}); + for (int h=n+1; h<=k; h++) + { + int v=kol.top().second; + kol.pop(); + tu[v]++; + kol.push({koszt(v, tu[v])-koszt(v, tu[v]+1), v}); + } + ll wyn=0; + for (int i=1; i<=n; i++) + wyn+=koszt(i, tu[i]); + printf("%lld\n", wyn); + return 0; +} + diff --git a/code/Radewoosh/1428F b/code/Radewoosh/1428F new file mode 100644 index 0000000..eff04f0 --- /dev/null +++ b/code/Radewoosh/1428F @@ -0,0 +1,122 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const ll inf=1e18; + +int n; + +char wcz[nax]; + +int tab[nax]; + +ll wyn; + +vector wek;//dlu, war + +ll ter, sum; + +void pop1() +{ + sum-=wek.back().second; + wek.back().first--; + if (!wek.back().first) + wek.pop_back(); +} + +void solve() +{ + for (int i=1; i<=n; i++) + { + if (tab[i]==1) + { + ter++; + wyn-=(ter*(ter-1))/2; + } + else + { + for (int j=1; j<=ter; j++) + pop1(); + for (int j=ter; j; j--) + { + sum+=j; + wek.push_back({1, j}); + } + ter=0; + } + ll kum=1; + while(!wek.empty() && wek.back().second<=ter) + { + sum-=wek.back().first*wek.back().second; + kum+=wek.back().first; + wek.pop_back(); + } + wek.push_back({kum, ter}); + sum+=wek.back().first*wek.back().second; + wyn+=sum; + //~ debug() << i << " " << wek << " " << sum; + } +} + +int main() +{ + scanf("%d", &n); + scanf("%s", wcz+1); + for (int i=1; i<=n; i++) + tab[i]=(wcz[i]-'0'); + solve(); + printf("%lld\n", wyn); + return 0; +} + diff --git a/code/Radewoosh/1428G1 b/code/Radewoosh/1428G1 new file mode 100644 index 0000000..9cd0d00 --- /dev/null +++ b/code/Radewoosh/1428G1 @@ -0,0 +1,157 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const int vax=9; +const ll inf=1e18; +const int d=6; +const int m=1000*1000; + +int k; +ll fort[vax]; + +ll dp[vax][nax]; + +ll pom[nax]; + +deque kol[3]; + +int przej(int v) +{ + if (v<=(k-1)*9) + return (v/3); + if (v%3) + return (k-1)*3; + return v/3; +} + +void przejscia(int v) +{ + for (int i=0; i=0) + //~ dp[v][i]=max(dp[v][i], pom[j]+przej(i-j)*fort[v]); + //~ } + //~ debug() << v << " " << range(dp[v], dp[v]+60) << " " << imie(pom[30]) << imie(przej(27)); + for (int i=0; i<3; i++) + kol[i].clear(); + ll f=fort[v]; + const int lim=(k-1)*9; + for (int i=0; i=pom[kol[p].back()]-(kol[p].back()/3)*f) + kol[p].pop_back(); + kol[p].push_back(i); + dp[v][i]=-inf; + for (int j=0; j<3; j++) + if (!kol[j].empty()) + dp[v][i]=max(dp[v][i], pom[kol[j].front()]+przej(i-kol[j].front())*f); + for (int j=i-k*9; j<=i-lim; j++) + if (j>=0) + dp[v][i]=max(dp[v][i], pom[j]+przej(i-j)*f); + } +} + +int main() +{ + scanf("%d", &k); + for (int i=0; i1) + { + for (int i=0; i<=d; i++) + for (int j=0; j=0; h--) + przejscia(h); + } + int q; + scanf("%d", &q); + while(q--) + { + int x; + scanf("%d", &x); + if (k==1) + { + ll w=0; + for (int i=0; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const int vax=9; +const ll inf=1e18; +const int d=6; +const int m=1000*1000; + +int k; +ll fort[vax]; + +ll dp[vax][nax]; + +ll pom[nax]; + +deque kol[3]; + +int przej(int v) +{ + if (v<=(k-1)*9) + return (v/3); + if (v%3) + return (k-1)*3; + return v/3; +} + +void przejscia(int v) +{ + for (int i=0; i=0) + //~ dp[v][i]=max(dp[v][i], pom[j]+przej(i-j)*fort[v]); + //~ } + //~ debug() << v << " " << range(dp[v], dp[v]+60) << " " << imie(pom[30]) << imie(przej(27)); + for (int i=0; i<3; i++) + kol[i].clear(); + ll f=fort[v]; + const int lim=(k-1)*9; + for (int i=0; i=pom[kol[p].back()]-(kol[p].back()/3)*f) + kol[p].pop_back(); + kol[p].push_back(i); + dp[v][i]=-inf; + for (int j=0; j<3; j++) + if (!kol[j].empty()) + dp[v][i]=max(dp[v][i], pom[kol[j].front()]+przej(i-kol[j].front())*f); + for (int j=i-k*9; j<=i-lim; j++) + if (j>=0) + dp[v][i]=max(dp[v][i], pom[j]+przej(i-j)*f); + } +} + +int main() +{ + scanf("%d", &k); + for (int i=0; i1) + { + for (int i=0; i<=d; i++) + for (int j=0; j=0; h--) + przejscia(h); + } + int q; + scanf("%d", &q); + while(q--) + { + int x; + scanf("%d", &x); + if (k==1) + { + ll w=0; + for (int i=0; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=3007; + +mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); + +void ff() +{ + fflush(stdout); +} + +int n, m; + +int pole; + +int poz[nax]; + +int zapy; + +int policz() +{ + int ret=0; + for (int i=0; i graf[nax]; + +void kra(int a, int b, int prz) +{ + prz%=(n*m); + prz+=(n*m); + prz%=(n*m); + graf[a].push_back({b, prz}); + graf[b].push_back({a, (n*m-prz)%(n*m)}); +} + +int malew[nax]; + +int wyn[nax]; +int bylo[nax]; + +void dfs(int v) +{ + bylo[v]=1; + for (pii i : graf[v]) + { + if (!bylo[i.first]) + { + wyn[i.first]=(wyn[v]+i.second)%(n*m); + dfs(i.first); + } + } +} + +int main() +{ + scanf("%d%d", &n, &m); + //~ for (int i=0; i=pam) + { + gru[j].push_back(i); + mam=1; + } + zrob(x, -1); + } + zrob(i, -1); + if (!mam) + { + gru[k].push_back(i); + k++; + } + } + //~ debug() << imie(k); + //~ for (int i=0; i=pam) + { + kra(x, y, ruchy+m); + malew[j]=1; + znaj=1; + //~ debug() << "od " << i << " do " << j << " " << x << " " << y << " " << imie(ruchy) << imie(poz[x]) << imie(poz[y]); + } + } + assert(znaj); + while(ruchy--) + zrob(x, -1); + break; + } + } + dfs(0); + //~ for (int i=0; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n; + +int tab[nax]; + +ll pre[nax]; +ll suf[nax]; + +ll sum[nax]; + +void ans(int v) +{ + if (v==1) + printf("YES\n"); + else + printf("NO\n"); +} + +void test() +{ + scanf("%d", &n); + for (int i=1; i<=n; i++) + { + scanf("%d", &tab[i]); + } + for (int i=0; i<=n+2; i++) + pre[i]=suf[i]=0; + if (n<=2) + { + ans(1); + return; + } + tab[n+1]=0; + for (int i=1; i +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const ll mod=998244353; + +int n, k; + +int sta[nax]; +int kon[nax]; + +int gdz[nax]; +int tab[nax]; + +int tak[nax]; + +void test() +{ + scanf("%d%d", &n, &k); + for (int i=1; i<=n; i++) + { + scanf("%d", &sta[i]); + gdz[sta[i]]=i; + tak[i]=0; + } + for (int i=1; i<=k; i++) + { + scanf("%d", &kon[i]); + tab[i]=gdz[kon[i]]; + } + tak[0]=tak[n+1]=1; + debug() << range(tab+1, tab+1+k); + ll wyn=1; + for (int i=k; i; i--) + { + ll ile=0; + ile+=(!tak[tab[i]-1]); + ile+=(!tak[tab[i]+1]); + tak[tab[i]]=1; + wyn=(wyn*ile)%mod; + } + printf("%lld\n", wyn); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Radewoosh/1442C b/code/Radewoosh/1442C new file mode 100644 index 0000000..e7dc414 --- /dev/null +++ b/code/Radewoosh/1442C @@ -0,0 +1,171 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=200*1007; +const ll mod=998244353; +const int d=20; + +int n, m; + +vi graf[nax][2]; + +ll waga(pll v) +{ + return v.first+(1LL< wyn; + +int bylo[nax]; +int norodl[nax]; + +int juz[nax][2]; + +pll comp(pll a, pll b) +{ + if (a.second==b.second || a.first==b.first) + return min(a, b); + if (a.second>kol; + kol.push({{0, 0}, {1, 0}}); + while(!kol.empty()) + { + pll dys=kol.top().first; + dys={-dys.second, -dys.first}; + int v=kol.top().second.first; + int typ=kol.top().second.second; + kol.pop(); + if (juz[v][typ]) + continue; + juz[v][typ]=1; + if (v==n) + { + wyn.push_back(dys); + break; + } + for (int i : graf[v][typ]) + kol.push({{-dys.second, -(dys.first+1)}, {i, typ}}); + kol.push({{-(dys.second+1), -(dys.first)}, {v, typ^1}}); + } + } + + + for (int i=1; i<=n; i++) + { + norodl[i]=0; + bylo[i]=0; + } + + bylo[1]=1; + for (int h=0; h<=d; h++) + { + priority_queue kol; + for (int i=1; i<=n; i++) + if (bylo[i]) + { + kol.push({-norodl[i], i}); + bylo[i]=0; + } + while(!kol.empty()) + { + int v=kol.top().second; + int dis=-kol.top().first; + kol.pop(); + if (bylo[v]) + continue; + bylo[v]=1; + norodl[v]=dis; + for (int i : graf[v][h&1]) + kol.push({-(dis+1), i}); + } + if (bylo[n]) + wyn.push_back({norodl[n], h}); + } + + + debug() << wyn; + + for (int i=0; i<(int)wyn.size(); i++) + wyn[0]=comp(wyn[0], wyn[i]); + ll x=1; + for (int i=1; i<=wyn[0].second; i++) + x=(x*2)%mod; + x+=mod-1; + x+=wyn[0].first; + x%=mod; + printf("%lld\n", x); + return 0; +} + diff --git a/code/Radewoosh/1442D b/code/Radewoosh/1442D new file mode 100644 index 0000000..b06a48c --- /dev/null +++ b/code/Radewoosh/1442D @@ -0,0 +1,127 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; + +int n, k; + +vll wek[nax]; +ll sum[nax]; + +ll wyn; + +ll dp[3007][3007]; + +void doloz(int v, int gdz) +{ + for (int i=0; i<=k; i++) + dp[gdz+1][i]=dp[gdz][i]; + int r=wek[v].size(); + for (int i=0; i+r<=k; i++) + dp[gdz+1][i+r]=max(dp[gdz+1][i+r], dp[gdz][i]+sum[v]); +} + +void rek(int a, int b, int g) +{ + if (a==b) + { + ll suma=0; + for (int i=0; i<=(int)wek[a].size() && i<=k; i++) + { + wyn=max(wyn, suma+dp[g][k-i]); + if (i<(int)wek[a].size()) + suma+=wek[a][i]; + } + return; + } + int s=(a+b)>>1; + { + int p=g; + for (int i=a; i<=s; i++) + { + doloz(i, p); + p++; + } + rek(s+1, b, p); + } + { + int p=g; + for (int i=s+1; i<=b; i++) + { + doloz(i, p); + p++; + } + rek(a, s, p); + } +} + +int main() +{ + scanf("%d%d", &n, &k); + for (int i=1; i<=n; i++) + { + int x, y; + scanf("%d", &x); + while(x--) + { + scanf("%d", &y); + wek[i].push_back(y); + sum[i]+=y; + } + } + rek(1, n, 0); + printf("%lld\n", wyn); + return 0; +} + diff --git a/code/Radewoosh/1442E b/code/Radewoosh/1442E new file mode 100644 index 0000000..5836337 --- /dev/null +++ b/code/Radewoosh/1442E @@ -0,0 +1,161 @@ +//~ while (clock()<=69*CLOCKS_PER_SEC) +//~ #pragma comment(linker, "/stack:200000000") +#pragma GCC optimize("O3") +//~ #pragma GCC target ("avx2") +//~ #pragma GCC optimize("Ofast") +//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") +//~ #pragma GCC optimize("unroll-loops") +#include +#include +#include + +using namespace __gnu_pbds; +using namespace std; + +template +using ordered_set = + tree, rb_tree_tag, tree_order_statistics_node_update>; + +#define sim template < class c +#define ris return * this +#define dor > debug & operator << +#define eni(x) sim > typename \ + enable_if(0) x 1, debug&>::type operator<<(c i) { +sim > struct rge { c b, e; }; +sim > rge range(c i, c j) { return rge{i, j}; } +sim > auto dud(c* x) -> decltype(cerr << *x, 0); +sim > char dud(...); +struct debug { +#ifdef LOCAL +~debug() { cerr << endl; } +eni(!=) cerr << boolalpha << i; ris; } +eni(==) ris << range(begin(i), end(i)); } +sim, class b dor(pair < b, c > d) { + ris << "(" << d.first << ", " << d.second << ")"; +} +sim dor(rge d) { + *this << "["; + for (auto it = d.b; it != d.e; ++it) + *this << ", " + 2 * (it == d.b) << *it; + ris << "]"; +} +#else +sim dor(const c&) { ris; } +#endif +}; +#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " + +#define shandom_ruffle random_shuffle + +using ll=long long; +using pii=pair; +using pll=pair; +using vi=vector; +using vll=vector; +const int nax=1000*1007; +const int inf=1e9; + +int n; +int tab[nax]; +vi graf[nax]; + +int dp[nax][3]; + +int wyn; + +int glolim; + +void dfs(int v, int oj) +{ + for (int i : graf[v]) + { + if (i==oj) + continue; + dfs(i, v); + } + for (int h=1; h<=2; h++) + { + if ((tab[v]^h)==3) + { + dp[v][h]=inf; + continue; + } + int a=0; + int b=0; + for (int i : graf[v]) + { + if (i==oj) + continue; + int x=min(dp[i][h], dp[i][h^3]+1); + if (x>=b) + { + a=b; + b=x; + continue; + } + if (x>=a) + a=x; + } + if (a+b<=glolim) + dp[v][h]=b; + else + dp[v][h]=inf; + } + for (int h=1; h<=2; h++) + dp[v][h]=min(dp[v][h], inf); + //~ debug() << v << " " << dp[v][1] << " " << dp[v][2]; +} + +int check(int v) +{ + glolim=v; + //~ debug() << imie(glolim); + dfs(1, 0); + return (dp[1][1]>1; + if (check(bss)) + bsb=bss; + else + bsa=bss+1; + } + //~ debug() << imie(bsa); + printf("%d\n", (bsa+3)/2); +} + +int main() +{ + int t; + scanf("%d", &t); + while(t--) + test(); + return 0; +} + diff --git a/code/Um_nik/1423C b/code/Um_nik/1423C new file mode 100644 index 0000000..24c8f42 --- /dev/null +++ b/code/Um_nik/1423C @@ -0,0 +1,154 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 10010; +int n; +vector g[N]; +int sz[N]; +bool bad[N]; +vector ans; + +void dfsSz(int v, int par) { + sz[v] = 1; + for (int u : g[v]) { + if (u == par || bad[u]) continue; + dfsSz(u, v); + sz[v] += sz[u]; + } +} + +int getCentr(int v) { + dfsSz(v, -1); + int S = sz[v]; + while(true) { + int w = v; + for (int u : g[v]) { + if (bad[u]) continue; + if (sz[u] > sz[v]) continue; + if (2 * sz[u] >= S) { + w = u; + } + } + if (w == v) break; + v = w; + } + return v; +} + +void addEdges(int v, int par, int to) { +// eprintf("addEdges %d %d %d\n", v, par, to); + ans.push_back(mp(v, to)); + for (int u : g[v]) { + if (bad[u] || u == par) continue; + addEdges(u, v, to); + } +} + +int solve(int v, int oldc, int oldv) { +// eprintf("solve %d %d\n", v, flag); + v = getCentr(v); + dfsSz(v, -1); + vector sons; + for (int u : g[v]) { + if (bad[u]) continue; + sons.push_back(mp(sz[u], u)); + } + bad[v] = 1; + if (oldc != -1) { + addEdges(oldv, oldc, oldc); + ans.push_back(mp(v, oldc)); + } + sort(all(sons)); + reverse(all(sons)); + for (int i = 0; i < (int)sons.size(); i++) { + int u = sons[i].second; + if (i == 0 && oldc == -1) { + solve(u, v, u); + } else { + addEdges(u, v, v); + solve(u, -1, -1); + } + } + return v; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int k; + scanf("%d%d", &n, &k); + set hv; + for (int i = 1; i < n; i++) { + int v, u; + scanf("%d%d", &v, &u); + v--;u--; + hv.insert(mp(v, u)); + hv.insert(mp(u, v)); + g[v].push_back(u); + g[u].push_back(v); + } + solve(1, -1, -1); + vector nans; + for (pii t : ans) + if (hv.count(t) == 0) nans.push_back(t); + if ((int)nans.size() > 10 * n) throw; + printf("%d\n", (int)nans.size()); + for (pii t : nans) { + printf("%d %d\n", t.first + 1, t.second + 1); + } + + return 0; +} + diff --git a/code/Um_nik/1427A b/code/Um_nik/1427A new file mode 100644 index 0000000..de521f5 --- /dev/null +++ b/code/Um_nik/1427A @@ -0,0 +1,88 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 555; +int n; +ll a[N]; + +void solve() { + scanf("%d", &n); + ll sum = 0; + for (int i = 0; i < n; i++) { + scanf("%lld", &a[i]); + sum += a[i]; + } + if (sum == 0) { + printf("NO\n"); + return; + } + sort(a, a + n); + if (sum > 0) reverse(a, a + n); + printf("YES\n"); + for (int i = 0; i < n; i++) + printf("%lld ", a[i]); + printf("\n"); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1427B b/code/Um_nik/1427B new file mode 100644 index 0000000..4a60cc8 --- /dev/null +++ b/code/Um_nik/1427B @@ -0,0 +1,121 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 200200; +char s[N]; +int n, k; + +void solve() { + scanf("%d%d", &n, &k); + k = min(n, k); + scanf("%s", s); + vector a, b; + int cur = 0; + int len = 0; + for (int i = 0; i < n; i++) { + int x = (s[i] == 'W' ? 1 : 0); + if (x != cur) { + if (cur == 0) { + a.push_back(len); + } else { + b.push_back(len); + } + cur = x; + len = 0; + } + len++; + } + if (cur == 0) { + a.push_back(len); + } else { + b.push_back(len); + a.push_back(0); + } + int cntZ = 0; + for (int x : a) cntZ += x; + if (cntZ <= k) { + printf("%d\n", 2 * n - 1); + return; + } + if (b.empty()) { + printf("%d\n", max(0, 2 * k - 1)); + return; + } + int ans = 0; + for (int x : b) ans += 2 * x - 1; + a.pop_back(); + reverse(all(a)); + a.pop_back(); + sort(all(a)); + ans += 2 * k; + for (int x : a) { + if (x <= k) { + ans++; + k -= x; + } else break; + } + printf("%d\n", ans); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1427C b/code/Um_nik/1427C new file mode 100644 index 0000000..54737b9 --- /dev/null +++ b/code/Um_nik/1427C @@ -0,0 +1,86 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 100100; +int n, m; +int a[N][3]; +int dp[N]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d%d", &m, &n); + m *= 4; + n++; + for (int i = 1; i < n; i++) + for (int j = 0; j < 3; j++) + scanf("%d", &a[i][j]); + a[0][1] = a[0][2] = 1; + for (int i = 1; i < n; i++) + dp[i] = -N; + for (int i = 1; i < n; i++) { + for (int j = max(0, i - m); j < i; j++) { + if (a[i][0] - a[j][0] >= abs(a[i][1] - a[j][1]) + abs(a[i][2] - a[j][2])) + dp[i] = max(dp[i], dp[j] + 1); + } + } + int ans = 0; + for (int i = 0; i < n; i++) + ans = max(ans, dp[i]); + printf("%d\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1427D b/code/Um_nik/1427D new file mode 100644 index 0000000..eb92899 --- /dev/null +++ b/code/Um_nik/1427D @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 55; +int n; +int a[N]; +vector> ans; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d", &a[i]); + a[i]--; + } + for (int x = 0; x < n - (n & 1); x++) { + int p = 0; + while(a[p] != x) p++; + assert(x <= p); + p++; + vector cur; + for (int i = 0; i < x; i++) + cur.push_back(1); + cur.push_back(p - x); + for (int i = p; i < n; i++) + cur.push_back(1); + if (x & 1) reverse(all(cur)); + if ((int)cur.size() > 1) ans.push_back(cur); + reverse(a + x, a + p); + } + printf("%d\n", (int)ans.size()); + for (int i = 0; i < (int)ans.size(); i++) { + printf("%d", (int)ans[i].size()); + for (int x : ans[i]) + printf(" %d", x); + printf("\n"); + } + + return 0; +} + diff --git a/code/Um_nik/1427E b/code/Um_nik/1427E new file mode 100644 index 0000000..e5dcc3b --- /dev/null +++ b/code/Um_nik/1427E @@ -0,0 +1,159 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 100100; +ll ans[N][3]; +int ansSz; + +void addAns(ll x, ll y, ll z) { + ans[ansSz][0] = x; + ans[ansSz][1] = y; + ans[ansSz][2] = z; + ansSz++; +} + +bool isPow2(ll x) { + return (x > 0 && (x & (x - 1)) == 0); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + ll x; + scanf("%lld", &x); + int k = 0; + while((1LL << (k + 1)) <= x) k++; + assert(k > 0); + ll y = x; + for (int i = 0; i < k; i++) { + addAns(y, 1, y); + y += y; + } + ll z = x + y; + addAns(x, 1, y); + addAns(z, 0, x); + z ^= x; + addAns(z, 0, y); + z ^= y; + assert(z != 0); + + if (!isPow2(z)) { + ll p = z + z; + addAns(z, 1, z); + addAns(z, 0, p); + z ^= p; + int n = 0; + while(((z >> n) & 1) == 0) n++; + int m = n + 1; + while(((z >> m) & 1) == 0) m++; + assert(z == ((1LL << n) + (1LL << m))); + assert(m + (m - n) < 61); + y = z; + for (int i = 0; i < m - n; i++) { + addAns(y, 1, y); + y += y; + } + p = y + z; + addAns(y, 1, z); + addAns(p, 0, y); + p ^= y; + addAns(p, 0, z); + p ^= z; + assert(isPow2(p)); + z = p; + } + + while(z <= x) { + if (x & z) { + addAns(x, 0, z); + x ^= z; + } + addAns(z, 1, z); + z += z; + } + + while(x > 1) { + y = x; + while(!isPow2(y)) { + addAns(y, 1, y); + y += y; + if (y & z) { + addAns(y, 0, z); + y ^= z; + } + } + assert(y < z); + ll p = y; + while(p < z) { + addAns(p, 1, p); + p += p; + } + z = y; + while(x >= z) { + int k = 0; + while((1LL << (k + 1)) <= x) k++; + addAns(x, 0, 1LL << k); + x ^= 1LL << k; + } + } + + printf("%d\n", ansSz); + for (int i = 0; i < ansSz; i++) { + printf("%lld %c %lld\n", ans[i][0], (ans[i][1] == 0 ? '^' : '+'), ans[i][2]); + } + + return 0; +} + diff --git a/code/Um_nik/1427G b/code/Um_nik/1427G new file mode 100644 index 0000000..e99dbe0 --- /dev/null +++ b/code/Um_nik/1427G @@ -0,0 +1,210 @@ +#pragma GCC optimize("Ofast") +#pragma GCC optimize("unroll-loops") +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +struct Edge { + int v, to, cap; + ll cost; + + Edge() : v(), to(), cap(), cost() {} + Edge(int _v, int _to, int _cap, ll _cost) : v(_v), to(_to), cap(_cap), cost(_cost) {} +}; +const int N = 222; +const int V = N * N + 3; +const int E = N * N * 9 + 3; +vector g[V]; +Edge ed[E]; +int edSz; +int S, T; + +void addEdge2(int v, int to, int cap, ll cost) { + ed[edSz] = Edge(v, to, cap, cost); + g[v].push_back(edSz++); +} +void addEdge(int v, int to, int cap, ll cost) { + addEdge2(v, to, cap, cost); + addEdge2(to, v, 0, -cost); +} + + +ll a[N][N]; +ll b[V]; +int n; + +bool good[V]; +bool used[V]; +int par[V]; + +int bV, bU; +int bstVal; + +void dfs1(int v, int st) { + used[v] = 1; + if (good[v] && b[v] - b[st] < bstVal) { + bstVal = b[v] - b[st]; + bV = st; + bU = v; + } + for (int id : g[v]) { + Edge e = ed[id]; + if (e.cap <= 0) continue; + int to = e.to; + if (used[to]) continue; + par[to] = id; + dfs1(to, st); + } +} + +ll MCMF() { + ll ans = 0; + while(true) { + vector ord; + for (int i = 0; i < n; i++) { + used[i] = false; + if (good[i]) ord.push_back(mp(b[i], i)); + } + sort(all(ord)); + reverse(all(ord)); + bV = bU = -1; + bstVal = 0; + for (auto t : ord) { + int v = t.second; + if (used[v]) continue; + dfs1(v, v); + } + if (bstVal == 0) return ans; + //eprintf("%d %d\n", bV, bU); + ans += bstVal; + int v = bU; + while(v != bV) { + int id = par[v]; + ed[id].cap--; + ed[id ^ 1].cap++; + v = ed[id].v; + } + good[bV] = good[bU] = false; + } + return ans; +} + +void read() { + /* + n = 200; + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + a[i][j] = 0; + if (i != 0 && i != n - 1 && j != 0 && j != n - 1) continue; + a[i][j] = myRand(1e9) + 1; + } + } + */ + + scanf("%d", &n); + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) { + scanf("%lld", &a[i][j]); + } + +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + read(); + + S = n * n; + T = S + 1; + + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) { + if (a[i][j] > 0) { +// addEdge(S, i * n + j, 1, a[i][j]); +// addEdge(i * n + j, T, 1, -a[i][j]); + b[i * n + j] = a[i][j]; + good[i * n + j] = 1; + } + } + ll ans = 0; + for (int x = 0; x < n; x++) + for (int y = 0; y < n; y++) { + if (a[x][y] == -1) continue; + if (x + 1 < n && a[x + 1][y] != -1) { + if (a[x][y] != 0 && a[x + 1][y] != 0) { + ans += abs(a[x][y] - a[x + 1][y]); + } else { + int v = x * n + y, u = (x + 1) * n + y; + addEdge2(v, u, 1, 0); + addEdge2(u, v, 1, 0); + } + } + if (y + 1 < n && a[x][y + 1] != -1) { + if (a[x][y] != 0 && a[x][y + 1] != 0) { + ans += abs(a[x][y] - a[x][y + 1]); + } else { + int v = x * n + y, u = x * n + y + 1; + addEdge2(v, u, 1, 0); + addEdge2(u, v, 1, 0); + } + } + } +// eprintf("curans = %lld\n", ans); +// n = T + 1; + n = n * n; + printf("%lld\n", ans - MCMF()); + + return 0; +} + diff --git a/code/Um_nik/1428A b/code/Um_nik/1428A new file mode 100644 index 0000000..91ce82e --- /dev/null +++ b/code/Um_nik/1428A @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) { + ll x1, y1, x2, y2; + scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); + ll dx = abs(x1 - x2), dy = abs(y1 - y2); + ll ans = dx + dy; + if (dx != 0 && dy != 0) ans += 2; + printf("%lld\n", ans); + } + + return 0; +} + diff --git a/code/Um_nik/1428B b/code/Um_nik/1428B new file mode 100644 index 0000000..8e8ea76 --- /dev/null +++ b/code/Um_nik/1428B @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 300300; +int n; +char s[N]; + +void solve() { + scanf("%d %s", &n, s); + bool okLeft = true, okRight = true; + for (int i = 0; i < n; i++) { + if (s[i] == '<') okRight = false; + if (s[i] == '>') okLeft = false; + } + s[n] = s[0]; + int ans = 0; + for (int i = 0; i < n; i++) { + if (s[i] == '-' || s[i + 1] == '-' || okLeft || okRight) ans++; + } + printf("%d\n", ans); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1428C b/code/Um_nik/1428C new file mode 100644 index 0000000..7d7c28b --- /dev/null +++ b/code/Um_nik/1428C @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 300300; +int n; +char s[N]; +char a[N]; +int m; + +int solve() { + scanf("%s", s); + n = strlen(s); + m = 0; + for (int i = 0; i < n; i++) { + if (s[i] == 'A') { + a[m++] = 'A'; + } else { + if (m > 0 && a[m - 1] == 'A') + m--; + else + a[m++] = 'B'; + } + } + int p = 0; + while(p + 2 <= m && a[p + 1] == 'B') p += 2; + return m - p; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) printf("%d\n", solve()); + + return 0; +} + diff --git a/code/Um_nik/1428D b/code/Um_nik/1428D new file mode 100644 index 0000000..f65e888 --- /dev/null +++ b/code/Um_nik/1428D @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 200200; +int n; +int a[N]; +int b[N]; +vector ans; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &n); + for (int i = 1; i <= n; i++) + scanf("%d", &a[i]); + int X = n + 1; + vector ones; + int lst23 = n + 1; + for (int i = n; i > 0; i--) { + if (a[i] == 0) continue; + if (X == n + 1 && a[i] > 1) { + printf("-1\n"); + return 0; + } + if (a[i] == 1) { + X--; + ans.push_back(mp(X, i)); + b[i] = X; + ones.push_back(i); + } else if (a[i] == 2) { + if (ones.empty()) { + printf("-1\n"); + return 0; + } + int v = ones.back(); + ones.pop_back(); + b[i] = b[v]; + ans.push_back(mp(b[i], i)); + lst23 = i; + } else if (a[i] == 3) { + X--; + int v = n + 1; + if (lst23 != n + 1) { + v = lst23; + } else if (!ones.empty()) { + v = ones.back(); + ones.pop_back(); + } else { + printf("-1\n"); + return 0; + } + ans.push_back(mp(X, v)); + ans.push_back(mp(X, i)); + b[i] = X; + lst23 = i; + } else throw; + assert(X >= 1); + } + printf("%d\n", (int)ans.size()); + for (pii t : ans) + printf("%d %d\n", t.first, t.second); + + return 0; +} + diff --git a/code/Um_nik/1428E b/code/Um_nik/1428E new file mode 100644 index 0000000..033e47b --- /dev/null +++ b/code/Um_nik/1428E @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 100100; +int n, k; +ll a[N]; +ll b[N]; +set setik; + +ll getScore(ll x, ll y) { + ll p = x / y; + return p * p * (y - x % y) + (p + 1) * (p + 1) * (x % y); +} +ll getDelta(ll x, ll y) { + return getScore(x, y + 1) - getScore(x, y); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d%d", &n, &k); + k -= n; + for (int i = 0; i < n; i++) { + scanf("%lld", &a[i]); + b[i] = 1; + if (b[i] < a[i]) setik.insert(mp(getDelta(a[i], b[i]), i)); + } + while(k--) { + assert(!setik.empty()); + int v = setik.begin()->second; + setik.erase(setik.begin()); + b[v]++; + if (b[v] < a[v]) setik.insert(mp(getDelta(a[v], b[v]), v)); + } + ll ans = 0; + for (int i = 0; i < n; i++) + ans += getScore(a[i], b[i]); + printf("%lld\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1428F b/code/Um_nik/1428F new file mode 100644 index 0000000..b683927 --- /dev/null +++ b/code/Um_nik/1428F @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 500500; +int n; +char s[N]; +int st[N][2]; +int sz; +ll ans; +ll sumAns; + +ll getSum(ll k) { + return k * (k + 1) / 2; +} + +void solve(int L) { + st[sz][0] = 1; + st[sz][1] = 0; + sz++; + if (L == 0) return; + int val = 0; + int len = 0; + while(sz > 0 && val < L) { + sz--; + if (st[sz][1] == 0) { + ans -= (ll)st[sz][0] * val; + len += st[sz][0]; + } else { + if (val + st[sz][0] > L) { + ans -= getSum(L) - getSum(val); + len += L - val; + st[sz][0] -= L - val; + val = L; + sz++; + break; + } + ans -= getSum(val + st[sz][0]) - getSum(val); + len += st[sz][0]; + val += st[sz][0]; + } + } + assert(len >= L); + if (len > L) { + st[sz][0] = len - L; + st[sz][1] = 0; + sz++; + ans += (ll)(len - L) * L; + } + st[sz][0] = L; + st[sz][1] = 1; + sz++; + ans += getSum(L); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d %s", &n, s); + int cur = 0; + for (int i = n - 1; i >= 0; i--) { + if (s[i] == '1') { + cur++; + } else { + cur = 0; + } + solve(cur); + sumAns += ans; + } + printf("%lld\n", sumAns); + + return 0; +} + diff --git a/code/Um_nik/1428G1 b/code/Um_nik/1428G1 new file mode 100644 index 0000000..4f35c01 --- /dev/null +++ b/code/Um_nik/1428G1 @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const ll INF = (ll)1e17; + +struct MaxStack { + vector val, mx; + + MaxStack() : val(), mx() {} + + bool empty() { + return val.empty(); + } + ll getMax() { + if (empty()) return -INF; + return mx.back(); + } + void push(ll x) { + val.push_back(x); + if (mx.empty()) + mx.push_back(x); + else + mx.push_back(max(mx.back(), x)); + } + ll pop() { + assert(!empty()); + ll x = val.back(); + val.pop_back(); + mx.pop_back(); + return x; + } +}; +struct MaxQueue { + MaxStack A, B; + + MaxQueue() : A(), B() {} + + void push(ll x) { + A.push(x); + } + void pop() { + if (B.empty()) { + while(!A.empty()) { + B.push(A.pop()); + } + } + assert(!B.empty()); + B.pop(); + } + ll getMax() { + return max(A.getMax(), B.getMax()); + } +}; + +const int N = (int)1e6 + 7; +ll dp[N], old[N]; +int k; + +void solve(int L, ll F) { + for (int i = 0; i < L; i++) { + old[i] = dp[i]; + dp[i] = 0; + } + MaxQueue Q[3]; + for (int i = 0; i < 3; i++) + Q[i] = MaxQueue(); + for (int x = 0; x < 10 * L; x++) { + if (x % 10 == 0) + Q[(x / 10) % 3].push(old[x / 10] - F * 10 * (x / 30)); + for (int y = 9 * (k - 1); y <= 9 * k; y++) { + if (x < y) continue; + if ((x - y) % 10 != 0) continue; + ll val = old[(x - y) / 10]; + val += 3 * F * (k - 1); + int z = y - 9 * (k - 1); + if (z % 3 == 0) val += F * (z / 3); + dp[x] = max(dp[x], val); + } + for (int i = 0; i < 3; i++) { + ll val = Q[i].getMax(); + if (val < -INF / 2) continue; + int z = x - 10 * i; + assert(z >= 0); + val += F * (z / 3); + dp[x] = max(dp[x], val); + } + if (x >= 9 * (k - 1) && (x - 9 * (k - 1)) % 10 == 0) { + int y = x - 9 * (k - 1); + Q[(y / 10) % 3].pop(); + } + } +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + ll F[6]; + scanf("%d", &k); + for (int i = 0; i < 6; i++) + scanf("%lld", &F[i]); + int L = 1; + for (int i = 5; i >= 0; i--) { + solve(L, F[i]); + L *= 10; + } + + int t; + scanf("%d", &t); + while(t--) { + int x; + scanf("%d", &x); + printf("%lld\n", dp[x]); + } + + return 0; +} + diff --git a/code/Um_nik/1428G2 b/code/Um_nik/1428G2 new file mode 100644 index 0000000..4f35c01 --- /dev/null +++ b/code/Um_nik/1428G2 @@ -0,0 +1,169 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const ll INF = (ll)1e17; + +struct MaxStack { + vector val, mx; + + MaxStack() : val(), mx() {} + + bool empty() { + return val.empty(); + } + ll getMax() { + if (empty()) return -INF; + return mx.back(); + } + void push(ll x) { + val.push_back(x); + if (mx.empty()) + mx.push_back(x); + else + mx.push_back(max(mx.back(), x)); + } + ll pop() { + assert(!empty()); + ll x = val.back(); + val.pop_back(); + mx.pop_back(); + return x; + } +}; +struct MaxQueue { + MaxStack A, B; + + MaxQueue() : A(), B() {} + + void push(ll x) { + A.push(x); + } + void pop() { + if (B.empty()) { + while(!A.empty()) { + B.push(A.pop()); + } + } + assert(!B.empty()); + B.pop(); + } + ll getMax() { + return max(A.getMax(), B.getMax()); + } +}; + +const int N = (int)1e6 + 7; +ll dp[N], old[N]; +int k; + +void solve(int L, ll F) { + for (int i = 0; i < L; i++) { + old[i] = dp[i]; + dp[i] = 0; + } + MaxQueue Q[3]; + for (int i = 0; i < 3; i++) + Q[i] = MaxQueue(); + for (int x = 0; x < 10 * L; x++) { + if (x % 10 == 0) + Q[(x / 10) % 3].push(old[x / 10] - F * 10 * (x / 30)); + for (int y = 9 * (k - 1); y <= 9 * k; y++) { + if (x < y) continue; + if ((x - y) % 10 != 0) continue; + ll val = old[(x - y) / 10]; + val += 3 * F * (k - 1); + int z = y - 9 * (k - 1); + if (z % 3 == 0) val += F * (z / 3); + dp[x] = max(dp[x], val); + } + for (int i = 0; i < 3; i++) { + ll val = Q[i].getMax(); + if (val < -INF / 2) continue; + int z = x - 10 * i; + assert(z >= 0); + val += F * (z / 3); + dp[x] = max(dp[x], val); + } + if (x >= 9 * (k - 1) && (x - 9 * (k - 1)) % 10 == 0) { + int y = x - 9 * (k - 1); + Q[(y / 10) % 3].pop(); + } + } +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + ll F[6]; + scanf("%d", &k); + for (int i = 0; i < 6; i++) + scanf("%lld", &F[i]); + int L = 1; + for (int i = 5; i >= 0; i--) { + solve(L, F[i]); + L *= 10; + } + + int t; + scanf("%d", &t); + while(t--) { + int x; + scanf("%d", &x); + printf("%lld\n", dp[x]); + } + + return 0; +} + diff --git a/code/Um_nik/1434A b/code/Um_nik/1434A new file mode 100644 index 0000000..af7890c --- /dev/null +++ b/code/Um_nik/1434A @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int INF = (int)1e9 + 5; +const int K = 6; +const int N = 100100; +int n; +int a[K]; +int b[N]; +int ans = INF; + +int solve(int m) { + if (b[0] - a[0] < m) return INF; + int res = 0; + for (int i = 0; i < K; i++) { + int p = n; + if (i + 1 < K) + p = lower_bound(b, b + n, m + a[i + 1]) - b; + if (p > 0) + res = max(res, b[p - 1] - a[i]); + } + return res - m; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + for (int i = 0; i < K; i++) + scanf("%d", &a[i]); + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &b[i]); + sort(a, a + K); + sort(b, b + n); + for (int i = 0; i < K; i++) + for (int j = 0; j < n; j++) + ans = min(ans, solve(b[j] - a[i])); + printf("%d\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1434B b/code/Um_nik/1434B new file mode 100644 index 0000000..a6be5ea --- /dev/null +++ b/code/Um_nik/1434B @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 200200; +int n; +int ord[N]; +set pos; +int a[N]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &n); + for (int i = 0; i < 2 * n; i++) { + char t; + scanf(" %c", &t); + if (t == '+') { + a[i] = -1; + pos.insert(i); + } else { + int x; + scanf("%d", &x); + a[i] = x; + ord[x - 1] = i; + } + } + for (int i = 0; i < n; i++) { + auto it = pos.lower_bound(ord[i]); + if (it == pos.begin()) { + printf("NO\n"); + return 0; + } + it--; + a[*it] = -(i + 1); + pos.erase(it); + } + set setik; + for (int i = 0; i < 2 * n; i++) { + if (a[i] < 0) { + setik.insert(-a[i]); + } else { + assert(!setik.empty()); + if (*setik.begin() != a[i]) { + printf("NO\n"); + return 0; + } + setik.erase(setik.begin()); + } + } + printf("YES\n"); + for (int i = 0; i < 2 * n; i++) + if (a[i] < 0) + printf("%d ", -a[i]); + printf("\n"); + + return 0; +} + diff --git a/code/Um_nik/1434C b/code/Um_nik/1434C new file mode 100644 index 0000000..f190c5e --- /dev/null +++ b/code/Um_nik/1434C @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +void solve() { + ll a, b, c, d, k; + scanf("%lld%lld%lld%lld", &a, &b, &c, &d); + if (a > b * c) { + printf("-1\n"); + return; + } + k = 1 + a / (b * d); + printf("%lld\n", k * a - b * d * (k - 1) * k / 2); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1434E b/code/Um_nik/1434E new file mode 100644 index 0000000..32cbd86 --- /dev/null +++ b/code/Um_nik/1434E @@ -0,0 +1,203 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 100010; +const int B = 460; +int n; +int a[N]; +int c[N][B]; +int rr[B][N]; +int nxt[N]; +pii st[B]; +int stSz; + +void read() { + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); +} + +void addStack(int y, int x) { + if (stSz == 0 || y > st[stSz - 1].first) { + st[stSz++] = mp(y, x); + } else { + st[stSz - 1].second = x; + } +} + +inline int getNext(int x, int p) { + if (rr[x][p] == p) return p; + return rr[x][p] = getNext(x, rr[x][p]); +} + +void setOnSegm(int x, int l, int r, int val) { +// eprintf("setOnSegm %d %d %d %d\n", x, l, r, val); + l = max(l, 0); + int p = nxt[l]; + while(true) { + p = getNext(x, p); + if (p >= n || a[p] >= r) break; +// eprintf("p = %d\n", p); + c[p][x] = 2 * a[p] - val; + rr[x][p] = p + 1; + } +} + +int solve() { + int z = 0; + for (int i = 0; i < n; i++) { + while(z <= a[i]) { + nxt[z++] = i; + } + } + while(z < N) { + nxt[z++] = n; + } + for (int x = 0; x < B; x++) + for (int i = 0; i <= n; i++) { + rr[x][i] = i; + c[i][x] = N; + } + int ans = 0; + for (int i = n - 1; i >= 0; i--) { + //eprintf("i = %d\n", i); + stSz = 0; + addStack(-1, 0); + for (int x = 1; x <= ans; x++) { + if (c[i][x - 1] < a[i]) + addStack(c[i][x - 1], x); + else + break; + } + /* + for (int j = 0; j < stSz; j++) + eprintf("(%d %d) ", st[j].first, st[j].second); + eprintf("\n"); + */ + st[stSz++] = mp(a[i], N); + for (int j = 0; j + 1 < stSz; j++) { + int x = st[j].second; + int l = st[j].first + 1, r = st[j + 1].first + 1; + ans = max(ans, x + 1); + setOnSegm(x, l, r, a[i]); + } + } + return ans; +} + +int dp[B][B]; +int MEX(vector z) { + sort(all(z)); + z.resize(unique(all(z)) - z.begin()); + for (int i = 0;; i++) + if (i >= (int)z.size() || z[i] != i) + return i; +} +int stupid() { + for (int i = n - 1; i >= 0; i--) + for (int j = n - 1; j >= i; j--) { + vector zz; + for (int h = j + 1; h < n; h++) { + if (a[j] - a[i] >= a[h] - a[j]) continue; + zz.push_back(dp[j][h]); + } + dp[i][j] = MEX(zz); + } + vector zz; + for (int i = 0; i < n; i++) + zz.push_back(dp[i][i]); + return MEX(zz); +} + +void gen() { + n = 5 + myRand(100); + while(true) { + for (int i = 0; i < n; i++) + a[i] = myRand(10000); + sort(a, a + n); + bool ok = true; + for (int i = 1; i < n; i++) + ok &= a[i] != a[i - 1]; + if (ok) break; + } +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); +/* + for (int it = 0; it < 100000; it++) { + if (it % 1000 == 0) eprintf("%d\n", it); + gen(); + if (solve() != stupid()) throw; + } +*/ + int q; + scanf("%d", &q); + int ans = 0; + while(q--) { + read(); + int res = solve(); +// int st = stupid(); +// eprintf("res = %d, st = %d\n", res, st); + ans ^= res; + } + if (ans) + printf("YES\n"); + else + printf("NO\n"); + + return 0; +} + diff --git a/code/Um_nik/1436A b/code/Um_nik/1436A new file mode 100644 index 0000000..3dee808 --- /dev/null +++ b/code/Um_nik/1436A @@ -0,0 +1,77 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) { + int n, m; + scanf("%d%d", &n, &m); + for (int i = 0; i < n; i++) { + int x; + scanf("%d", &x); + m -= x; + } + if (m == 0) + printf("YES\n"); + else + printf("NO\n"); + } + + return 0; +} + diff --git a/code/Um_nik/1436B b/code/Um_nik/1436B new file mode 100644 index 0000000..d7ee584 --- /dev/null +++ b/code/Um_nik/1436B @@ -0,0 +1,91 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +bool isPrime(int x) { + if (x < 2) return false; + for (int y = 2; y * y <= x; y++) + if (x % y == 0) + return false; + return true; +} + +void solve(int n) { + for (int y = 4;; y += 2) { + if (isPrime(1 + (n - 1) * y)) { + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + printf("%d ", (i == j ? 1 : y)); + } + printf("\n"); + } + return; + } + } +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) { + int n; + scanf("%d", &n); + solve(n); + } + + return 0; +} + diff --git a/code/Um_nik/1436C b/code/Um_nik/1436C new file mode 100644 index 0000000..3044b49 --- /dev/null +++ b/code/Um_nik/1436C @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + + +const ll MOD = (ll)1e9 + 7; +ll add(ll x, ll y) { + x += y; + if (x >= MOD) return x - MOD; + return x; +} +ll sub(ll x, ll y) { + x -= y; + if (x < 0) return x + MOD; + return x; +} +ll mult(ll x, ll y) { + return (x * y) % MOD; +} +ll bin_pow(ll x, ll p) { + if (p == 0) return 1; + if (p & 1) return mult(x, bin_pow(x, p - 1)); + return bin_pow(mult(x, x), p / 2); +} +ll rev(ll x) { + return bin_pow(x, MOD - 2); +} + +const int N = 1010; +ll C[N][N], f[N]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + for (int i = 0; i < N; i++) + C[i][0] = C[i][i] = 1; + for (int i = 0; i < N; i++) + for (int j = 1; j < i; j++) + C[i][j] = add(C[i - 1][j - 1], C[i - 1][j]); + f[0] = 1; + for (int i = 1; i < N; i++) + f[i] = mult(f[i - 1], i); + + int n, p, x; + scanf("%d%d%d", &n, &x, &p); + x--; + + int sm = 0, big = 0; + int l = 0, r = n; + while(l < r) { + int mid = (l + r) / 2; + if (mid == p) { + l = mid + 1; + } else if (mid < p) { + sm++; + l = mid + 1; + } else { + big++; + r = mid; + } + } + + int ans = 1; + ans = mult(ans, mult(C[x][sm], f[sm])); + ans = mult(ans, mult(C[n - 1 - x][big], f[big])); + ans = mult(ans, f[n - 1 - sm - big]); + printf("%d\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1436D b/code/Um_nik/1436D new file mode 100644 index 0000000..b17a361 --- /dev/null +++ b/code/Um_nik/1436D @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 200200; +int n; +int par[N]; +ll a[N]; +ll sz[N]; +ll ans = 0; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &n); + for (int i = 1; i < n; i++) { + scanf("%d", &par[i]); + par[i]--; + } + for (int i = 0; i < n; i++) { + scanf("%lld", &a[i]); + sz[i] = 1; + } + for (int i = 1; i < n; i++) + sz[par[i]] = 0; + for (int i = n - 1; i > 0; i--) { + a[par[i]] += a[i]; + sz[par[i]] += sz[i]; + } + for (int i = 0; i < n; i++) + ans = max(ans, (a[i] + sz[i] - 1) / sz[i]); + printf("%lld\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1436E b/code/Um_nik/1436E new file mode 100644 index 0000000..e704fd5 --- /dev/null +++ b/code/Um_nik/1436E @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = (1 << 17); +struct Node { + int l, r; + int val; + + Node() : l(), r(), val(-1) {} + Node(int _l, int _r) : l(_l), r(_r), val(-1) {} +}; +Node tree[2 * N + 5]; + +void build() { + for (int i = 0; i < N; i++) + tree[N + i] = Node(i, i + 1); + for (int i = N - 1; i > 0; i--) + tree[i] = Node(tree[2 * i].l, tree[2 * i + 1].r); +} + +void setInPoint(int p, int x) { + p += N; + tree[p].val = x; + while(p > 1) { + p >>= 1; + tree[p].val = min(tree[2 * p].val, tree[2 * p + 1].val); + } +} +int minOnSegm(int v, int l, int r) { + if (l <= tree[v].l && tree[v].r <= r) return tree[v].val; + if (l >= tree[v].r || tree[v].l >= r) return N; + return min(minOnSegm(2 * v, l, r), minOnSegm(2 * v + 1, l, r)); +} + +int n; +int a[N + 5]; +int lst[N + 5]; +bool ans[N + 5]; + +int getMex(int l) { + int v = 1; + while(v < N) { + assert(tree[v].val < l); + v *= 2; + if (tree[v].val >= l) v++; + } + return v - N; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + build(); + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d", &a[i]); + a[i]--; + } + for (int i = 0; i < n; i++) + lst[i] = -1; + for (int i = 0; i < n; i++) { + int x = a[i]; + int l = lst[x]; + if (l + 1 < i) { + //eprintf("%d %d\n", l + 1, i); + ans[getMex(l + 1)] = 1; + } + setInPoint(x, i); + lst[x] = i; + } + ans[getMex(0)] = 1; + for (int x = 0; x < n; x++) + if (lst[x] + 1 < n) + ans[getMex(lst[x] + 1)] = 1; + int x = 0; + while(ans[x]) x++; + printf("%d\n", x + 1); + + return 0; +} + diff --git a/code/Um_nik/1437A b/code/Um_nik/1437A new file mode 100644 index 0000000..e375316 --- /dev/null +++ b/code/Um_nik/1437A @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) { + ll x, y; + scanf("%lld%lld", &x, &y); + if (2 * x > y) { + printf("YES\n"); + } else { + printf("NO\n"); + } + } + + return 0; +} + diff --git a/code/Um_nik/1437B b/code/Um_nik/1437B new file mode 100644 index 0000000..1d944e6 --- /dev/null +++ b/code/Um_nik/1437B @@ -0,0 +1,76 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 300300; +int n; +char s[N]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) { + scanf("%d%s", &n, s); + int ans = 0; + for (int i = 0; i < n - 1; i++) + if (s[i] == s[i + 1]) + ans++; + printf("%d\n", (ans + 1) / 2); + } + + return 0; +} + diff --git a/code/Um_nik/1437C b/code/Um_nik/1437C new file mode 100644 index 0000000..6e66762 --- /dev/null +++ b/code/Um_nik/1437C @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int INF = (int)1e8; +const int N = 422; +int n; +int a[N]; +int dp[N][N]; + +void solve() { + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); + sort(a, a + n); + int l = -1, r = INF; + for (int i = 0; i <= n; i++) + for (int x = 0; x <= 2 * n; x++) + dp[i][x] = INF; + dp[0][0] = 0; + for (int i = 0; i <= n; i++) + for (int x = 0; x < 2 * n; x++) { + dp[i][x + 1] = min(dp[i][x + 1], dp[i][x]); + if (i < n) + dp[i + 1][x + 1] = min(dp[i + 1][x + 1], dp[i][x] + abs(a[i] - (x + 1))); + } + printf("%d\n", dp[n][2 * n]); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1437D b/code/Um_nik/1437D new file mode 100644 index 0000000..0d69e15 --- /dev/null +++ b/code/Um_nik/1437D @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 200200; +int n; +int a[N]; +int b[N]; + +void solve() { + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); + b[0] = 1; + int m = 1; + int l = 1; + while(l < n) { + int k = b[m - 1]; + b[m] = 0; + while(k > 0) { + k--; + if (l == n) continue; + l++; + b[m]++; + while(l < n && a[l] > a[l - 1]) { + l++; + b[m]++; + } + } + m++; + } + printf("%d\n", m - 1); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1437E b/code/Um_nik/1437E new file mode 100644 index 0000000..9e61b97 --- /dev/null +++ b/code/Um_nik/1437E @@ -0,0 +1,117 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int INF = (int)1e9 + (int)1e8; +const int N = 500500; +int a[N]; +int c[N]; +int n; + +int solve(int L, int R) { + /* + eprintf("solve\n"); + for (int i = L; i <= R; i++) + eprintf("%d ", a[i]); + eprintf("\n"); + */ + if (a[L] > a[R]) return N; + int len = R - L; + for (int i = 0; i < len; i++) + c[i] = INF; + c[0] = 0; + for (int i = L + 1; i < R; i++) { + if (a[i] < a[L] || a[i] > a[R]) continue; + int p = upper_bound(c, c + len, a[i]) - c; + c[p] = a[i]; + } + int ans = len - 1; + while(c[ans] == INF) ans--; + return len - 1 - ans; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int k; + scanf("%d%d", &n, &k); + for (int i = 1; i <= n; i++) { + scanf("%d", &a[i]); + a[i] += n - i; + } + a[0] = 0; + a[n + 1] = INF - 2; + n += 2; + int ans = 0; + int lst = 0; + for (int i = 0; i < k; i++) { + int x; + scanf("%d", &x); + ans += solve(lst, x); + if (ans >= N) { + printf("-1\n"); + return 0; + } + lst = x; + } + ans += solve(lst, n - 1); + if (ans >= N) { + printf("-1\n"); + } else { + printf("%d\n", ans); + } + + return 0; +} + diff --git a/code/Um_nik/1437F b/code/Um_nik/1437F new file mode 100644 index 0000000..72efd64 --- /dev/null +++ b/code/Um_nik/1437F @@ -0,0 +1,110 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + + +const ll MOD = 998244353; +ll add(ll x, ll y) { + x += y; + if (x >= MOD) return x - MOD; + return x; +} +ll sub(ll x, ll y) { + x -= y; + if (x < 0) return x + MOD; + return x; +} +ll mult(ll x, ll y) { + return (x * y) % MOD; +} +ll bin_pow(ll x, ll p) { + if (p == 0) return 1; + if (p & 1) return mult(x, bin_pow(x, p - 1)); + return bin_pow(mult(x, x), p / 2); +} +ll rev(ll x) { + return bin_pow(x, MOD - 2); +} + +const int N = 5020; +int n; +int a[N]; +int dp[N][N]; +int pref[N][N]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d", &n); + for (int i = 0; i < n; i++) + scanf("%d", &a[i]); + sort(a, a + n); + + for (int i = 0; i < n; i++) + dp[i][0] = 1; + int p = 0; + for (int i = 0; i < n; i++) { + while(2 * a[p] <= a[i]) p++; + for (int x = 0; x < i; x++) + dp[i][x + 1] = add(dp[i][x + 1], pref[x][p]); + for (int x = 0; x < p; x++) + dp[i][x + 1] = add(dp[i][x + 1], mult(dp[i][x], p - x)); + for (int x = 0; x <= i; x++) + pref[x][i + 1] = add(pref[x][i], dp[i][x]); + } + printf("%d\n", dp[n - 1][n - 1]); + + return 0; +} + diff --git a/code/Um_nik/1437G b/code/Um_nik/1437G new file mode 100644 index 0000000..a9373e6 --- /dev/null +++ b/code/Um_nik/1437G @@ -0,0 +1,235 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +struct SegTreeNode { + int l, r; + int val; + + SegTreeNode() : l(-1), r(-1), val(-1) {} + SegTreeNode(int _l, int _r) : l(_l), r(_r), val(-1) {} +}; +struct SegmentTree { + vector tree; + int L; + + SegmentTree() : tree(), L(-1) {} + SegmentTree(int _L) { + L = _L; + while(L & (L - 1)) L++; + tree.resize(2 * L); + for (int i = 0; i < L; i++) + tree[L + i] = SegTreeNode(i, i + 1); + for (int i = L - 1; i > 0; i--) + tree[i] = SegTreeNode(tree[2 * i].l, tree[2 * i + 1].r); + } + + void putInPoint(int v, int x) { + v += L; + tree[v].val = x; + while(v > 1) { + v >>= 1; + tree[v].val = max(tree[2 * v].val, tree[2 * v + 1].val); + } + } + int getMax(int v, int l, int r) { + if (l <= tree[v].l && tree[v].r <= r) return tree[v].val; + if (l >= tree[v].r || tree[v].l >= r) return -1; + return max(getMax(2 * v, l, r), getMax(2 * v + 1, l, r)); + } +}; + +const int N = 300300; +const int A = 26; +int n, q; +int go[N][A]; +int suffLink[N]; +int m; +char s[N]; +int a[N]; +int myVert[N]; +multiset setik[N]; +vector g[N]; +int Q[N]; +int topQ; +int sz[N]; +int info[N][2]; +int treeSz[N]; +int treePar[N]; +SegmentTree trees[N]; + +void dfsSz(int v) { + sz[v] = 1; + for (int u : g[v]) { + dfsSz(u); + sz[v] += sz[u]; + } +} + +void dfsHLD(int v, int id, int p) { + info[v][0] = id; + info[v][1] = p; + treeSz[id] = max(treeSz[id], p + 1); + int big = -1; + for (int u : g[v]) { + if (big == -1 || sz[big] < sz[u]) + big = u; + } + for (int u : g[v]) { + if (u == big) { + dfsHLD(u, id, p + 1); + } else { + treePar[m] = v; + m++; + dfsHLD(u, m - 1, 0); + } + } +} + +void prepare() { + dfsSz(0); + m = 1; + treePar[0] = -1; + dfsHLD(0, 0, 0); + for (int i = 0; i < m; i++) + trees[i] = SegmentTree(treeSz[i]); +} + +void setVertex(int v, int x) { + int id = info[v][0]; + trees[id].putInPoint(info[v][1], x); +} +int getVertex(int v) { + int ans = -1; + while(v != -1) { + int id = info[v][0]; + ans = max(ans, trees[id].getMax(1, 0, info[v][1] + 1)); + v = treePar[id]; + } + return ans; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d%d", &n, &q); + m = 1; + for (int i = 0; i < n; i++) { + scanf("%s", s); + int L = strlen(s); + int v = 0; + for (int j = 0; j < L; j++) { + int c = (int)(s[j] - 'a'); + if (go[v][c] == 0) go[v][c] = m++; + v = go[v][c]; + } + myVert[i] = v; + } + Q[topQ++] = 0; + suffLink[0] = -1; + for (int i = 0; i < topQ; i++) { + int v = Q[i]; + for (int c = 0; c < A; c++) { + if (go[v][c] == 0) continue; + int u = go[v][c]; + int w = suffLink[v]; + while(w != -1 && go[w][c] == 0) w = suffLink[w]; + if (w == -1) + w = 0; + else + w = go[w][c]; + suffLink[u] = w; + g[w].push_back(u); + Q[topQ++] = u; + } + } + prepare(); + for (int i = 0; i < n; i++) { + int v = myVert[i]; + setik[v].insert(0); + setVertex(v, 0); + } + while(q--) { + int t; + scanf("%d", &t); + if (t == 1) { + int p, v, x; + scanf("%d%d", &p, &x); + p--; + v = myVert[p]; + setik[v].erase(setik[v].find(a[p])); + a[p] = x; + setik[v].insert(x); + setVertex(v, *setik[v].rbegin()); + } else { + scanf("%s", s); + int L = strlen(s); + int v = 0; + int ans = -1; + for (int i = 0; i < L; i++) { + int c = (int)(s[i] - 'a'); + while(v != -1 && go[v][c] == 0) v = suffLink[v]; + if (v == -1) + v = 0; + else + v = go[v][c]; + ans = max(ans, getVertex(v)); + } + printf("%d\n", ans); + } + } + + return 0; +} + diff --git a/code/Um_nik/1441A b/code/Um_nik/1441A new file mode 100644 index 0000000..360cfa4 --- /dev/null +++ b/code/Um_nik/1441A @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + + +const ll MOD = 998244353; +ll add(ll x, ll y) { + x += y; + if (x >= MOD) return x - MOD; + return x; +} +ll sub(ll x, ll y) { + x -= y; + if (x < 0) return x + MOD; + return x; +} +ll mult(ll x, ll y) { + return (x * y) % MOD; +} +ll bin_pow(ll x, ll p) { + if (p == 0) return 1; + if (p & 1) return mult(x, bin_pow(x, p - 1)); + return bin_pow(mult(x, x), p / 2); +} +ll rev(ll x) { + return bin_pow(x, MOD - 2); +} + +const int N = 200200; +int n; +int a[N]; +int b[N]; +bool bad[N]; + +void solve() { + int k; + scanf("%d%d", &n, &k); + for (int i = 1; i <= n; i++) { + int x; + scanf("%d", &x); + b[x] = i; + } + for (int i = 0; i < k; i++) { + scanf("%d", &a[i]); + a[i] = b[a[i]]; +// eprintf("%d ", a[i]); + } +// eprintf("\n"); + n += 2; + for (int i = 0; i < n; i++) + bad[i] = false; + bad[0] = bad[n - 1] = true; + for (int i = 0; i < k; i++) + bad[a[i]] = true; + int ans = 1; + for (int i = 0; i < k; i++) { + int c = 0; + for (int j = -1; j <= 1; j += 2) + c += !bad[a[i] + j]; + bad[a[i]] = false; + ans = mult(ans, c); + } + printf("%d\n", ans); +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int t; + scanf("%d", &t); + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/1441B b/code/Um_nik/1441B new file mode 100644 index 0000000..0e4c9e4 --- /dev/null +++ b/code/Um_nik/1441B @@ -0,0 +1,192 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + + +const ll MOD = 998244353; +ll add(ll x, ll y) { + x += y; + if (x >= MOD) return x - MOD; + return x; +} +ll sub(ll x, ll y) { + x -= y; + if (x < 0) return x + MOD; + return x; +} +ll mult(ll x, ll y) { + return (x * y) % MOD; +} +ll bin_pow(ll x, ll p) { + if (p == 0) return 1; + if (p & 1) return mult(x, bin_pow(x, p - 1)); + return bin_pow(mult(x, x), p / 2); +} +ll rev(ll x) { + return bin_pow(x, MOD - 2); +} + + +const int N = 200200; +const int LOG = 20; +vector Q[N + 1]; +int dist[N]; +vector g[2][N]; +int n; +pii d2[N][2]; + +void read() { + /* + n = 27; + for (int i = 0; i < n / 2; i++) { + g[1][2 * i].push_back(2 * i + 1); + g[0][2 * i + 1].push_back(2 * i); + g[1][2 * i + 2].push_back(2 * i + 1); + g[0][2 * i + 1].push_back(2 * i + 2); + + } + */ + + int m; + scanf("%d%d", &n, &m); + while(m--) { + int v, u; + scanf("%d%d", &v, &u); + v--;u--; + g[0][v].push_back(u); + g[1][u].push_back(v); + } + +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + read(); + + for (int v = 0; v < n; v++) + dist[v] = N; + dist[0] = 0; + + int ans = (int)1e8; + for (int t = 0; t < LOG; t++) { + //eprintf("t = %d\n", t); + for (int i = 0; i <= N; i++) + Q[i].clear(); + for (int v = 0; v < n; v++) { + if (dist[v] < N) + Q[dist[v]].push_back(v); + } + for (int d = 0; d < N; d++) { + while(!Q[d].empty()) { + int v = Q[d].back(); + //eprintf("v = %d\n", v); + Q[d].pop_back(); + if (dist[v] < d) continue; + for (int u : g[t & 1][v]) { + if (dist[u] <= d + 1) continue; + dist[u] = d + 1; + Q[d + 1].push_back(u); + } + } + } + if (dist[n - 1] < N) + ans = min(ans, dist[n - 1] + (1 << t) - 1); + } + if (ans < (int)1e8) { + printf("%d\n", ans); + return 0; + } + for (int v = 0; v < n; v++) { + if (dist[v] < N) { + for (int j = 0; j < 2; j++) + d2[v][(LOG - 1 + j) & 1] = mp(LOG - 1 + j, dist[v]); + } + else { + for (int j = 0; j < 2; j++) + d2[v][j] = mp(N, N); + } + } + set> setik; + for (int v = 0; v < n; v++) { + for (int j = 0; j < 2; j++) + setik.insert(mp(d2[v][j], mp(v, j))); + } + while(!setik.empty()) { + int v = setik.begin()->second.first; + int t = setik.begin()->second.second; + setik.erase(setik.begin()); + int p = d2[v][t].first, s = d2[v][t].second; + for (int u : g[t][v]) { + pii w = mp(p, s + 1); + if (d2[u][t] <= w) continue; + setik.erase(mp(d2[u][t], mp(u, t))); + d2[u][t] = w; + setik.insert(mp(d2[u][t], mp(u, t))); + } + pii w = mp(p + 1, s); + if (d2[v][t ^ 1] <= w) continue; + setik.erase(mp(d2[v][t ^ 1], mp(v, t ^ 1))); + d2[v][t ^ 1] = w; + setik.insert(mp(d2[v][t ^ 1], mp(v, t ^ 1))); + } + pii res = min(d2[n - 1][0], d2[n - 1][1]); + eprintf("%d %d\n", res.first, res.second); + ans = bin_pow(2, res.first) - 1; + ans = add(ans, res.second); + printf("%d\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1441C b/code/Um_nik/1441C new file mode 100644 index 0000000..3197b3b --- /dev/null +++ b/code/Um_nik/1441C @@ -0,0 +1,115 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 3030; +int n, k; +vector a[N]; +ll b[N][2]; +ll dp[N][N]; +int m; +ll ans; + +void addDp(int L, int R) { + for (int i = L; i < R; i++) { + for (int x = 0; x <= k; x++) { + dp[m + 1][x] = dp[m][x]; + if (x >= b[i][0]) + dp[m + 1][x] = max(dp[m + 1][x], dp[m][x - b[i][0]] + b[i][1]); + } + m++; + } +} + +void solve(int L, int R) { + if (L + 1 == R) { + ll cur = 0; + for (int i = 0; i <= k && i <= (int)a[L].size(); i++) { + ans = max(ans, cur + dp[m][k - i]); + if (i < (int)a[L].size()) cur += a[L][i]; + } + return; + } + int oldm = m; + int M = (L + R) / 2; + addDp(M, R); + solve(L, M); + m = oldm; + addDp(L, M); + solve(M, R); + m = oldm; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d%d", &n, &k); + for (int i = 0; i < n; i++) { + int sz; + scanf("%d", &sz); + b[i][0] = sz; + b[i][1] = 0; + a[i] = vector(sz); + for (int j = 0; j < sz; j++) { + scanf("%lld", &a[i][j]); + b[i][1] += a[i][j]; + } + } + m = 0; + solve(0, n); + printf("%lld\n", ans); + + return 0; +} + diff --git a/code/Um_nik/1442F b/code/Um_nik/1442F new file mode 100644 index 0000000..95d8722 --- /dev/null +++ b/code/Um_nik/1442F @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int N = 1010; +const int M = (1 << 18) + 5; +int U[M]; +int ans[5555][3]; +int ansSz; +int n, s; +int nim[N]; +vector W; +vector g[N]; +char sss[111]; + +void addAns(int x, int y, int z) { + ans[ansSz][0] = x; + ans[ansSz][1] = y; + ans[ansSz][2] = z; + ansSz++; +} + +void solve() { + int mask = 0; + int ans = -1; + for (int i = 0; i < s; i++) { + printf("? 1 %d\n", W[i]); + fflush(stdout); + scanf("%s", sss); + if (sss[0] == 'W') { + mask ^= 1 << i; + } else if (sss[0] == 'L') { + ans = W[i]; + } else if (sss[0] == 'D') { + + } else if (sss[0] == 'S') { + throw; + } else throw; + } + if (ans == -1) { + assert(U[mask] != 0); + ans = U[mask]; + } + printf("! %d\n", ans); + fflush(stdout); + scanf("%s", sss); + if (sss[0] == 'C') return; + exit(0); +} + +int findBestMask(int mask) { + vector Q; + Q.push_back(mask); + for (int i = 0; i < (int)Q.size(); i++) { + int v = Q[i]; + if (U[v] == 0) return v; + for (int j = 0; j < s; j++) { + int u = v ^ (1 << j); + Q.push_back(u); + } + } + throw; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + int m, t; + scanf("%d%d%d", &n, &m, &t); + while(m--) { + int v, u; + scanf("%d%d", &v, &u); + g[v].push_back(u); + } + s = min(18, n); + vector ord; + for (int v = 1; v <= n; v++) + ord.push_back(mp((int)g[v].size(), v)); + sort(all(ord)); + W.resize(n); + for (int v = 1; v <= n; v++) + nim[v] = -1; + for (int i = 0; i < s; i++) { + int v = ord[i].second; + W[i] = v; + nim[v] = i; + for (int u : g[v]) { + addAns(-1, v, u); + } + g[v].clear(); + } + for (int i = 0; i < s; i++) + for (int j = i + 1; j < s; j++) + addAns(1, W[j], W[i]); + for (int v = 1; v <= n; v++) { + if (nim[v] != -1) continue; + addAns(1, v, v); + int mask = 0; + for (int u : g[v]) { + if (nim[u] != -1) + mask |= 1 << nim[u]; + } + int nmask = findBestMask(mask); + U[nmask] = v; + for (int i = 0; i < s; i++) { + if (((mask ^ nmask) >> i) & 1) { + if ((mask >> i) & 1) { + addAns(-1, v, W[i]); + } else { + addAns(1, v, W[i]); + } + } + } + } + + printf("%d\n", ansSz); + for (int i = 0; i < ansSz; i++) { + if (ans[i][0] == -1) + printf("-"); + else + printf("+"); + printf(" %d %d\n", ans[i][1], ans[i][2]); + } + fflush(stdout); + + while(t--) solve(); + + return 0; +} + diff --git a/code/Um_nik/165E b/code/Um_nik/165E new file mode 100644 index 0000000..83b9cc5 --- /dev/null +++ b/code/Um_nik/165E @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const int K = 22; +const int N = (int)1e6 + 7; +int n; +int a[N]; +int b[(1 << K) + 3]; + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + for (int i = 0; i < (1 << K); i++) + b[i] = -1; + scanf("%d", &n); + for (int i = 0; i < n; i++) { + scanf("%d", &a[i]); + b[((1 << K) - 1) ^ a[i]] = i; + //b[a[i]] = i; + } + for (int k = 0; k < K; k++) { + for (int mask = 0; mask < (1 << K); mask++) { + if (((mask >> k) & 1) == 0) continue; + if (b[mask] == -1) continue; + b[mask ^ (1 << k)] = b[mask]; + } + } + for (int i = 0; i < n; i++) { + if (b[a[i]] == -1) + printf("-1 "); + else + printf("%d ", a[b[a[i]]]); + } + printf("\n"); + + return 0; +} + diff --git a/code/Um_nik/83B b/code/Um_nik/83B new file mode 100644 index 0000000..1d864dc --- /dev/null +++ b/code/Um_nik/83B @@ -0,0 +1,108 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; + +#ifdef LOCAL + #define eprintf(...) fprintf(stderr, __VA_ARGS__);fflush(stderr); +#else + #define eprintf(...) 42 +#endif + +using ll = long long; +using ld = long double; +using uint = unsigned int; +using ull = unsigned long long; +template +using pair2 = pair; +using pii = pair; +using pli = pair; +using pll = pair; +mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +ll myRand(ll B) { + return (ull)rng() % B; +} + +#define pb push_back +#define mp make_pair +#define all(x) (x).begin(),(x).end() +#define fi first +#define se second + +clock_t startTime; +double getCurrentTime() { + return (double)(clock() - startTime) / CLOCKS_PER_SEC; +} + +const ll INF = (ll)1e9 + 7; +const int N = 300300; +int n; +ll k; +ll a[N]; +vector ans1, ans2; + +ll count(ll x) { + ll ans = 0; + for (int i = 0; i < n; i++) + ans += min(x, a[i]); + return ans; +} + +int main() +{ + startTime = clock(); +// freopen("input.txt", "r", stdin); +// freopen("output.txt", "w", stdout); + + scanf("%d%lld", &n, &k); + for (int i = 0; i < n; i++) + scanf("%lld", &a[i]); + + ll l = 0, r = INF; + while(r - l > 1) { + ll mid = (l + r) / 2; + if (count(mid) < k) + l = mid; + else + r = mid; + } + if (r == INF) { + printf("-1\n"); + return 0; + } + k -= count(l); + for (int i = 0; i < n; i++) { + if (a[i] <= l) continue; + if (k == 0) { + ans2.push_back(i + 1); + } else { + k--; + if (a[i] > r) + ans1.push_back(i + 1); + } + } + for (int x : ans2) + printf("%d ", x); + for (int x : ans1) + printf("%d ", x); + printf("\n"); + + return 0; +} + diff --git a/code/ecnerwala/1421A b/code/ecnerwala/1421A new file mode 100644 index 0000000..57450c3 --- /dev/null +++ b/code/ecnerwala/1421A @@ -0,0 +1 @@ +for l in[*open(0)][1:]:a,b=map(int,l.split());print(a^b) diff --git a/code/ecnerwala/1421B b/code/ecnerwala/1421B new file mode 100644 index 0000000..deccc0e --- /dev/null +++ b/code/ecnerwala/1421B @@ -0,0 +1,25 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int N; cin >> N; + vector G(N); for (auto& s : G) cin >> s; + vector> ans0; + vector> ans1; + (G[0][1] == '0' ? ans0 : ans1).push_back({0,1}); + (G[1][0] == '0' ? ans0 : ans1).push_back({1,0}); + (G[N-1][N-2] == '1' ? ans0 : ans1).push_back({N-1,N-2}); + (G[N-2][N-1] == '1' ? ans0 : ans1).push_back({N-2,N-1}); + if (ans0.size() > ans1.size()) swap(ans0, ans1); + cout << ans0.size() << '\n'; + for (auto it : ans0) { + cout << it.first+1 << ' ' << it.second+1 << '\n'; + } + } + + return 0; +} + diff --git a/code/ecnerwala/1421C b/code/ecnerwala/1421C new file mode 100644 index 0000000..bed8e57 --- /dev/null +++ b/code/ecnerwala/1421C @@ -0,0 +1,20 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + string S; cin >> S; + int N = int(S.size()); + cout << 3 << '\n'; + cout << 'R' << ' ' << N-1 << '\n'; + cout << 'L' << ' ' << N << '\n'; + cout << 'L' << ' ' << 2 << '\n'; + + return 0; +} + +// abac +// abaca +// cababaca +// acababaca + diff --git a/code/ecnerwala/1421D b/code/ecnerwala/1421D new file mode 100644 index 0000000..b21df39 --- /dev/null +++ b/code/ecnerwala/1421D @@ -0,0 +1,30 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + + int T; cin >> T; + while (T--) { + int64_t X, Y; cin >> X >> Y; + array C; for (auto& c : C) cin >> c; + // Y * C1 + X * C5 + int64_t ans = + (Y >= 0 ? C[1] * Y : C[4] * -Y) + + (X >= 0 ? C[5] * X : C[2] * -X); + // (X-Y) * C5 + Y * C0 + ans = min(ans, + ((X-Y) >= 0 ? C[5] * (X-Y) : C[2] * -(X-Y)) + + (Y >= 0 ? C[0] * Y : C[3] * -Y) + ); + // X * C0 + (X-Y) * C4 + ans = min(ans, + ((X-Y) >= 0 ? C[4] * (X-Y) : C[1] * -(X-Y)) + + (X >= 0 ? C[0] * X : C[3] * -X) + ); + cout << ans << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1421E b/code/ecnerwala/1421E new file mode 100644 index 0000000..0debd8e --- /dev/null +++ b/code/ecnerwala/1421E @@ -0,0 +1,45 @@ +#include + +template void setmax(T& a, T b) { if (b > a) a = b; } + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + + using i64 = int64_t; + + int N; cin >> N; + vector A(N); + for (int i = 0; i < N; i++) { + cin >> A[i]; + } + if (N == 1) { + cout << A[0] << '\n'; + exit(0); + } + + const i64 INF = 1e18; + array dp_alternating{0,-INF,-INF}; + array dp{-INF,-INF,-INF}; + for (int i = 0; i < N; i++) { + array ndp_alternating{-INF,-INF,-INF}; + array ndp{-INF,-INF,-INF}; + for (int a = 0; a < 3; a++) { + for (int z : {1, -1}) { + setmax(ndp[(a + 3 + z) % 3], dp[a] + z * A[i]); + setmax((z == (i % 2 ? -1 : 1) ? ndp_alternating : ndp)[(a + 3 + z) % 3], dp_alternating[a] + z * A[i]); + } + } + dp = ndp; + dp_alternating = ndp_alternating; + } + + cout << dp[1] << '\n'; + + return 0; +} + +// An array is valid if you can take 2 adjacent equal and replace with 1 opposite, and it converges to 1 +// [+ - + - +] +// [+ + - - +] -> [- - - +] -> [- + +] -> [- -] -> [+] + diff --git a/code/ecnerwala/1434A b/code/ecnerwala/1434A new file mode 100644 index 0000000..9c0d50b --- /dev/null +++ b/code/ecnerwala/1434A @@ -0,0 +1,36 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + array A; + for (int& a : A) cin >> a; + sort(A.begin(), A.end()); + int N; cin >> N; + vector B(N); for (int& b : B) cin >> b; + + // Set of fret #, note #, string # + set> vals; + for (int i = 0; i < N; i++) { + vals.insert({B[i] - A[5], i, 5}); + } + auto get_cur_range = [&]() -> int { + return get<0>(*vals.rbegin()) - get<0>(*vals.begin()); + }; + int ans = get_cur_range(); + while (true) { + auto [v, i, j] = *vals.begin(); + vals.erase(vals.begin()); + if (j == 0) { + // this string is already as long as possible, so we're stuck + break; + } + j--; + vals.insert({B[i] - A[j], i, j}); + ans = min(ans, get_cur_range()); + } + cout << ans << '\n'; + + return 0; +} + diff --git a/code/ecnerwala/1434B b/code/ecnerwala/1434B new file mode 100644 index 0000000..aec809a --- /dev/null +++ b/code/ecnerwala/1434B @@ -0,0 +1,34 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int N; cin >> N; + stack> stk; + stk.push({-1, N}); + vector ans(N); + int num_placed = 0; + for (int z = 0; z < 2 * N; z++) { + char c; cin >> c; + if (c == '+') { + stk.push({num_placed++, 0}); + } else if (c == '-') { + int v; cin >> v; + if (stk.top().second >= v) { + cout << "NO" << '\n'; + exit(0); + } + ans[stk.top().first] = v; + stk.pop(); + stk.top().second = max(stk.top().second, v); + } else assert(false); + } + + cout << "YES" << '\n'; + for (int i = 0; i < N; i++) { + cout << ans[i] << " "; + } + cout << '\n'; + return 0; +} + diff --git a/code/ecnerwala/1434C b/code/ecnerwala/1434C new file mode 100644 index 0000000..e506a41 --- /dev/null +++ b/code/ecnerwala/1434C @@ -0,0 +1,29 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + + int T; cin >> T; + while (T--) { + int64_t A, B, C, D; cin >> A >> B >> C >> D; + cout << [&]() -> int64_t { + if (B * C < A) { + return -1; + } + // We'll cast the spell at + // 0, D, 2D, 3D, 4D + // The time before the i-1th and the ith cast will deal + // -A + B*sum_{j=0}^{i-1} min(D, max(C - j * D, 0)) + // Find max k so that (D * k) * B - A < 0 + // D * k * B < A + int64_t k = (A-1) / D / B; + assert(D * k <= C); + // We will take time D*k as our end + return A * (k+1) - B * D * (k * (k+1)) / 2; + }() << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1434D b/code/ecnerwala/1434D new file mode 100644 index 0000000..34c213d --- /dev/null +++ b/code/ecnerwala/1434D @@ -0,0 +1,532 @@ +#include + +using std::pair; +using std::swap; + +template T& setmax(T& a, const T& b) { if (b > a) a = b; return a; } +template T& setmin(T& a, const T& b) { if (b < a) a = b; return a; } + +const int INF = 1e9; + +struct top_tree_node { + mutable top_tree_node* p = nullptr; + top_tree_node* c[3] = {nullptr, nullptr, nullptr}; + + int d() const { + assert(p); + if (this == p->c[0]) { + return 0; + } else if (this == p->c[1]) { + return 1; + } else if (this == p->c[2]) { + return 2; + } else assert(false); + } + top_tree_node*& p_c() const { return p->c[d()]; } // p->c which points to you + + // 3 types of verts: path edges, path verts, non-path edges + bool is_path; + bool is_vert; + + bool r() const { return !p || p->is_path != is_path; } + + bool flip_path = false; + + bool own_parity; + + int path_len; + bool path_parity; + int best_path; + std::array best_up; + std::array best_down; + + void do_flip_path() { + assert(is_path); + flip_path ^= 1; + swap(best_up, best_down); + } + + void do_flip_edge_parity() { + assert(!is_vert); + own_parity ^= 1; + update(); + } + + void downdate() { + if (flip_path) { + assert(is_path); + if (!is_vert) { + if (c[0]) c[0]->do_flip_path(); + if (c[1]) c[1]->do_flip_path(); + } + swap(c[0], c[1]); + flip_path = false; + } + } + + void update() { + if (is_vert) { + assert(is_path); + assert(!c[2]); + + path_len = 0; + path_parity = 0; + best_path = 0; + best_up = {0, -INF}; + for (int d = 0; d < 2; d++) { + if (!c[d]) continue; + setmax(best_path, c[d]->best_path); + for (int z = 0; z < 2; z++) { + setmax(best_path, c[d]->best_up[z] + best_up[z]); + setmax(best_up[z], c[d]->best_up[z]); + } + } + best_down = best_up; + } else if (is_path) { + assert(!c[2]); + assert(c[0] && c[1]); + + // path edge + path_len = 1; + path_parity = own_parity; + best_up = {0, -INF}; + best_down = {0, -INF}; + setmax(best_up[path_parity], path_len); + setmax(best_down[path_parity], path_len); + best_path = std::max(best_up[0], best_down[0]); + + // merge c[0] and self + { + setmax(best_path, c[0]->best_path); + + setmax(best_path, c[0]->best_down[0] + best_up[0]); + setmax(best_path, c[0]->best_down[1] + best_up[1]); + + if (c[0]->path_parity) { + swap(best_up[0], best_up[1]); + } + best_up[0] += c[0]->path_len; + best_up[1] += c[0]->path_len; + + setmax(best_up[0], c[0]->best_up[0]); + setmax(best_up[1], c[0]->best_up[1]); + + setmax(best_down[0 ^ path_parity], c[0]->best_down[0] + path_len); + setmax(best_down[1 ^ path_parity], c[0]->best_down[1] + path_len); + + path_len += c[0]->path_len; + path_parity ^= c[0]->path_parity; + } + + // merge self and c[1] + { + setmax(best_path, c[1]->best_path); + + setmax(best_path, best_down[0] + c[1]->best_up[0]); + setmax(best_path, best_down[1] + c[1]->best_up[1]); + + if (c[1]->path_parity) { + swap(best_down[0], best_down[1]); + } + best_down[0] += c[1]->path_len; + best_down[1] += c[1]->path_len; + + setmax(best_down[0], c[1]->best_down[0]); + setmax(best_down[1], c[1]->best_down[1]); + + setmax(best_up[0 ^ path_parity], c[1]->best_up[0] + path_len); + setmax(best_up[1 ^ path_parity], c[1]->best_up[1] + path_len); + + path_len += c[1]->path_len; + path_parity ^= c[1]->path_parity; + } + } else { + assert(c[2]); + + best_path = c[2]->best_path; + best_up = c[2]->best_up; + + // add yourself + if (own_parity) { + swap(best_up[0], best_up[1]); + } + best_up[0]++, best_up[1]++; + setmax(best_path, best_up[0]); + + for (int d = 0; d < 2; d++) { + if (!c[d]) continue; + setmax(best_path, c[d]->best_path); + for (int z = 0; z < 2; z++) { + setmax(best_path, c[d]->best_up[z] + best_up[z]); + setmax(best_up[z], c[d]->best_up[z]); + } + } + } + + if (flip_path) { + assert(is_path); + swap(best_up, best_down); + } + } + + void downdate_all() { + if (p) p->downdate_all(); + downdate(); + } + + void update_all() { + update(); + if (p) p->update_all(); + } + +private: + + void rot() { + assert(!is_vert); + assert(!r()); + top_tree_node* pa = p; + int x = d(); assert(x == 0 || x == 1); + top_tree_node* ch = c[!x]; + + if (pa->p) pa->p_c() = this; + this->p = pa->p; + + pa->c[x] = ch; + if (ch) ch->p = pa; + + this->c[!x] = pa; + pa->p = this; + + pa->update(); + } + + void rot_2(int c_d) { + assert(!is_vert); + assert(!r()); + assert(c[c_d]); + assert(!c[c_d]->is_vert); + + if (d() == c_d) { + rot(); + return; + } + + top_tree_node* pa = p; + int x = d(); assert(x == 0 || x == 1); + assert(c_d == !x); + top_tree_node* ch = c[c_d]->c[!x]; + + if (pa->p) pa->p_c() = this; + this->p = pa->p; + + pa->c[x] = ch; + if (ch) ch->p = pa; + + this->c[c_d]->c[!x] = pa; + pa->p = this->c[c_d]; + + pa->update(); + } + + void splay_dir(int x) { + while (!r() && d() == x) { + if (!p->r() && p->d() == x) { + p->rot(); + } + rot(); + } + } + + void splay_2(int c_d) { + assert(!is_vert && is_path); + assert(c[c_d] && !c[c_d]->is_vert); + while (!r()) { + if (!p->r()) { + if (p->d() == d()) { + p->rot(); + } else { + rot_2(c_d); + } + } + rot_2(c_d); + } + } + + void splay_2() { + assert(!is_vert && is_path); + assert(!r()); + p->splay_2(d()); + } + + void splay_vert() { + assert(is_vert); + if (r()) { + return; + } + p->splay_dir(d()); + if (p->r()) { + return; + } + + assert(p->d() != d()); + // we have a preference to be the left child + if (d() == 1) { + p->rot(); + } + assert(d() == 0); + + p->splay_2(); + assert(d() == 0); + assert(p->d() == 1); + assert(p->p->r()); + } + + void splay() { + assert(!is_vert); + while (!r()) { + if (!p->r()) { + if (p->d() == d()) { + p->rot(); + } else { + rot(); + } + } + rot(); + } + } + + top_tree_node* cut_right() { + assert(is_vert && is_path); + splay_vert(); + + if (r() || d() == 1) { + assert(r() || (d() == 1 && p->r())); + assert(c[0] == nullptr); + return nullptr; + } + + top_tree_node* pa = p; + assert(pa->r() || (pa->d() == 1 && pa->p->r())); + assert(!pa->is_vert); + assert(pa->is_path); + assert(pa->c[0] == this); + assert(pa->c[2] == nullptr); + + if (pa->p) pa->p_c() = this; + this->p = pa->p; + + pa->is_path = false; + pa->c[2] = pa->c[1]; // don't need to change the parent + + pa->c[0] = c[0]; if (c[0]) c[0]->p = pa; + pa->c[1] = c[1]; if (c[1]) c[1]->p = pa; + + c[0] = nullptr; + c[1] = pa; pa->p = this; + assert(c[2] == nullptr); + + assert(c[0] == nullptr); + + pa->update(); + return pa; + } + + top_tree_node* splice_non_path() { + assert(!is_path); + assert(!is_vert); + + splay(); + assert(p && p->is_vert && p->is_path); + p->cut_right(); + + if (!p->is_path) rot(); + assert(p && p->is_vert && p->is_path); + assert(p->r() || (p->d() == 1 && p->p->r())); + assert(p->c[d()] == this && p->c[!d()] == nullptr); + + top_tree_node* pa = p; + + if (pa->p) pa->p_c() = this; + this->p = pa->p; + + pa->c[0] = c[0]; if (c[0]) c[0]->p = pa; + pa->c[1] = c[1]; if (c[1]) c[1]->p = pa; + + assert(c[2] && c[2]->is_path); + c[1] = c[2]; // don't need to change parent + c[0] = pa; pa->p = this; + c[2] = nullptr; + + is_path = true; + + pa->update(); + return pa; + } + + void splice_all(top_tree_node*& res) { + if (!is_path) { + res = splice_non_path(); + } + assert(is_path); + if (!p) return; + p->splice_all(res); + } + +public: + top_tree_node* expose() { + assert(is_vert); + downdate_all(); + + top_tree_node* res = nullptr; + splice_all(res); + + cut_right(); + + update_all(); + + return res; + } + + void meld_path_end() { + assert(!p); + top_tree_node* rt = this; + while (true) { + rt->downdate(); + if (rt->is_vert) break; + rt = rt->c[1]; + } + assert(rt->is_vert); + rt->splay_vert(); + if (rt->c[0] && rt->c[1]) { + top_tree_node* ch = rt->c[1]; + while (true) { + ch->downdate(); + if (!ch->c[0]) break; + ch = ch->c[0]; + } + ch->splay(); + assert(ch->c[0] == nullptr); + + ch->c[0] = rt->c[0]; + ch->c[0]->p = ch; + + rt->c[0] = nullptr; + + ch->update(); + } else if (rt->c[0]) { + rt->c[1] = rt->c[0]; + rt->c[0] = nullptr; + } + assert(rt->c[0] == nullptr); + rt->update_all(); + } + + void make_root() { + expose(); + + top_tree_node* rt = this; + while (rt->p) { + assert(rt->d() == 1); + rt = rt->p; + } + rt->do_flip_path(); + rt->meld_path_end(); + + expose(); + + assert(!p); + } + + // link v2 as a child of v1 with edge e + friend void link(top_tree_node* e, top_tree_node* v1, top_tree_node* v2) { + assert(e && v1 && v2); + assert(!e->c[0] && !e->c[1] && !e->c[2]); + v1->expose(); while (v1->p) v1 = v1->p; + v2->make_root(); + + assert(!v1->p); + assert(!v2->p); + + e->is_path = true, e->is_vert = false; + e->c[0] = v1; + v1->p = e; + e->c[1] = v2; + v2->p = e; + e->update(); + } + + friend pair cut(top_tree_node* e) { + assert(!e->p); + assert(e->is_path); + assert(!e->is_vert); + + e->downdate(); + + top_tree_node* l = e->c[0]; + top_tree_node* r = e->c[1]; + assert(l && r); + + e->c[0] = e->c[1] = nullptr; + l->p = r->p = nullptr; + + assert(e->c[2] == nullptr); + + l->meld_path_end(); + + return {l, r}; + } + + friend top_tree_node* get_path(top_tree_node* a, top_tree_node* b) { + assert(a->is_vert && b->is_vert); + a->make_root(); + b->expose(); + if (a == b) { + assert(!b->p); + return b; + } + assert(!b->p->p); + return b->p; + } + + friend top_tree_node* get_subtree(top_tree_node* rt, top_tree_node* n) { + rt->make_root(); + n->expose(); + return n; + } +}; + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int N; cin >> N; + + vector nodes(N+(N-1)); + + for (int i = 0; i < N; i++) { + top_tree_node* n = &nodes[i]; + n->is_path = n->is_vert = true; + n->update(); + } + + vector> edges(N-1); + for (int e = 0; e < N-1; e++) { + int u, v, t; cin >> u >> v >> t; u--, v--; + edges[e] = {u, v}; + + nodes[N+e].own_parity = t; + link(&nodes[N+e], &nodes[u], &nodes[v]); + } + + int Q; cin >> Q; + while (Q--) { + int e; cin >> e; e--; + auto [u,v] = edges[e]; + auto pth = get_path(&nodes[u], &nodes[v]); + assert(pth == &nodes[N+e]); + pth->do_flip_edge_parity(); + cout << pth->best_path << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1434E b/code/ecnerwala/1434E new file mode 100644 index 0000000..f7f8ad5 --- /dev/null +++ b/code/ecnerwala/1434E @@ -0,0 +1,80 @@ +#include + +template T& setmax(T& a, const T& b) { if (b > a) a = b; return a; } +template T& setmin(T& a, const T& b) { if (b < a) a = b; return a; } + +int get_nimber(std::vector A) { + using namespace std; + + //cerr << "START" << '\n'; + //for (auto a : A) { cerr << a << ' '; } cerr << '\n'; + + assert(std::is_sorted(A.begin(), A.end())); + + // find the maximum convex subsequence + int N = int(A.size()); + + set step_0; + const int MAXL = 500; + vector> min_first(MAXL+1); + vector val(N); + vector> to_insert(N); + vector cutoffs; cutoffs.reserve(MAXL+1); + + for (int i = N-1; i >= 0; i--) { + cutoffs.clear(); + for (int l = 0; l <= MAXL; l++) { + while (!min_first[l].empty() && val[min_first[l].top()] != l) { + min_first[l].pop(); + } + if (min_first[l].empty()) { + break; + } + assert(l+1 <= MAXL); + int nv = 2 * A[i] - A[min_first[l].top()]; + assert(nv < A[i]); + int j = int(upper_bound(A.begin(), A.end(), nv) - A.begin()); + assert(j <= i); + cutoffs.push_back(j); + } + int V = int(cutoffs.size()); + step_0.insert(V); + + val[i] = V; + min_first[V].push(i); + for (int v = 1; v < V; v++) { + cutoffs[v] = max(cutoffs[v-1], cutoffs[v]); + } + for (int v = V-1; v >= 0; v--) { + to_insert[cutoffs[v]].push_back(i); + } + + for (auto a : to_insert[i]) { + val[a]--; + min_first[val[a]].push(a); + } + } + + int ans = 0; + while (step_0.count(ans)) ans++; + //cerr << "Answer " << ans << '\n'; + return ans; +} + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + + int T; cin >> T; + int ans = 0; + while (T--) { + int N; cin >> N; + vector A(N); + for (auto& a : A) cin >> a; + ans ^= get_nimber(std::move(A)); + } + cout << (ans != 0 ? "YES" : "NO") << '\n'; + + return 0; +} + diff --git a/code/ecnerwala/1435A b/code/ecnerwala/1435A new file mode 100644 index 0000000..51c8d57 --- /dev/null +++ b/code/ecnerwala/1435A @@ -0,0 +1,12 @@ +#include +int main() { +using namespace std; +int T; cin >> T; +while (T--) { +int N; cin >> N; N /= 2; +while (N--) { +int a, b; cin >> a >> b; +cout << b << ' ' << -a << '\n'; +} +} +} diff --git a/code/ecnerwala/1435B b/code/ecnerwala/1435B new file mode 100644 index 0000000..c06ee71 --- /dev/null +++ b/code/ecnerwala/1435B @@ -0,0 +1,26 @@ +#include +using namespace std; + +int main() { +int T; cin >> T; +while (T--) { +int N, M; cin >> N >> M; +vector idx(N*M); +for (int i = 0; i < N; i++) { +for (int j = 0; j < M; j++) { +int v; cin >> v; v--; +idx[v] += j; +} +} +for (int j = 0; j < M; j++) { +for (int i = 0; i < N; i++) { +int v; cin >> v; v--; +idx[v] += i*M; +} +} +vector ans(N*M); +for (int i = 0; i < N*M; i++) ans[idx[i]] = i+1; +for (int i = 0; i < N*M; i++) cout << ans[i] << ' '; +cout << '\n'; +} +} diff --git a/code/ecnerwala/1441F b/code/ecnerwala/1441F new file mode 100644 index 0000000..5791d31 --- /dev/null +++ b/code/ecnerwala/1441F @@ -0,0 +1,167 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + + while (T--) { + int N, M; cin >> N >> M; + using pt_t = complex; + vector pts(N); + for (auto& pt : pts) { int x, y; cin >> x >> y; pt = {x,y}; } + vector dest(2*M); + vector>> adj(N); + for (int e = 0; e < M; e++) { + int u, v; cin >> u >> v; u--, v--; + dest[2*e+0] = v; + adj[u].push_back({2*e+0, v}); + dest[2*e+1] = u; + adj[v].push_back({2*e+1, u}); + } + + vector nxt(2*M), prv(2*M); + + // Build the DCEL + for (int i = 0; i < N; i++) { + sort(adj[i].begin(), adj[i].end(), [&](auto e1, auto e2) { + pt_t p1 = pts[e1.second] - pts[i], p2 = pts[e2.second] - pts[i]; + bool d1 = make_pair(p1.imag(), p1.real()) > make_pair(0,0); + bool d2 = make_pair(p2.imag(), p2.real()) > make_pair(0,0); + if (d1 != d2) return d1 < d2; + return int64_t(p1.real()) * int64_t(p2.imag()) - int64_t(p1.imag()) * int64_t(p2.real()) > 0; + }); + for (int z = 0; z < int(adj[i].size()); z++) { + int e1 = adj[i][z].first ^ 1; + int e2 = adj[i][(z+1)%int(adj[i].size())].first; + nxt[e1] = e2; + prv[e2] = e1; + } + } + + vector ccid(2*M, -1); + for (int e = 0; e < 2 * M; e++) { + if (ccid[e] != -1) continue; + for (int cur = e; ccid[cur] == -1; cur = nxt[cur]) { + ccid[cur] = e; + } + } + + vector is_bridge(M); + vector bridge_queue; bridge_queue.reserve(M); + vector is_alive(M, true); + + for (int e = 0; e < M; e++) { + if (ccid[2*e+0] == ccid[2*e+1]) { + is_bridge[e] = true; + bridge_queue.push_back(e); + } + } + + // Set a -> b + auto splice = [&](int a, int b) { + b = prv[b]; + if (ccid[a] == ccid[b]) { + // it's a cut; no need to change id's + } else { + // it's a join, let's fix the id's + for (int ca = nxt[a], cb = nxt[b]; true; ca = nxt[ca], cb = nxt[cb]) { + if (cb == b) { + break; + } else if (ca == a) { + swap(a, b); + break; + } + } + int nid = ccid[a]; + for (int cur = b; ccid[cur] != nid; cur = nxt[cur]) { + ccid[cur] = nid; + if (ccid[cur^1] == nid && !is_bridge[cur>>1]) { + is_bridge[cur>>1] = true; + bridge_queue.push_back(cur>>1); + } + } + } + swap(nxt[a], nxt[b]); + prv[nxt[a]] = a; + prv[nxt[b]] = b; + }; + + auto delete_edge = [&](int e) { + assert(is_alive[e]); + is_alive[e] = false; + splice(2*e+0, 2*e+1); + splice(2*e+1, 2*e+0); + }; + + int epoch = 0; + vector vis(2*M, epoch); + vector in_set(N, epoch); + array, 2> q; + q[0].reserve(2*M); + q[1].reserve(2*M); + + int num_matched = 0; + while (!bridge_queue.empty()) { + int e = bridge_queue.back(); bridge_queue.pop_back(); + if (!is_alive[e]) continue; + assert(ccid[2*e+0] == ccid[2*e+1]); + + int u = dest[2*e+1], v = dest[2*e+0]; + int ve = nxt[2*e+0]; + int ue = nxt[2*e+1]; + + // First, you split the current face and delete this edge + delete_edge(e); + + // Now, we check the 2 cc's for parity + bool should_cut = [&]() -> bool { + if (ve/2 == e || ue/2 == e) { + return true; + } + // otherwise, we have at least one edge in each side + epoch++; + q[0].clear(); q[0].push_back(ue); + vis[ue] = epoch; + q[1].clear(); q[1].push_back(ve); + vis[ve] = epoch; + array cnt{0,0}; + for (int z = 0; true; z++) { + for (int d = 0; d < 2; d++) { + if (z == int(q[d].size())) { + return cnt[d] & 1; + } + int cur = q[d][z]; + if (in_set[dest[cur]] != epoch) cnt[d]++, in_set[dest[cur]] = epoch; + if (vis[nxt[cur]] != epoch) { + vis[nxt[cur]] = epoch; + q[d].push_back(nxt[cur]); + } + if (vis[cur^1] != epoch) { + vis[cur^1] = epoch; + q[d].push_back(cur^1); + } + } + } + }(); + if (should_cut) { + num_matched ++; + + // cut everything else at these two vertices + for (auto [oe, _] : adj[u]) { + oe /= 2; + if (is_alive[oe]) delete_edge(oe); + } + for (auto [oe, _] : adj[v]) { + oe /= 2; + if (is_alive[oe]) delete_edge(oe); + } + } + } + + cout << int(num_matched * 2 == N) << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1442B b/code/ecnerwala/1442B new file mode 100644 index 0000000..e4c9146 --- /dev/null +++ b/code/ecnerwala/1442B @@ -0,0 +1,49 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int N, K; cin >> N >> K; + vector invA(N); + for (int i = 0; i < N; i++) { + int a; cin >> a; a--; + invA[a] = i; + } + vector remTime(N, -1); + for (int k = 0; k < K; k++) { + int b; cin >> b; b--; b = invA[b]; + remTime[b] = k; + } + + auto ans = [&]() -> std::optional { + int res = 0; + for (int i = 0; i < N; i++) { + if (remTime[i] == -1) continue; + bool a = (i-1 >= 0 && remTime[i-1] < remTime[i]); + bool b = (i+1 < N && remTime[i+1] < remTime[i]); + if (!a && !b) return std::nullopt; + if (a && b) res ++; + } + return res; + }(); + + if (ans) { + const int MOD = 998244353; + int v = 1; + for (int z = *ans; z--; ) { + v <<= 1; + v = (v >= MOD ? v - MOD : v); + } + cout << v << '\n'; + } else { + cout << 0 << '\n'; + } + + // we go backwards + } + + return 0; +} + diff --git a/code/ecnerwala/1442D b/code/ecnerwala/1442D new file mode 100644 index 0000000..9ac353f --- /dev/null +++ b/code/ecnerwala/1442D @@ -0,0 +1,72 @@ +#include + +namespace std { + +template +class y_combinator_result { + Fun fun_; +public: + template + explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} + + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; + +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} + +} // namespace std + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int N, K; cin >> N >> K; + int L = 0; + vector Atot(N); + vector> A(N); + for (int i = 0; i < N; i++) { + int t; cin >> t; L += t; + A[i].resize(t); + for (auto& a : A[i]) cin >> a, Atot[i] += a; + } + + int64_t ans = 0; + std::y_combinator([&](auto self, int lo, int hi, std::vector dp) -> void { + if (hi - lo == 1) { + int64_t pref = 0; + for (int v = 0; v <= K; v++) { + ans = max(ans, pref + dp[K-v]); + if (v == int(A[lo].size())) break; + pref += A[lo][v]; + } + return; + } + int md = lo + (hi-lo)/2; + { + vector dp2 = dp; + for (int i = lo; i < md; i++) { + for (int k = K, l = K - int(A[i].size()); l >= 0; k--, l--) { + dp2[k] = max(dp2[k], dp2[l] + Atot[i]); + } + } + self(md, hi, dp2); + } + { + for (int i = md; i < hi; i++) { + for (int k = K, l = K - int(A[i].size()); l >= 0; k--, l--) { + dp[k] = max(dp[k], dp[l] + Atot[i]); + } + } + self(lo, md, dp); + } + })(0,N,vector(K+1,0)); + cout << ans << '\n'; + + return 0; +} + diff --git a/code/ecnerwala/1442E b/code/ecnerwala/1442E new file mode 100644 index 0000000..c61cc47 --- /dev/null +++ b/code/ecnerwala/1442E @@ -0,0 +1,51 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int N; cin >> N; + vector C(N); for (int& c : C) { cin >> c; c--; if (c == -1) c = 2; } + vector> adj(N); + for (int e = 0; e < N-1; e++) { + int u, v; cin >> u >> v; u--, v--; + adj[u].push_back(v); + adj[v].push_back(u); + } + + int ans = N+1; + for (int z = 0; z < 2; z++) { + vector deg(N); + array, 3> queue; queue[0].reserve(N), queue[1].reserve(N), queue[2].reserve(N); + for (int i = 0; i < N; i++) { + deg[i] = int(adj[i].size()); + if (deg[i] <= 1) { + queue[C[i]].push_back(i); + } + } + + int t = 0; + int num_processed = 0; + for (; num_processed < N; t++) { + while (true) { + int d = 2; + if (queue[d].empty()) d = (t+z)&1; + if (queue[d].empty()) break; + int i = queue[d].back(); queue[d].pop_back(); + num_processed++; + for (int j : adj[i]) { + if (--deg[j] == 1) { + queue[C[j]].push_back(j); + } + } + } + } + ans = min(ans, t); + } + cout << ans << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1442F b/code/ecnerwala/1442F new file mode 100644 index 0000000..06e3744 --- /dev/null +++ b/code/ecnerwala/1442F @@ -0,0 +1,136 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int N, M, Q; cin >> N >> M >> Q; + vector> adj(N); + vector> radj(N); + vector outDeg(N); + for (int e = 0; e < M; e++) { + int u, v; cin >> u >> v; u--, v--; + adj[u].push_back(v); + radj[v].push_back(u); + outDeg[u]++; + } + int L = min(N, 20); + vector los; los.reserve(L); + for (int i = 0; i < N && int(los.size()) < L; i++) { + if (!outDeg[i]) { + los.push_back(i); + } + } + for (int z = 0; int(los.size()) < L; z++) { + int i = los[z]; + for (int j : radj[i]) { + if (!--outDeg[j]) { + los.push_back(j); + if (int(los.size()) == L) break; + } + } + } + assert(int(los.size()) == L); + vector lo_idx(N, -1); + for (int i = 0; i < L; i++) { + lo_idx[los[i]] = i; + } + + vector> ops; + for (int i = 0; i < L; i++) { + for (int j = 0; j < i; j++) { + ops.push_back({'+', los[i], los[j]}); + } + } + vector idx(1 << L, -1); + for (int i = 0; i < N; i++) { + if (lo_idx[i] != -1) continue; + ops.push_back({'+', i, i}); + + int cur_msk = 0; + for (int j : adj[i]) { + if (lo_idx[j] != -1) { + cur_msk ^= 1 << lo_idx[j]; + } + } + int new_msk = [&]() { + if (idx[cur_msk] == -1) { + return cur_msk; + } + for (int a = 0; a < L; a++) { + int cnd = cur_msk ^ (1 << a); + if (idx[cnd] == -1) { + return cnd; + } + } + for (int a = 0; a < L; a++) { + for (int b = a+1; b < L; b++) { + int cnd = cur_msk ^ (1 << a) ^ (1 << b); + if (idx[cnd] == -1) { + return cnd; + } + } + } + for (int a = 0; a < L; a++) { + for (int b = a+1; b < L; b++) { + for (int c = b+1; c < L; c++) { + int cnd = cur_msk ^ (1 << a) ^ (1 << b) ^ (1 << c); + if (idx[cnd] == -1) { + return cnd; + } + } + } + } + assert(false); + }(); + + assert(idx[new_msk] == -1); + idx[new_msk] = i; + for (int a = 0; a < L; a++) { + if ((cur_msk ^ new_msk) & (1 << a)) { + if (cur_msk & (1 << a)) { + ops.push_back({'-', i, los[a]}); + } else { + ops.push_back({'+', i, los[a]}); + } + } + } + } + + cout << ops.size() << '\n'; + for (auto [v, a, b] : ops) { + cout << v << ' ' << a+1 << ' ' << b+1 << '\n'; + } + cout << flush; + + while (Q--) { + int solve = [&]() -> int { + int cur_msk = 0; + for (int a = 0; a < L; a++) { + cout << '?' << ' ' << 1 << ' ' << los[a]+1 << '\n' << flush; + string resp; cin >> resp; + if (resp == "Slow") { + // wrong answer + exit(0); + } else if (resp == "Lose") { + return los[a]; + } else if (resp == "Win") { + cur_msk ^= 1 << a; + } else if (resp == "Draw") { + // do nothing + } else assert(false); + } + assert(idx[cur_msk] != -1); + return idx[cur_msk]; + }(); + cout << '!' << ' ' << solve+1 << '\n' << flush; + string resp; cin >> resp; + if (resp == "Correct") { + // good + } else if (resp == "Wrong") { + exit(0); + } else assert(false); + } + + return 0; +} + diff --git a/code/ecnerwala/1443A b/code/ecnerwala/1443A new file mode 100644 index 0000000..b054a10 --- /dev/null +++ b/code/ecnerwala/1443A @@ -0,0 +1,13 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int N; cin >> N; + for (int i = 1; i <= N; i++) cout << ((N+i)<<1) << '\n'; + } + + return 0; +} diff --git a/code/ecnerwala/1443B b/code/ecnerwala/1443B new file mode 100644 index 0000000..1f72d9a --- /dev/null +++ b/code/ecnerwala/1443B @@ -0,0 +1,25 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int A, B; cin >> A >> B; + int ans = 0; + int cnt0 = A+1; + string s; cin >> s; + for (char c : s) { + if (c == '0') { + cnt0++; + } else { + ans += min(A, B * cnt0); + cnt0 = 0; + } + } + cout << ans << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1443C b/code/ecnerwala/1443C new file mode 100644 index 0000000..592a901 --- /dev/null +++ b/code/ecnerwala/1443C @@ -0,0 +1,26 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; cin >> T; + while (T--) { + int N; cin >> N; + vector> A(N); + for (auto& a : A) cin >> a.first; + for (auto& a : A) cin >> a.second; + sort(A.begin(), A.end()); + + int suff = 0; + for (int i = N-1; true; i--) { + suff += A[i].second; + if (!i || suff >= A[i-1].first) { + cout << min(A[i].first, suff) << '\n'; + break; + } + } + } + + return 0; +} + diff --git a/code/ecnerwala/1443D b/code/ecnerwala/1443D new file mode 100644 index 0000000..d61e699 --- /dev/null +++ b/code/ecnerwala/1443D @@ -0,0 +1,24 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + + int T; cin >> T; + while (T--) { + int N; cin >> N; + const int64_t INF = 1e18; + int64_t a = INF, b = 0; + for (int i = 0; i < N; i++) { + int64_t v; cin >> v; + int64_t na = min(a, v-b); + int64_t nb = max(v-a, b); + assert(na + nb == v); + a = na, b = nb; + } + cout << (a >= 0 ? "YES" : "NO") << '\n'; + } + + return 0; +} + diff --git a/code/ecnerwala/1443E b/code/ecnerwala/1443E new file mode 100644 index 0000000..357b1ba --- /dev/null +++ b/code/ecnerwala/1443E @@ -0,0 +1,53 @@ +#include + +int main() { + using namespace std; + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int N, Q; cin >> N >> Q; + vector A(N); iota(A.begin(), A.end(), 1); + vector pref(N+1); + for (int i = 0; i < N; i++) pref[i+1] = pref[i] + A[i]; + + while (Q--) { + int op; cin >> op; + if (op == 1) { + int l, r; cin >> l >> r; l--; + cout << pref[r] - pref[l] << '\n'; + } else if (op == 2) { + int x; cin >> x; + int first_changed = N; + while (x > 0) { + int fact = 1; + int len = 1; + while (len+1 <= N && fact * (len+1) - 1 <= x && A[N-len-1] < A[N-len]) { + fact *= ++len; + } + if (len == 1) { + // we just run next_permutation + x--; + + int i = N-1; + while (A[i-1] > A[i]) i--; + assert(A[i-1] < A[i]); + int j = N-1; + while (A[i-1] > A[j]) j--; + assert(j >= i); + assert(A[i-1] < A[j]); + swap(A[i-1], A[j]); + reverse(A.begin()+i, A.end()); + first_changed = min(first_changed, i-1); + } else { + assert(fact-1 > 0); + x -= fact-1; + + reverse(A.end()-len, A.end()); + first_changed = min(first_changed, N-len); + } + } + for (int i = first_changed; i < N; i++) pref[i+1] = pref[i] + A[i]; + } else assert(false); + } + + return 0; +} + diff --git a/code/ksun48/1187B b/code/ksun48/1187B new file mode 100644 index 0000000..1e1cf73 --- /dev/null +++ b/code/ksun48/1187B @@ -0,0 +1,30 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + string s; + cin >> s; + vector > where(26, {0}); + vector freq(26); + for(int i = 0; i < n; i++){ + int c = s[i] - 'a'; + freq[c]++; + where[c].push_back(i+1); + } + int m; + cin >> m; + for(int i = 0; i < m; i++){ + string a; + cin >> a; + vector cfreq(26, 0); + for(char c : a) cfreq[c-'a']++; + int res = 0; + for(int j = 0; j < 26; j++){ + res = max(res, where[j][cfreq[j]]); + } + cout << res << '\n'; + } +} diff --git a/code/ksun48/1296C b/code/ksun48/1296C new file mode 100644 index 0000000..6340cf9 --- /dev/null +++ b/code/ksun48/1296C @@ -0,0 +1,42 @@ +#include +using namespace std; + +void solve(){ + int r; + cin >> r; + string s; + cin >> s; + string g = "LRUD"; + vector dx = {1, -1, 0, 0}; + vector dy = {0, 0, 1, -1}; + map, int> last; + pair cur = {0, 0}; + last[cur] = 0; + pair best = {-1, -1}; + for(int i = 0; i < (int)s.size(); i++){ + for(int j = 0; j < 4; j++){ + if(s[i] == g[j]){ + cur.first += dx[j]; + cur.second += dy[j]; + } + } + if(last.count(cur)){ + if(best.first == -1 || best.second - best.first > i - last[cur]){ + best = {last[cur], i}; + } + } + last[cur] = i+1; + } + if(best.first == -1){ + cout << -1 << '\n'; + } else { + cout << best.first + 1 << ' ' << best.second + 1 << '\n'; + } +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1427A b/code/ksun48/1427A new file mode 100644 index 0000000..0e75bfd --- /dev/null +++ b/code/ksun48/1427A @@ -0,0 +1,31 @@ +#include +using namespace std; + +void solve(){ + int n; + cin >> n; + vector a(n); + int s = 0; + for(int i = 0; i < n; i++){ + cin >> a[i]; + s += a[i]; + } + if(s == 0){ + cout << "NO" << '\n'; + } else { + cout << "YES" << '\n'; + sort(a.begin(), a.end()); + if(s > 0){ + reverse(a.begin(), a.end()); + } + for(int x : a) cout << x << ' '; + cout << '\n'; + } +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1427B b/code/ksun48/1427B new file mode 100644 index 0000000..8b1b6b3 --- /dev/null +++ b/code/ksun48/1427B @@ -0,0 +1,49 @@ +#include +using namespace std; + +void solve(){ + int n, k; + cin >> n >> k; + string s; + cin >> s; + int nl = 0; + int score = 0; + for(int i = 0; i < n; i++){ + if(s[i] == 'L') nl++; + if(i > 0 && s[i] == 'W' && s[i-1] == 'W') score++; + } + score += n - nl; + k = min(k, nl); + score += 2*k; + if(nl == n){ + if(k > 0) score -= 1; + } else { + vector group; + int csz = -1e8; + for(int i = 0; i < n; i++){ + if(s[i] == 'W'){ + if(csz > 0){ + group.push_back(csz); + } + csz = 0; + } else { + csz++; + } + } + sort(group.begin(), group.end()); + for(int a : group){ + if(k >= a){ + k -= a; + score += 1; + } + } + } + cout << score << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1427C b/code/ksun48/1427C new file mode 100644 index 0000000..f4b9ece --- /dev/null +++ b/code/ksun48/1427C @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int r, n; + cin >> r >> n; + n++; + vector t(n, 0), x(n, 0), y(n, 0); + vector dp(n, -1e8); + dp[0] = 0; + for(int i = 1; i < n; i++){ + cin >> t[i] >> x[i] >> y[i]; + x[i]--; y[i]--; + } + vector pdp(n, -1e8); + pdp[0] = 0; + const int F = 1100; + for(int i = 1; i < n; i++){ + for(int g = i-1; g >= 0 && g >= i-F; g--){ + if(abs(x[i] - x[g]) + abs(y[i] - y[g]) <= t[i] - t[g]){ + dp[i] = max(dp[i], dp[g] + 1); + } + } + if(i >= F) dp[i] = max(dp[i], pdp[i-F] + 1); + pdp[i] = max(dp[i], pdp[i-1]); + } + cout << pdp[n-1] << '\n'; +} diff --git a/code/ksun48/1427D b/code/ksun48/1427D new file mode 100644 index 0000000..ba0ee06 --- /dev/null +++ b/code/ksun48/1427D @@ -0,0 +1,71 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + vector a(n); + for(int i = 0; i < n; i++) cin >> a[i]; + vector > ops; + auto do_op = [&](vector r){ + vector s; + for(int x : r) if(x != 0) s.push_back(x); + ops.push_back(s); + int cur = 0; + for(int x : s){ + reverse(a.begin() + cur, a.begin() + cur + x); + cur += x; + } + assert(cur == n); + reverse(a.begin(), a.end()); + }; + for(int j = 2; j <= n; j++){ + int p1 = 0; + while(a[p1] != 1) p1++; + int dir = 1; + if(p1 > 0 && a[p1-1] == 2) dir = -1; + + int pj = 0; + while(a[pj] != j) pj++; + vector op; + if(dir == 1){ + if(pj > p1){ + // 12345 pj + for(int f = 0; f < p1 + j-1; f++) op.push_back(1); + op.push_back((pj+1) - (p1+j-1)); + op.push_back(n - (pj+1)); + } else { + // pj 12345 + for(int f = 0; f < pj+1; f++) op.push_back(1); + op.push_back((p1+j-1) - (pj+1)); + op.push_back(n - (p1+j-1)); + } + } else { + // 54321 pj + if(pj > p1){ + op.push_back((p1-j+2)); + op.push_back(pj - (p1-j+2)); + for(int f = 0; f < n - pj; f++) op.push_back(1); + } else { + // pj 54321 + op.push_back(pj); + op.push_back((p1-j+2)-pj); + for(int f = 0; f < n - (p1-j+2); f++) op.push_back(1); + } + } + do_op(op); + } + if(a[0] != 1){ + do_op(vector(n, 1)); + } + for(int i = 0; i < n; i++) assert(a[i] == i+1); + cout << ops.size() << '\n'; + for(vector s : ops){ + cout << s.size(); + for(int b : s){ + cout << ' ' << b; + } + cout << '\n'; + } +} diff --git a/code/ksun48/1427E b/code/ksun48/1427E new file mode 100644 index 0000000..ea8edc8 --- /dev/null +++ b/code/ksun48/1427E @@ -0,0 +1,54 @@ +#include +using namespace std; + +using ll = long long; + +vector, char> > ops; + +void op(ll a, char c, ll b){ + ops.push_back({{a, b}, c}); +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + ll x; + cin >> x; + for(ll j = 0; j < 30; j++){ + op(x << j, '+', x << j); + } + ll cx = x; + for(ll j = 1; j < 25; j++){ + if(cx & (1ll << j)){ + op(cx, '^', x << j); + cx ^= (x << j); + } + } + assert((x & cx) == 1); + // x is big + op(x, '+', cx); + op(x+cx, '^', cx); + ll y = (x + cx) ^ cx; + assert(y == x + 2); + + op(y, '+', cx); + op(y+cx, '^', cx); + ll z = (y + cx) ^ cx; + assert(z == y + 2); + op(x, '^', y); + op(y, '^', z); + assert((x ^ y) == 2 || (y ^ z) == 2); + for(ll j = 1; j < 30; j++){ + op(1ll << j, '+', 1ll << j); + } + for(ll j = 1; j < 30; j++){ + if(x & (1ll << j)){ + op(x, '^', 1ll << j); + x ^= (1ll << j); + } + } + assert(x == 1); + cout << ops.size() << '\n'; + for(auto e : ops){ + cout << e.first.first << " " << (char)(e.second) << " " << e.first.second << '\n'; + } +} diff --git a/code/ksun48/1427F b/code/ksun48/1427F new file mode 100644 index 0000000..007cd4b --- /dev/null +++ b/code/ksun48/1427F @@ -0,0 +1,104 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + n *= 6; + vector color(n, 1); + for(int i = 0; i < n; i++){ + int a; + cin >> a; + a--; + color[a] = 0; + } + const int FAIL = -1e8; + vector > dp(n+1, vector(n+1, 0)); + vector > where(n+1, vector(n+1, 0)); + for(int l = 0; l <= n; l += 3){ + for(int j = 0; j + l <= n; j++){ + if(l == 0){ + dp[j][j+l] = FAIL; + } else { + for(int k = j+1; k < j+l; k++){ + if(!dp[j][j+l] && dp[j][k] && dp[k][j+l]){ + dp[j][j+l] = k; + } + } + for(int k = j+1; k < j+l-1; k++){ + if(!dp[j][j+l] && color[j] == color[k] && color[k] == color[j+l-1] && dp[j+1][k] && dp[k+1][j+l-1]){ + dp[j][j+l] = -k; + } + } + } + } + } + pair works = {-1, -1}; + for(int a = 0; a <= n; a++){ + for(int b = 0; b <= n; b++){ + if(color[a] == 1 && dp[0][a] && dp[a][b] && dp[b][n]){ + works = {a, b}; + } + } + } + assert(works.first >= 0); + vector > segs; + segs.push_back({0, works.first}); + segs.push_back({works.first, works.second}); + segs.push_back({works.second, n}); + + vector > trips; + int turn = 1; + while(!segs.empty()){ + for(int i = 0; i < (int)segs.size(); i++){ + if(segs[i].first == segs[i].second){ + segs.erase(segs.begin() + i); + goto here; + } + } + for(int i = 0; i < (int)segs.size(); i++){ + int r = segs[i].first; + int s = segs[i].second; + assert(dp[r][s]); + if(dp[r][s] > 0){ + int mid = dp[r][s]; + segs.erase(segs.begin() + i); + segs.push_back({r, mid}); + segs.push_back({mid, s}); + goto here; + } + } + for(int i = 0; i < (int)segs.size(); i++){ + int r = segs[i].first; + int s = segs[i].second; + if(dp[r][s] < 0 && s-r > 3 && color[r] == turn){ + int mid = -dp[r][s]; + trips.push_back({r, mid, s-1}); + segs.erase(segs.begin() + i); + segs.push_back({r+1, mid}); + segs.push_back({mid+1, s-1}); + turn ^= 1; + goto here; + } + } + for(int i = 0; i < (int)segs.size(); i++){ + int r = segs[i].first; + int s = segs[i].second; + if(dp[r][s] < 0 && color[r] == turn){ + int mid = -dp[r][s]; + trips.push_back({r, mid, s-1}); + segs.erase(segs.begin() + i); + segs.push_back({r+1, mid}); + segs.push_back({mid+1, s-1}); + turn ^= 1; + goto here; + } + } + here:; + } + reverse(trips.begin(), trips.end()); + for(vector a : trips){ + cout << a[0] + 1 << ' ' << a[1] + 1 << ' ' << a[2] + 1 << '\n'; + } +} diff --git a/code/ksun48/1427G b/code/ksun48/1427G new file mode 100644 index 0000000..270c2b5 --- /dev/null +++ b/code/ksun48/1427G @@ -0,0 +1,223 @@ +#include +using namespace std; + +namespace std { + +template +class y_combinator_result { + Fun fun_; +public: + template + explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} + + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; + +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} + +} // namespace std + +#define rep(i, a, b) for(int i = a; i < (b); ++i) +#define trav(a, x) for(auto& a : x) +#define all(x) x.begin(), x.end() +#define sz(x) (int)(x).size() +typedef long long ll; +typedef pair pii; +typedef vector vi; + +// struct Dinic { +// struct Edge { +// int to, rev; +// ll c, oc; +// ll flow() { return max(oc - c, 0LL); } // if you need flows +// }; +// vi lvl, ptr, q; +// vector> adj; +// Dinic(int n) : lvl(n), ptr(n), q(n), adj(n) {} +// void addEdge(int a, int b, ll c, int rcap = 0) { +// adj[a].push_back({b, sz(adj[b]), c, c}); +// adj[b].push_back({a, sz(adj[a]) - 1, rcap, rcap}); +// } +// ll dfs(int v, int t, ll f) { +// if (v == t || !f) return f; +// for (int& i = ptr[v]; i < sz(adj[v]); i++) { +// Edge& e = adj[v][i]; +// if (lvl[e.to] == lvl[v] + 1) +// if (ll p = dfs(e.to, t, min(f, e.c))) { +// e.c -= p, adj[e.to][e.rev].c += p; +// return p; +// } +// } +// return 0; +// } +// ll calc(int s, int t) { +// ll flow = 0; q[0] = s; +// rep(L,0,31) do { // 'int L=30' maybe faster for random data +// lvl = ptr = vi(sz(q)); +// int qi = 0, qe = lvl[s] = 1; +// while (qi < qe && !lvl[t]) { +// int v = q[qi++]; +// trav(e, adj[v]) +// if (!lvl[e.to] && e.c >> (30 - L)) +// q[qe++] = e.to, lvl[e.to] = lvl[v] + 1; +// } +// while (ll p = dfs(s, t, LLONG_MAX)) flow += p; +// } while (lvl[t]); +// return flow; +// } +// bool leftOfMinCut(int a) { return lvl[a] != 0; } +// }; + +struct PushRelabel { + struct Edge { + int dest, back; + ll f, c; + }; + vector> g; + vector ec; + vector cur; + vector hs; vi H; + PushRelabel(int n) : g(n), ec(n), cur(n), hs(2*n), H(n) {} + + void addEdge(int s, int t, ll cap, ll rcap=0) { + if (s == t) return; + g[s].push_back({t, sz(g[t]), 0, cap}); + g[t].push_back({s, sz(g[s])-1, 0, rcap}); + } + + void addFlow(Edge& e, ll f) { + Edge &back = g[e.dest][e.back]; + if (!ec[e.dest] && f) hs[H[e.dest]].push_back(e.dest); + e.f += f; e.c -= f; ec[e.dest] += f; + back.f -= f; back.c += f; ec[back.dest] -= f; + } + ll calc(int s, int t) { + int v = sz(g); H[s] = v; ec[t] = 1; + vi co(2*v); co[0] = v-1; + rep(i,0,v) cur[i] = g[i].data(); + trav(e, g[s]) addFlow(e, e.c); + + for (int hi = 0;;) { + while (hs[hi].empty()) if (!hi--) return -ec[s]; + int u = hs[hi].back(); hs[hi].pop_back(); + while (ec[u] > 0) // discharge u + if (cur[u] == g[u].data() + sz(g[u])) { + H[u] = 1e9; + trav(e, g[u]) if (e.c && H[u] > H[e.dest]+1) + H[u] = H[e.dest]+1, cur[u] = &e; + if (++co[H[u]], !--co[hi] && hi < v) + rep(i,0,v) if (hi < H[i] && H[i] < v) + --co[H[i]], H[i] = v + 1; + hi = H[u]; + } else if (cur[u]->c && H[u] == H[cur[u]->dest]+1) + addFlow(*cur[u], min(ec[u], cur[u]->c)); + else ++cur[u]; + } + } + bool leftOfMinCut(int a) { return H[a] >= sz(g); } +}; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + vector > A(n, vector(n)); + vector vals; + vector > unknown; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + cin >> A[i][j]; + if(A[i][j] > 0) vals.push_back(A[i][j]); + if(A[i][j] == 0) unknown.push_back({i, j}); + } + } + vector > ans = A; + vals.push_back(1); + sort(vals.begin(), vals.end()); + vals.resize(unique(vals.begin(), vals.end()) - vals.begin()); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + if(ans[i][j] == 0) ans[i][j] = 1; + } + } + y_combinator( + [&](auto self, int l, int r, vector > cur) -> void { + if(l == r){ + return; + } + set > curset(cur.begin(), cur.end()); + int m = (l + r) / 2; + set > care; + auto add = [&](int a, int b){ + if(a >= 0 && b >= 0 && a < n && b < n && A[a][b] != -1){ + care.insert({a, b}); + } + }; + for(pair p : cur){ + add(p.first, p.second); + add(p.first+1, p.second); + add(p.first-1, p.second); + add(p.first, p.second+1); + add(p.first, p.second-1); + } + map, int> idx; + for(pair v : care){ + int k = (int)idx.size(); + idx[v] = k; + } + int k = (int)idx.size(); + PushRelabel mf(k + 2); + for(pair p : care){ + if(curset.count(p)){ + // unknown + assert(ans[p.first][p.second] == vals[l]); + } else if(ans[p.first][p.second] <= vals[m]){ + mf.addEdge(k, idx[p], 1e6); + } else if(ans[p.first][p.second] > vals[m]){ + mf.addEdge(idx[p], k+1, 1e6); + } else { + assert(false); + } + } + for(pair p : care){ + if(care.count({p.first + 1, p.second})){ + mf.addEdge(idx[p], idx[{p.first + 1, p.second}], 1); + mf.addEdge(idx[{p.first + 1, p.second}], idx[p], 1); + } + if(care.count({p.first, p.second + 1})){ + mf.addEdge(idx[p], idx[{p.first, p.second + 1}], 1); + mf.addEdge(idx[{p.first, p.second + 1}], idx[p], 1); + } + } + ll flow = mf.calc(k, k+1); + vector > cur1, cur2; + for(pair p : cur){ + int i = idx[p]; + if(mf.leftOfMinCut(i)){ + cur1.push_back(p); + ans[p.first][p.second] = vals[l]; + } else { + cur2.push_back(p); + ans[p.first][p.second] = vals[m+1]; + } + } + self(l, m, cur1); + self(m+1, r, cur2); + } + )(0, (int)vals.size() - 1, unknown); + ll tot = 0; + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + assert(ans[i][j] != 0); + if(j > 0 && ans[i][j] > 0 && ans[i][j-1] > 0) tot += abs(ans[i][j] - ans[i][j-1]); + if(i > 0 && ans[i][j] > 0 && ans[i-1][j] > 0) tot += abs(ans[i][j] - ans[i-1][j]); + } + } + cout << tot << '\n'; +} diff --git a/code/ksun48/1428A b/code/ksun48/1428A new file mode 100644 index 0000000..967e669 --- /dev/null +++ b/code/ksun48/1428A @@ -0,0 +1,17 @@ +#include +using namespace std; + +void solve(){ + int x1, y1, x2, y2; + cin >> x1 >> y1 >> x2 >> y2; + int ans = abs(x1 - x2) + abs(y1 - y2); + if(x1 != x2 && y1 != y2) ans += 2; + cout << ans << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1428B b/code/ksun48/1428B new file mode 100644 index 0000000..4a356bd --- /dev/null +++ b/code/ksun48/1428B @@ -0,0 +1,31 @@ +#include +using namespace std; + +void solve(){ + int n; + cin >> n; + string s; + cin >> s; + bool allleft = true; + bool allright = true; + for(int i = 0; i < n; i++){ + if(s[i] == '<') allleft = false; + if(s[i] == '>') allright = false; + } + if(allleft || allright){ + cout << n << '\n'; + } else { + int cnt = 0; + for(int i = 0; i < n; i++){ + if(s[i] == '-' || s[(i+1)%n] == '-') cnt++; + } + cout << cnt << '\n'; + } +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1428C b/code/ksun48/1428C new file mode 100644 index 0000000..ac0b1f1 --- /dev/null +++ b/code/ksun48/1428C @@ -0,0 +1,23 @@ +#include +using namespace std; + +void solve(){ + string s; + cin >> s; + string cur; + for(char c : s){ + if(c == 'B' && !cur.empty()){ + cur.pop_back(); + } else { + cur += c; + } + } + cout << cur.size() << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1428D b/code/ksun48/1428D new file mode 100644 index 0000000..e815d7f --- /dev/null +++ b/code/ksun48/1428D @@ -0,0 +1,79 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + vector a(n); + vector > locs; + for(int i = 0; i < n; i++) cin >> a[i]; + int cur = 0; + int last3 = -1; + for(int i = 0; i < n; i++){ + if(a[i] == 3){ + if(last3 != -1){ + locs.push_back({cur, last3}); + locs.push_back({cur, i}); + cur++; + } + last3 = i; + a[i] = 0; + } + } + if(last3 != -1){ + int done = 0; + for(int i = last3+1; i < n; i++){ + if(a[i] == 2){ + locs.push_back({cur, last3}); + locs.push_back({cur, i}); + cur++; + done = 1; + break; + } + } + if(!done){ + for(int i = last3+1; i < n; i++){ + if(a[i] == 1){ + locs.push_back({cur, last3}); + locs.push_back({cur, i}); + cur++; + locs.push_back({cur, i}); + cur++; + done = 1; + a[i] = 0; + break; + } + } + } + if(!done){ + cout << -1 << '\n'; + return 0; + } + } + int p1 = 0; + for(int i = 0; i < n; i++){ + if(a[i] == 2){ + while(p1 < n && (p1 < i || a[p1] != 1)) p1++; + if(p1 == n){ + cout << -1 << '\n'; + return 0; + } + locs.push_back({cur, i}); + locs.push_back({cur, p1}); + cur++; + a[i] = a[p1] = 0; + } + } + for(int i = 0; i < n; i++){ + if(a[i] == 1){ + locs.push_back({cur, i}); + cur++; + a[i] = 0; + } + } + cout << locs.size() << '\n'; + for(pair p : locs){ + cout << p.first + 1 << ' ' << p.second + 1 << '\n'; + } +} diff --git a/code/ksun48/1428E b/code/ksun48/1428E new file mode 100644 index 0000000..3eb6e8a --- /dev/null +++ b/code/ksun48/1428E @@ -0,0 +1,39 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + ll n, k; + cin >> n >> k; + vector a(n); + for(ll& x : a) cin >> x; + ll csum = n; + ll cans = 0; + for(ll x : a) cans += x * x; + + vector cval(n, 1); + auto cost = [&](ll i, ll v){ + ll r = a[i] / v; + return v * r * r + (a[i] % v) * (2 * r + 1); + }; + set > best_cost; + for(ll i = 0; i < n; i++){ + if(cval[i] < a[i]){ + best_cost.insert({cost(i, cval[i]+1) - cost(i, cval[i]), i}); + } + } + while(csum < k){ + pair v = *best_cost.begin(); + best_cost.erase(best_cost.begin()); + ll i = v.second; + cans -= cost(i, cval[i]); + cval[i] += 1; + cans += cost(i, cval[i]); + csum++; + if(cval[i] < a[i]){ + best_cost.insert({cost(i, cval[i]+1) - cost(i, cval[i]), i}); + } + } + cout << cans << '\n'; +} diff --git a/code/ksun48/1428F b/code/ksun48/1428F new file mode 100644 index 0000000..1ab9090 --- /dev/null +++ b/code/ksun48/1428F @@ -0,0 +1,37 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + ll n; + string s; + cin >> n >> s; + vector > segs; + ll total = 0; + ll csum = 0; + ll cur1 = 0; + for(ll i = 0; i < n; i++){ + if(s[i] == '0'){ + for(ll f = cur1; f >= 1; f--) segs.push_back({1, f}); + cur1 = 0; + segs.push_back({1, 0}); + total += csum; + } else if(s[i] == '1'){ + cur1++; + ll ccnt = 0; + while(!segs.empty() && segs.back().second < cur1){ + csum -= segs.back().first * segs.back().second; + ccnt += segs.back().first; + segs.pop_back(); + } + if(ccnt > 0){ + csum += ccnt * cur1; + segs.push_back({ccnt, cur1}); + } + csum += cur1; + total += csum; + } else assert(false); + } + cout << total << '\n'; +} diff --git a/code/ksun48/1428G1 b/code/ksun48/1428G1 new file mode 100644 index 0000000..5750d9a --- /dev/null +++ b/code/ksun48/1428G1 @@ -0,0 +1,53 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + int k; + cin >> k; + const int R = 1e6; + vector dp(R, -1e16); + dp[0] = 0; + + const int S = 6; + vector F(S); + for(ll& x : F) cin >> x; + int p10 = 1; + for(int i = 0; i < S; i++){ + vector newdp(R, -1e16); + for(int j = 0; j < R; j++){ + for(int x = 9*k-9; x <= j / p10 && x <= 9*k; x++){ + int d = x / 3; + if(x % 3 != 0) d = 3 * (k-1); + newdp[j] = max(newdp[j], dp[j - x*p10] + F[i] * d); + } + } + + int B = 9*k-9; + for(int res10 = 0; res10 < p10; res10++){ + for(int res3 = 0; res3 < 3; res3++){ + multiset cvals; + for(int j = res10; j < R; j += p10){ + int id = j / p10; + if(id % 3 == res3){ + cvals.insert(dp[j] - F[i] * (id / 3)); + } + if(!cvals.empty()) newdp[j] = max(newdp[j], *prev(cvals.end()) + F[i] * ((id - res3) / 3)); + if(id >= B && (id - B) % 3 == res3){ + cvals.erase(cvals.find(dp[j - p10 * B] - F[i] * ((id - B) / 3))); + } + } + } + } + dp = newdp; + p10 *= 10; + } + int Q; + cin >> Q; + while(Q--){ + int a; + cin >> a; + cout << dp[a] << '\n'; + } +} diff --git a/code/ksun48/1428G2 b/code/ksun48/1428G2 new file mode 100644 index 0000000..5750d9a --- /dev/null +++ b/code/ksun48/1428G2 @@ -0,0 +1,53 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + int k; + cin >> k; + const int R = 1e6; + vector dp(R, -1e16); + dp[0] = 0; + + const int S = 6; + vector F(S); + for(ll& x : F) cin >> x; + int p10 = 1; + for(int i = 0; i < S; i++){ + vector newdp(R, -1e16); + for(int j = 0; j < R; j++){ + for(int x = 9*k-9; x <= j / p10 && x <= 9*k; x++){ + int d = x / 3; + if(x % 3 != 0) d = 3 * (k-1); + newdp[j] = max(newdp[j], dp[j - x*p10] + F[i] * d); + } + } + + int B = 9*k-9; + for(int res10 = 0; res10 < p10; res10++){ + for(int res3 = 0; res3 < 3; res3++){ + multiset cvals; + for(int j = res10; j < R; j += p10){ + int id = j / p10; + if(id % 3 == res3){ + cvals.insert(dp[j] - F[i] * (id / 3)); + } + if(!cvals.empty()) newdp[j] = max(newdp[j], *prev(cvals.end()) + F[i] * ((id - res3) / 3)); + if(id >= B && (id - B) % 3 == res3){ + cvals.erase(cvals.find(dp[j - p10 * B] - F[i] * ((id - B) / 3))); + } + } + } + } + dp = newdp; + p10 *= 10; + } + int Q; + cin >> Q; + while(Q--){ + int a; + cin >> a; + cout << dp[a] << '\n'; + } +} diff --git a/code/ksun48/1434A b/code/ksun48/1434A new file mode 100644 index 0000000..830737a --- /dev/null +++ b/code/ksun48/1434A @@ -0,0 +1,29 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + vector a(6); + for(ll& x : a) cin >> x; + int n; + cin >> n; + vector b(n); + for(ll& x : b) cin >> x; + sort(a.rbegin(), a.rend()); + set > cur; + for(int i = 0; i < n; i++){ + cur.insert({b[i] - a[0], i}); + } + vector which(n, 0); + ll ans = 1e18; + while(true){ + ans = min(ans, (*cur.rbegin()).first - (*cur.begin()).first); + int f = (*cur.begin()).second; + cur.erase(cur.begin()); + which[f]++; + if(which[f] == 6) break; + cur.insert({b[f] - a[which[f]], f}); + } + cout << ans << '\n'; +} diff --git a/code/ksun48/1434B b/code/ksun48/1434B new file mode 100644 index 0000000..ec11323 --- /dev/null +++ b/code/ksun48/1434B @@ -0,0 +1,48 @@ +#include +using namespace std; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n; + cin >> n; + n *= 2; + vector f(n); + vector a(n, 0); + for(int i = 0; i < n; i++){ + cin >> f[i]; + if(f[i] == '-') cin >> a[i]; + } + vector cur; + for(int i = 0; i < n; i++){ + if(f[i] == '+'){ + cur.push_back(i); + } else if(f[i] == '-'){ + if(cur.empty()){ + cout << "NO" << '\n'; + exit(0); + } + a[cur.back()] = a[i]; + cur.pop_back(); + } + } + set z; + for(int i = 0; i < n; i++){ + if(f[i] == '+'){ + z.insert(a[i]); + } else if(f[i] == '-'){ + if(*z.begin() != a[i]){ + cout << "NO" << '\n'; + exit(0); + } else { + z.erase(z.begin()); + } + } + } + cout << "YES" << '\n'; + for(int i = 0; i < n; i++){ + if(f[i] == '+'){ + cout << a[i] << ' '; + } + } + cout << '\n'; +} diff --git a/code/ksun48/1434C b/code/ksun48/1434C new file mode 100644 index 0000000..6687ec8 --- /dev/null +++ b/code/ksun48/1434C @@ -0,0 +1,22 @@ +#include +using namespace std; + +void solve(){ + using ll = long long; + ll a, b, c, d; + cin >> a >> b >> c >> d; + if(a > b * c){ + cout << -1 << '\n'; + return; + } + ll k = a / b / d; + ll ans = (k+1) * a - k * (k+1) / 2 * b * d; + cout << ans << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1434E b/code/ksun48/1434E new file mode 100644 index 0000000..60ba383 --- /dev/null +++ b/code/ksun48/1434E @@ -0,0 +1,83 @@ +#include +using namespace std; + +int mex(set r){ + int z = 0; + while(r.count(z)) z++; + return z; +} + +const int M = 500; + +struct UF { + int n; + vector par; + UF(int _n) : n(_n) { + for(int i = 0; i < n; i++) par.push_back(i); + } + int find(int a){ + if(a != par[a]) par[a] = find(par[a]); + return par[a]; + } + bool join(int a, int b){ + a = find(a); + b = find(b); + par[a] = b; + return (a != b); + } +}; + +int solve(vector a){ + int n = (int)a.size(); + + vector old_where(n, 0); + vector curk(n, n-1); + set st; + int ndone = 0; + for(int v = 0; ; v++){ + vector first_occ(n, -1); + UF uf(n+1); + for(int j = n-1; j >= 0; j--){ + curk[j] = min(curk[j], first_occ[j]); + if(old_where[j] == j+1) continue; + int where; + if(curk[j] == -1){ + where = j+1; + } else { + int look = 2 * a[j] - a[curk[j]]; + where = lower_bound(a.begin(), a.end(), look+1) - a.begin(); + } + while(true){ + int r = uf.find(old_where[j]); + if(r >= where) break; + uf.join(r, r+1); + first_occ[r] = j; + } + if(where == j+1){ + ndone++; + st.insert(v); + } + old_where[j] = where; + } + if(ndone == n) break; + if((int)st.size() == n) break; + } + return mex(st); +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int ans = 0; + int T; + cin >> T; + while(T--){ + int n; + cin >> n; + vector a(n); + for(int& x : a) cin >> x; + // for(int i = 0; i < n; i++) a[i] = i; + int res = solve(a); + ans ^= res; + } + cout << (ans == 0 ? "NO" : "YES") << '\n'; +} diff --git a/code/ksun48/1442A b/code/ksun48/1442A new file mode 100644 index 0000000..32d6f00 --- /dev/null +++ b/code/ksun48/1442A @@ -0,0 +1,28 @@ +#include +using namespace std; + +void solve(){ + int n; + cin >> n; + vector a(n); + for(int& x : a) cin >> x; + int pa = 1e8; + int pb = 0; + for(int i = 0; i < n; i++){ + int na = min(a[i] - pb, pa); + if(na < 0){ + cout << "NO" << '\n'; + return; + } + pa = na; + pb = a[i] - pa; + } + cout << "YES" << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1442B b/code/ksun48/1442B new file mode 100644 index 0000000..87a51be --- /dev/null +++ b/code/ksun48/1442B @@ -0,0 +1,137 @@ +#include +using namespace std; + +template struct modnum { + static constexpr int MOD = MOD_; + static_assert(MOD_ > 0, "MOD must be positive"); + +private: + using ll = long long; + + int v; + + static int minv(int a, int m) { + a %= m; + assert(a); + return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); + } + +public: + + modnum() : v(0) {} + modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } + explicit operator int() const { return v; } + friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } + friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } + + friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } + friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } + + modnum inv() const { + modnum res; + res.v = minv(v, MOD); + return res; + } + friend modnum inv(const modnum& m) { return m.inv(); } + modnum neg() const { + modnum res; + res.v = v ? MOD-v : 0; + return res; + } + friend modnum neg(const modnum& m) { return m.neg(); } + + modnum operator- () const { + return neg(); + } + modnum operator+ () const { + return modnum(*this); + } + + modnum& operator ++ () { + v ++; + if (v == MOD) v = 0; + return *this; + } + modnum& operator -- () { + if (v == 0) v = MOD; + v --; + return *this; + } + modnum& operator += (const modnum& o) { + v += o.v; + if (v >= MOD) v -= MOD; + return *this; + } + modnum& operator -= (const modnum& o) { + v -= o.v; + if (v < 0) v += MOD; + return *this; + } + modnum& operator *= (const modnum& o) { + v = int(ll(v) * ll(o.v) % MOD); + return *this; + } + modnum& operator /= (const modnum& o) { + return *this *= o.inv(); + } + + friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } + friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } + friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } + friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } + friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } + friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } +}; + +template T pow(T a, long long b) { + assert(b >= 0); + T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; +} + +using num = modnum<998244353>; + +void solve(){ + int n, k; + cin >> n >> k; + vector a(n); + vector b_used(n, 0); + vector b(k); + for(int& x : a){ + cin >> x; + x--; + } + for(int& x : b){ + cin >> x; + x--; + } + vector a_where(n); + for(int i = 0; i < n; i++){ + a_where[a[i]] = i; + } + for(int x : b) b_used[a_where[x]] = 1; + set exists; + for(int i = 0; i < n; i++) exists.insert(i); + exists.insert(-1); + exists.insert(n); + num ans = 1; + for(int x : b){ + int idx = a_where[x]; + assert(b_used[idx]); + b_used[idx] = 0; + int l = *prev(exists.find(idx)); + int r = *next(exists.find(idx)); + int cur = 0; + if(l >= 0 && l < n && b_used[l] == 0) cur++; + if(r >= 0 && r < n && b_used[r] == 0) cur++; + ans *= cur; + exists.erase(idx); + } + cout << ans << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1442C b/code/ksun48/1442C new file mode 100644 index 0000000..a986962 --- /dev/null +++ b/code/ksun48/1442C @@ -0,0 +1,179 @@ +#include +using namespace std; + + +template struct modnum { + static constexpr int MOD = MOD_; + static_assert(MOD_ > 0, "MOD must be positive"); + +private: + using ll = long long; + + int v; + + static int minv(int a, int m) { + a %= m; + assert(a); + return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); + } + +public: + + modnum() : v(0) {} + modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } + explicit operator int() const { return v; } + friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } + friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } + + friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } + friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } + + modnum inv() const { + modnum res; + res.v = minv(v, MOD); + return res; + } + friend modnum inv(const modnum& m) { return m.inv(); } + modnum neg() const { + modnum res; + res.v = v ? MOD-v : 0; + return res; + } + friend modnum neg(const modnum& m) { return m.neg(); } + + modnum operator- () const { + return neg(); + } + modnum operator+ () const { + return modnum(*this); + } + + modnum& operator ++ () { + v ++; + if (v == MOD) v = 0; + return *this; + } + modnum& operator -- () { + if (v == 0) v = MOD; + v --; + return *this; + } + modnum& operator += (const modnum& o) { + v += o.v; + if (v >= MOD) v -= MOD; + return *this; + } + modnum& operator -= (const modnum& o) { + v -= o.v; + if (v < 0) v += MOD; + return *this; + } + modnum& operator *= (const modnum& o) { + v = int(ll(v) * ll(o.v) % MOD); + return *this; + } + modnum& operator /= (const modnum& o) { + return *this *= o.inv(); + } + + friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } + friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } + friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } + friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } + friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } + friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } +}; + +template T pow(T a, long long b) { + assert(b >= 0); + T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; +} + +using num = modnum<998244353>; + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + int n, m; + cin >> n >> m; + vector > > edges(2, vector > (n)); + for(int i = 0; i < m; i++){ + int u, v; + cin >> u >> v; + u--; v--; + edges[0][u].push_back(v); + edges[1][v].push_back(u); + } + const ll MAXR = 1e16; + { + ll ans = MAXR; + int R = 20; + vector dist(n, MAXR); + dist[0] = 0; + for(int r = 0; r < R; r++){ + vector vis(n, false); + priority_queue > cur; + for(int i = 0; i < n; i++) cur.push({-dist[i], i}); + while(!cur.empty()){ + pair z = cur.top(); + cur.pop(); + int v = z.second; + if(vis[v]) continue; + vis[v] = true; + for(int w : edges[r&1][v]){ + if(dist[w] > dist[v] + 1){ + dist[w] = dist[v] + 1; + cur.push({-dist[w], w}); + } + } + } + ans = min(ans, dist[n-1] + (1ll << r) - 1); + } + if(ans < MAXR){ + cout << ans << '\n'; + return 0; + } + } + { + vector > e2(2*n); + for(int v = 0; v < n; v++){ + for(int w : edges[0][v]){ + e2[v].push_back(w); + e2[w+n].push_back(v+n); + } + } + ll SW = 1e8; + + vector dist(2*n, MAXR); + dist[0] = 0; + vector vis(2*n, false); + priority_queue > cur; + for(int i = 0; i < 2*n; i++) cur.push({-dist[i], i}); + while(!cur.empty()){ + pair z = cur.top(); + cur.pop(); + int v = z.second; + if(vis[v]) continue; + vis[v] = true; + for(int w : e2[v]){ + if(dist[w] > dist[v] + 1){ + dist[w] = dist[v] + 1; + cur.push({-dist[w], w}); + } + } + { + int w = (v + n) % (2*n); + if(dist[w] > dist[v] + SW){ + dist[w] = dist[v] + SW; + cur.push({-dist[w], w}); + } + } + } + ll ans = min(dist[n-1], dist[n+n-1]); + assert(ans < MAXR); + ll nsw = ans / SW; + ll ne = ans % SW; + num z = pow(num(2), nsw) + ne - 1; + cout << z << '\n'; + } +} diff --git a/code/ksun48/1442D b/code/ksun48/1442D new file mode 100644 index 0000000..d7b1ede --- /dev/null +++ b/code/ksun48/1442D @@ -0,0 +1,77 @@ +#include +using namespace std; + + + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + using ll = long long; + int n, K; + cin >> n >> K; + + auto add = [&](vector& dp, int cost, ll wgt) -> void { + int r = (int)dp.size(); + for(int i = r-1-cost; i >= 0; i--){ + dp[i+cost] = max(dp[i+cost], dp[i] + wgt); + } + }; + auto combine = [&](vector dp1, vector dp2) -> vector { + vector res(dp1.size(), 0); + for(int i = 0; i < (int)dp1.size(); i++){ + for(int j = 0; i + j < (int)dp1.size(); j++){ + res[i+j] = max(res[i+j], dp1[i] + dp2[j]); + } + } + return res; + }; + vector > arrays; + vector > obj; + for(int i = 0; i < n; i++){ + int s; + cin >> s; + vector a(s); + for(ll& x : a) cin >> x; + if((int)a.size() > K) a.resize(K); + arrays.push_back(a); + ll sum = 0; + for(ll x : a) sum += x; + obj.push_back({a.size(), sum}); + } + + const int B = 50; + vector splits; + for(int i = 0; i <= n; i++){ + if(i % B == 0 || i == n) splits.push_back(i); + } + vector > pref(splits.size(), vector(K+1, 0)); + vector > suff(splits.size(), vector(K+1, 0)); + for(int i = 0; i + 1 < (int)splits.size(); i++){ + pref[i+1] = pref[i]; + for(int j = splits[i]; j < splits[i+1]; j++){ + add(pref[i+1], obj[j].first, obj[j].second); + } + } + for(int i = (int)splits.size() - 1; i > 0; i--){ + suff[i-1] = suff[i]; + for(int j = splits[i-1]; j < splits[i]; j++){ + add(suff[i-1], obj[j].first, obj[j].second); + } + } + ll ans = 0; + for(int i = 0; i + 1 < (int)splits.size(); i++){ + vector res = combine(pref[i], suff[i+1]); + for(int j = splits[i]; j < splits[i+1]; j++){ + vector tmp = res; + for(int k = splits[i]; k < splits[i+1]; k++){ + if(k != j) add(tmp, obj[k].first, obj[k].second); + } + ans = max(ans, tmp[K]); + ll csum = 0; + for(int v = 0; v < (int)arrays[j].size(); v++){ + csum += arrays[j][v]; + ans = max(ans, tmp[K-v-1] + csum); + } + } + } + cout << ans << '\n'; +} diff --git a/code/ksun48/1442E b/code/ksun48/1442E new file mode 100644 index 0000000..ff2829d --- /dev/null +++ b/code/ksun48/1442E @@ -0,0 +1,85 @@ +#include +using namespace std; + +namespace std { + +template +class y_combinator_result { + Fun fun_; +public: + template + explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} + + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; + +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} + +} // namespace std + +void solve(){ + int n; + cin >> n; + vector color(n); + for(int i = 0; i < n; i++){ + cin >> color[i]; + color[i]--; + } + vector > edges(n); + for(int i = 0; i < n-1; i++){ + int u, v; + cin >> u >> v; + u--; v--; + edges[u].push_back(v); + edges[v].push_back(u); + } + int s = 0; + int e = n; + while(s + 1 < e){ + int m = (s + e) / 2; + pair res = y_combinator( + [&](auto self, int v, int p) -> pair { + int m0a = 1, m0b = 1; + int m1a = 1, m1b = 1; + for(int w : edges[v]){ + if(w == p) continue; + pair z = self(w, v); + m0b = max(m0b, z.first); + if(m0b > m0a) swap(m0a, m0b); + m1b = max(m1b, z.second); + if(m1b > m1a) swap(m1a, m1b); + } + int r0 = 1e8; + int r1 = 1e8; + if(color[v] != 1 && m0a + m0b-1 <= 2*m-1){ + r0 = m0a; + } + if(color[v] != 0 && m1a + m1b-1 <= 2*m-1){ + r1 = m1a; + } + r0 = min(r0, r1 + 1); + r1 = min(r1, r0 + 1); + return {r0, r1}; + } + )(0, -1); + if(res.first <= n || res.second <= n){ + e = m; + } else { + s = m; + } + } + cout << e << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1444A b/code/ksun48/1444A new file mode 100644 index 0000000..d10a75b --- /dev/null +++ b/code/ksun48/1444A @@ -0,0 +1,31 @@ +#include +using namespace std; + +using ll = long long; +void solve(){ + ll p, q; + cin >> p >> q; + ll best = 0; + set pf; + ll cq = q; + for(ll r = 2; r * r <= cq; r++){ + while(cq % r == 0){ + pf.insert(r); + cq /= r; + } + } + if(cq > 1) pf.insert(cq); + for(ll r : pf){ + ll cp = p; + while(cp % q == 0) cp /= r; + best = max(best, cp); + } + cout << best << '\n'; +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/1444B b/code/ksun48/1444B new file mode 100644 index 0000000..5946072 --- /dev/null +++ b/code/ksun48/1444B @@ -0,0 +1,123 @@ +#include +using namespace std; + +template struct modnum { + static constexpr int MOD = MOD_; + static_assert(MOD_ > 0, "MOD must be positive"); + +private: + using ll = long long; + + int v; + + static int minv(int a, int m) { + a %= m; + assert(a); + return a == 1 ? 1 : int(m - ll(minv(m, a)) * ll(m) / a); + } + +public: + + modnum() : v(0) {} + modnum(ll v_) : v(int(v_ % MOD)) { if (v < 0) v += MOD; } + explicit operator int() const { return v; } + friend std::ostream& operator << (std::ostream& out, const modnum& n) { return out << int(n); } + friend std::istream& operator >> (std::istream& in, modnum& n) { ll v_; in >> v_; n = modnum(v_); return in; } + + friend bool operator == (const modnum& a, const modnum& b) { return a.v == b.v; } + friend bool operator != (const modnum& a, const modnum& b) { return a.v != b.v; } + + modnum inv() const { + modnum res; + res.v = minv(v, MOD); + return res; + } + friend modnum inv(const modnum& m) { return m.inv(); } + modnum neg() const { + modnum res; + res.v = v ? MOD-v : 0; + return res; + } + friend modnum neg(const modnum& m) { return m.neg(); } + + modnum operator- () const { + return neg(); + } + modnum operator+ () const { + return modnum(*this); + } + + modnum& operator ++ () { + v ++; + if (v == MOD) v = 0; + return *this; + } + modnum& operator -- () { + if (v == 0) v = MOD; + v --; + return *this; + } + modnum& operator += (const modnum& o) { + v += o.v; + if (v >= MOD) v -= MOD; + return *this; + } + modnum& operator -= (const modnum& o) { + v -= o.v; + if (v < 0) v += MOD; + return *this; + } + modnum& operator *= (const modnum& o) { + v = int(ll(v) * ll(o.v) % MOD); + return *this; + } + modnum& operator /= (const modnum& o) { + return *this *= o.inv(); + } + + friend modnum operator ++ (modnum& a, int) { modnum r = a; ++a; return r; } + friend modnum operator -- (modnum& a, int) { modnum r = a; --a; return r; } + friend modnum operator + (const modnum& a, const modnum& b) { return modnum(a) += b; } + friend modnum operator - (const modnum& a, const modnum& b) { return modnum(a) -= b; } + friend modnum operator * (const modnum& a, const modnum& b) { return modnum(a) *= b; } + friend modnum operator / (const modnum& a, const modnum& b) { return modnum(a) /= b; } +}; + +template T pow(T a, long long b) { + assert(b >= 0); + T r = 1; while (b) { if (b & 1) r *= a; b >>= 1; a *= a; } return r; +} + +using num = modnum<998244353>; + +vector fact, ifact; + +void init(){ + int N = 1100000; + fact = {1}; + for(int i = 1; i < N; i++) fact.push_back(i * fact[i-1]); + ifact.resize(N); + ifact.back() = 1 / fact.back(); + for(int i = N - 1; i > 0; i--) ifact[i-1] = i * ifact[i]; +} + +num ncr(int n, int k){ + if(k < 0 || k > n) return 0; + return fact[n] * ifact[k] * ifact[n-k]; +} +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + init(); + using ll = long long; + int n; + cin >> n; + vector a(2*n); + for(ll& x : a) cin >> x; + sort(a.begin(), a.end()); + num ans = ncr(2*n, n); + num mult; + for(int i = 0; i < n; i++){ + mult += a[i+n] - a[i]; + } + cout << ans * mult << '\n'; +} diff --git a/code/ksun48/1444C b/code/ksun48/1444C new file mode 100644 index 0000000..685ed93 --- /dev/null +++ b/code/ksun48/1444C @@ -0,0 +1,129 @@ +#include +using namespace std; + +namespace std { + +template +class y_combinator_result { + Fun fun_; +public: + template + explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} + + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; + +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} + +} // namespace std +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n, m, k; + cin >> n >> m >> k; + vector c(n); + for(int& x : c){ + cin >> x; + x--; + } + vector > > each_edges(k); + map, vector > > color_edges; + for(int i = 0; i < m; i++){ + int u, v; + cin >> u >> v; + u--; v--; + int cu = c[u]; + int cv = c[v]; + if(cu > cv){ + swap(cu, cv); + swap(u, v); + } + if(cu == cv){ + each_edges[cu].push_back({u, v}); + } else { + color_edges[{cu, cv}].push_back({u, v}); + } + } + vector > color_verts(k); + for(int i = 0; i < n; i++){ + color_verts[c[i]].push_back(i); + } + vector fail(k, false); + vector par(n); + vector offset(n); + for(int i = 0; i < n; i++){ + par[i] = i; + offset[i] = 0; + } + for(int col = 0; col < k; col++){ + map > edges; + for(pair e : each_edges[col]){ + edges[e.first].push_back(e.second); + edges[e.second].push_back(e.first); + } + for(int st : color_verts[col]){ + if(offset[st]) continue; + y_combinator( + [&](auto self, int v, int val) -> void { + if(offset[v] == -val) fail[col] = true; + if(offset[v]) return; + par[v] = st; + offset[v] = val; + for(int w : edges[v]){ + self(w, -val); + } + } + )(st, 1); + } + } + using ll = long long; + ll tot = 1ll * k * (k-1) / 2; + int good_groups = k; + for(int i = 0; i < k; i++){ + if(fail[i]){ + good_groups -= 1; + tot -= good_groups; + } + } + + vector tmp(n); + for(auto edge_set : color_edges){ + pair groups = edge_set.first; + if(fail[groups.first] || fail[groups.second]) continue; + + map > > edges; + for(pair e : edge_set.second){ + int val = -offset[e.first] * offset[e.second]; + edges[par[e.first]].push_back({par[e.second], val}); + edges[par[e.second]].push_back({par[e.first], val}); + } + + bool works = true; + for(auto g : edges){ + int st = g.first; + if(tmp[st]) continue; + y_combinator( + [&](auto self, int v, int val) -> void { + if(tmp[v] == -val) works = false; + if(tmp[v]) return; + tmp[v] = val; + for(auto e : edges[v]){ + self(e.first, val * e.second); + } + } + )(st, 1); + } + + for(auto g : edges){ + int st = g.first; + tmp[st] = 0; + } + if(!works) tot -= 1; + } + cout << tot << '\n'; +} diff --git a/code/ksun48/1444D b/code/ksun48/1444D new file mode 100644 index 0000000..f9bb4e6 --- /dev/null +++ b/code/ksun48/1444D @@ -0,0 +1,90 @@ +#include +using namespace std; + +const int R = 5.1e5; +using B = bitset; +void solve(){ + vector a[2]; + for(int j = 0; j < 2; j++){ + int r; + cin >> r; + a[j].resize(r); + for(int& x : a[j]) cin >> x; + sort(a[j].begin(), a[j].end()); + } + if(a[0].size() != a[1].size()){ + cout << "No" << '\n'; + return; + } + vector dir_len[2][2]; + for(int j = 0; j < 2; j++){ + int tot = 0; + vector f(a[j].size() + 1); + f[0][0] = 1; + for(int r = 0; r < (int)a[j].size(); r++){ + tot += a[j][r]; + f[r+1] = f[r] | (f[r] << a[j][r]); + } + if(tot % 2 != 0){ + cout << "No" << '\n'; + return; + } + if(f.back()[tot/2]){ + int cur = tot/2; + for(int r = (int)a[j].size() - 1; r >= 0; r--){ + if(f[r][cur]){ + dir_len[j][0].push_back(a[j][r]); + } else { + cur -= a[j][r]; + dir_len[j][1].push_back(a[j][r]); + } + } + } else { + cout << "No" << '\n'; + return; + } + } + vector r = dir_len[0][0]; + vector l = dir_len[0][1]; + vector u = dir_len[1][0]; + vector d = dir_len[1][1]; + sort(u.begin(), u.end()); + sort(d.rbegin(), d.rend()); + int lind = 0, rind = 0; + int llen = 0; + int rlen = 0; + while(lind + rind + 1 < (int)d.size()){ + assert(lind < (int)l.size() && rind < (int)r.size()); + if(llen + l[lind] > rlen + r[rind]){ + rlen += r[rind]; + rind++; + } else { + llen += l[lind]; + lind++; + } + } + vector dx; + for(int i = lind - 1; i >= 0; i--) dx.push_back(-l[i]); + for(int x : r) dx.push_back(x); + for(int i = (int)l.size()-1; i >= lind; i--) dx.push_back(-l[i]); + vector dy; + for(int x : d) dy.push_back(-x); + for(int x : u) dy.push_back(x); + cout << "Yes" << '\n'; + assert(dx.size() == dy.size()); + int cx = 0; + int cy = 0; + for(int i = 0; i < (int)dx.size(); i++){ + cout << cx << ' ' << cy << '\n'; + cy += dy[i]; + cout << cx << ' ' << cy << '\n'; + cx += dx[i]; + } +} + +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int T; + cin >> T; + while(T--) solve(); +} diff --git a/code/ksun48/414D b/code/ksun48/414D new file mode 100644 index 0000000..8d03372 --- /dev/null +++ b/code/ksun48/414D @@ -0,0 +1,65 @@ +#include +using namespace std; + +namespace std { + +template +class y_combinator_result { + Fun fun_; +public: + template + explicit y_combinator_result(T &&fun): fun_(std::forward(fun)) {} + + template + decltype(auto) operator()(Args &&...args) { + return fun_(std::ref(*this), std::forward(args)...); + } +}; + +template +decltype(auto) y_combinator(Fun &&fun) { + return y_combinator_result>(std::forward(fun)); +} + +} // namespace std +int main(){ + ios_base::sync_with_stdio(false), cin.tie(nullptr); + int n, k, p; + cin >> n >> k >> p; + vector > edges(n); + for(int i = 0; i < n-1; i++){ + int u, v; + cin >> u >> v; + u--; v--; + edges[u].push_back(v); + edges[v].push_back(u); + } + vector depths; + y_combinator( + [&](auto self, int v, int par, int d) -> void { + if(d > 0) depths.push_back(d); + for(int w : edges[v]){ + if(w == par) continue; + self(w, v, d+1); + } + } + )(0, -1, 0); + int best = 1; + sort(depths.rbegin(), depths.rend()); + int ccost = 0; + int j = 0; + int cval = depths.front(); + for(int i = 0; i < (int)depths.size(); i++){ + while(j < (int)depths.size() && ccost + (cval - depths[j]) <= p){ + ccost += cval - depths[j]; + j++; + } + best = max(best, j-i); + if(i + 1 < (int)depths.size()){ + ccost -= (j-i-1) * (cval - depths[i+1]); + cval = depths[i+1]; + } + } + best = min(best, k); + cout << best << '\n'; +} diff --git a/code/tourist/1423J b/code/tourist/1423J new file mode 100644 index 0000000..5fabb01 --- /dev/null +++ b/code/tourist/1423J @@ -0,0 +1,233 @@ +/** + * author: tourist + * created: 05.10.2020 16:10:32 +**/ +#include + +using namespace std; + +template +T inverse(T a, T m) { + T u = 0, v = 1; + while (a != 0) { + T t = m / a; + m -= t * a; swap(a, m); + u -= t * v; swap(u, v); + } + assert(m == 1); + return u; +} + +template +class Modular { + public: + using Type = typename decay::type; + + constexpr Modular() : value() {} + template + Modular(const U& x) { + value = normalize(x); + } + + template + static Type normalize(const U& x) { + Type v; + if (-mod() <= x && x < mod()) v = static_cast(x); + else v = static_cast(x % mod()); + if (v < 0) v += mod(); + return v; + } + + const Type& operator()() const { return value; } + template + explicit operator U() const { return static_cast(value); } + constexpr static Type mod() { return T::value; } + + Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } + Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } + template Modular& operator+=(const U& other) { return *this += Modular(other); } + template Modular& operator-=(const U& other) { return *this -= Modular(other); } + Modular& operator++() { return *this += 1; } + Modular& operator--() { return *this -= 1; } + Modular operator++(int) { Modular result(*this); *this += 1; return result; } + Modular operator--(int) { Modular result(*this); *this -= 1; return result; } + Modular operator-() const { return Modular(-value); } + + template + typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { +#ifdef _WIN32 + uint64_t x = static_cast(value) * static_cast(rhs.value); + uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m; + asm( + "divl %4; \n\t" + : "=a" (d), "=d" (m) + : "d" (xh), "a" (xl), "r" (mod()) + ); + value = m; +#else + value = normalize(static_cast(value) * static_cast(rhs.value)); +#endif + return *this; + } + template + typename enable_if::Type, int64_t>::value, Modular>::type& operator*=(const Modular& rhs) { + int64_t q = static_cast(static_cast(value) * rhs.value / mod()); + value = normalize(value * rhs.value - q * mod()); + return *this; + } + template + typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) { + value = normalize(value * rhs.value); + return *this; + } + + Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } + + template + friend const Modular& abs(const Modular& v) { return v; } + + template + friend bool operator==(const Modular& lhs, const Modular& rhs); + + template + friend bool operator<(const Modular& lhs, const Modular& rhs); + + template + friend std::istream& operator>>(std::istream& stream, Modular& number); + + private: + Type value; +}; + +template bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; } +template bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); } +template bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; } + +template bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); } +template bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); } +template bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); } + +template bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; } + +template Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; } +template Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; } +template Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; } + +template Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } + +template Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } + +template Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } + +template +Modular power(const Modular& a, const U& b) { + assert(b >= 0); + Modular x = a, res = 1; + U p = b; + while (p > 0) { + if (p & 1) res *= x; + x *= x; + p >>= 1; + } + return res; +} + +template +bool IsZero(const Modular& number) { + return number() == 0; +} + +template +string to_string(const Modular& number) { + return to_string(number()); +} + +template +std::ostream& operator<<(std::ostream& stream, const Modular& number) { + return stream << number(); +} + +template +std::istream& operator>>(std::istream& stream, Modular& number) { + typename common_type::Type, int64_t>::type x; + stream >> x; + number.value = Modular::normalize(x); + return stream; +} + +/* +using ModType = int; + +struct VarMod { static ModType value; }; +ModType VarMod::value; +ModType& md = VarMod::value; +using Mint = Modular; +*/ + +constexpr int md = (int) 1e9 + 7; +using Mint = Modular::type, md>>; + +vector fact(1, 1); +vector inv_fact(1, 1); + +Mint C(int n, int k) { + if (k < 0 || k > n) { + return 0; + } + while ((int) fact.size() < n + 1) { + fact.push_back(fact.back() * (int) fact.size()); + inv_fact.push_back(1 / fact.back()); + } + return fact[n] * inv_fact[k] * inv_fact[n - k]; +} + +const int M = 7; + +int d[123]; +Mint dp[M]; +Mint new_dp[M + 1]; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int tt; + cin >> tt; + while (tt--) { + long long m; + cin >> m; + int len = 0; + while (m > 0) { + d[len++] = (m & 1); + m >>= 1; + } + for (int i = 0; i < M; i++) { + dp[i] = 0; + } + dp[0] = 1; + for (int it = len - 1; it >= 0; it--) { + for (int i = 0; i <= M; i++) { + new_dp[i] = 0; + } + for (int i = 0; i < M; i++) { + int ni = i * 2 + d[it]; + int from = max(ni - 7, 0); + int to = min(M - 1, ni); + new_dp[from] += dp[i]; + new_dp[to + 1] -= dp[i]; + } + Mint s = 0; + for (int i = 0; i < M; i++) { + s += new_dp[i]; + dp[i] = s; + } + } + cout << dp[0] << '\n'; + } + return 0; +} diff --git a/code/tourist/1434A b/code/tourist/1434A new file mode 100644 index 0000000..9a685ff --- /dev/null +++ b/code/tourist/1434A @@ -0,0 +1,63 @@ +/** + * author: tourist + * created: 25.10.2020 14:04:52 +**/ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n = 6; + vector a(n); + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + int m; + cin >> m; + vector b(m); + for (int i = 0; i < m; i++) { + cin >> b[i]; + } + vector> x(m, vector(n)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + x[i][j] = b[i] - a[j]; + } + sort(x[i].begin(), x[i].end()); + } + vector> p; + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + p.emplace_back(x[i][j], i); + } + } + sort(p.begin(), p.end()); + multiset s; + for (int i = 0; i < m; i++) { + s.insert(x[i][0]); + } + vector ptr(m, 0); + int ans = (int) 2e9; + { + int mn = *s.begin(); + int mx = *prev(s.end()); + ans = min(ans, mx - mn); + } + for (auto& q : p) { + int i = q.second; + s.erase(s.find(x[i][ptr[i]])); + ++ptr[i]; + if (ptr[i] == n) { + break; + } + s.insert(x[i][ptr[i]]); + int mn = *s.begin(); + int mx = *prev(s.end()); + ans = min(ans, mx - mn); + } + cout << ans << '\n'; + return 0; +} + diff --git a/code/tourist/1434B b/code/tourist/1434B new file mode 100644 index 0000000..5230200 --- /dev/null +++ b/code/tourist/1434B @@ -0,0 +1,52 @@ +/** + * author: tourist + * created: 25.10.2020 14:11:57 +**/ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n; + cin >> n; + vector op(2 * n); + vector st; + for (int i = 0; i < 2 * n; i++) { + string s; + cin >> s; + if (s == "+") { + st.push_back(i); + } else { + if (st.empty()) { + cout << "NO" << '\n'; + return 0; + } + cin >> op[i]; + op[st.back()] = -op[i]; + st.pop_back(); + } + } + set s; + for (int i = 0; i < 2 * n; i++) { + if (op[i] < 0) { + s.insert(-op[i]); + } else { + if (s.empty() || *s.begin() != op[i]) { + cout << "NO" << '\n'; + return 0; + } + s.erase(s.begin()); + } + } + cout << "YES" << '\n'; + for (int i = 0; i < 2 * n; i++) { + if (op[i] < 0) { + cout << -op[i] << " "; + } + } + cout << '\n'; + return 0; +} + diff --git a/code/tourist/1434C b/code/tourist/1434C new file mode 100644 index 0000000..914970f --- /dev/null +++ b/code/tourist/1434C @@ -0,0 +1,30 @@ +/** + * author: tourist + * created: 25.10.2020 14:23:37 +**/ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int tt; + cin >> tt; + while (tt--) { + long long a, b, c, d; + cin >> a >> b >> c >> d; + if (a - b * c > 0) { + cout << -1 << '\n'; + continue; + } + long long step = b * d; + long long last = a % step; + long long first = a; + long long cnt = (first - last) / step + 1; + long long ans = (last + first) * cnt / 2; + cout << ans << '\n'; + } + return 0; +} + diff --git a/code/tourist/1434E b/code/tourist/1434E new file mode 100644 index 0000000..267691b --- /dev/null +++ b/code/tourist/1434E @@ -0,0 +1,195 @@ +/** + * author: tourist + * created: 25.10.2020 14:45:24 +**/ +#include + +using namespace std; + +template +string to_string(pair p); + +template +string to_string(tuple p); + +template +string to_string(tuple p); + +string to_string(const string& s) { + return '"' + s + '"'; +} + +string to_string(const char* s) { + return to_string((string) s); +} + +string to_string(bool b) { + return (b ? "true" : "false"); +} + +string to_string(vector v) { + bool first = true; + string res = "{"; + for (int i = 0; i < static_cast(v.size()); i++) { + if (!first) { + res += ", "; + } + first = false; + res += to_string(v[i]); + } + res += "}"; + return res; +} + +template +string to_string(bitset v) { + string res = ""; + for (size_t i = 0; i < N; i++) { + res += static_cast('0' + v[i]); + } + return res; +} + +template +string to_string(A v) { + bool first = true; + string res = "{"; + for (const auto &x : v) { + if (!first) { + res += ", "; + } + first = false; + res += to_string(x); + } + res += "}"; + return res; +} + +template +string to_string(pair p) { + return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; +} + +template +string to_string(tuple p) { + return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; +} + +template +string to_string(tuple p) { + return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; +} + +void debug_out() { cerr << endl; } + +template +void debug_out(Head H, Tail... T) { + cerr << " " << to_string(H); + debug_out(T...); +} + +#ifdef LOCAL +#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) +#else +#define debug(...) 42 +#endif + +class dsu { + public: + vector p; + int n; + + dsu(int _n) : n(_n) { + p.resize(n); + iota(p.begin(), p.end(), 0); + } + + inline int get(int x) { + return (x == p[x] ? x : (p[x] = get(p[x]))); + } + + inline bool unite(int x, int y) { + x = get(x); + y = get(y); + if (x != y) { + p[x] = y; + return true; + } + return false; + } +}; + +const int MAX = 100010; +int bound[MAX]; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int tt; + cin >> tt; + int x = 0; + while (tt--) { + int n; + cin >> n; + vector v(n); + for (int i = 0; i < n; i++) { + cin >> v[i]; + } + { + int ptr = -1; + for (int val = 0; val < MAX; val++) { + if (ptr + 1 < n && v[ptr + 1] == val) { + ++ptr; + } + bound[val] = ptr; + } + } + vector> who(1, vector(n, -1)); + vector d(1, dsu(n + 1)); + vector seen; + for (int i = n - 1; i >= 0; i--) { + int start = 0; + int j = 0; + while (start < i) { + if (j == (int) who.size()) { + who.emplace_back(n, -1); + d.emplace_back(n + 1); + } + int finish = i - 1; + int tmp = who[j][i]; + if (tmp != -1) { + int val = v[i] - (v[tmp] - v[i]); + finish = (val <= 0 ? -1 : bound[val]); + } +// debug(i, j, start, finish); + if (start <= finish) { + int pos = d[j].get(start); + while (pos <= finish) { + who[j][pos] = i; + d[j].unite(pos, pos + 1); + pos = d[j].get(pos); + } + start = finish + 1; + } + ++j; + } + int g = 0; + while (g < (int) who.size() && who[g][i] != -1) { + ++g; + } + if (g >= (int) seen.size()) { + seen.resize(g + 1); + } + seen[g] = true; + } + int g = 0; + while (g < (int) seen.size() && seen[g]) { + ++g; + } + x ^= g; + debug(g); + } + cout << (x > 0 ? "YES" : "NO") << '\n'; + return 0; +} + diff --git a/code/tourist/1441A b/code/tourist/1441A new file mode 100644 index 0000000..236eae9 --- /dev/null +++ b/code/tourist/1441A @@ -0,0 +1,246 @@ +/** + * author: tourist + * created: 01.11.2020 17:04:57 +**/ +#include + +using namespace std; + +template +T inverse(T a, T m) { + T u = 0, v = 1; + while (a != 0) { + T t = m / a; + m -= t * a; swap(a, m); + u -= t * v; swap(u, v); + } + assert(m == 1); + return u; +} + +template +class Modular { + public: + using Type = typename decay::type; + + constexpr Modular() : value() {} + template + Modular(const U& x) { + value = normalize(x); + } + + template + static Type normalize(const U& x) { + Type v; + if (-mod() <= x && x < mod()) v = static_cast(x); + else v = static_cast(x % mod()); + if (v < 0) v += mod(); + return v; + } + + const Type& operator()() const { return value; } + template + explicit operator U() const { return static_cast(value); } + constexpr static Type mod() { return T::value; } + + Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } + Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } + template Modular& operator+=(const U& other) { return *this += Modular(other); } + template Modular& operator-=(const U& other) { return *this -= Modular(other); } + Modular& operator++() { return *this += 1; } + Modular& operator--() { return *this -= 1; } + Modular operator++(int) { Modular result(*this); *this += 1; return result; } + Modular operator--(int) { Modular result(*this); *this -= 1; return result; } + Modular operator-() const { return Modular(-value); } + + template + typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { +#ifdef _WIN32 + uint64_t x = static_cast(value) * static_cast(rhs.value); + uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m; + asm( + "divl %4; \n\t" + : "=a" (d), "=d" (m) + : "d" (xh), "a" (xl), "r" (mod()) + ); + value = m; +#else + value = normalize(static_cast(value) * static_cast(rhs.value)); +#endif + return *this; + } + template + typename enable_if::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { + long long q = static_cast(static_cast(value) * rhs.value / mod()); + value = normalize(value * rhs.value - q * mod()); + return *this; + } + template + typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) { + value = normalize(value * rhs.value); + return *this; + } + + Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } + + friend const Type& abs(const Modular& x) { return x.value; } + + template + friend bool operator==(const Modular& lhs, const Modular& rhs); + + template + friend bool operator<(const Modular& lhs, const Modular& rhs); + + template + friend V& operator>>(V& stream, Modular& number); + + private: + Type value; +}; + +template bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; } +template bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); } +template bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; } + +template bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); } +template bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); } +template bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); } + +template bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; } + +template Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; } +template Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; } +template Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; } + +template Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } + +template Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } + +template Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } + +template +Modular power(const Modular& a, const U& b) { + assert(b >= 0); + Modular x = a, res = 1; + U p = b; + while (p > 0) { + if (p & 1) res *= x; + x *= x; + p >>= 1; + } + return res; +} + +template +bool IsZero(const Modular& number) { + return number() == 0; +} + +template +string to_string(const Modular& number) { + return to_string(number()); +} + +// U == std::ostream? but done this way because of fastoutput +template +U& operator<<(U& stream, const Modular& number) { + return stream << number(); +} + +// U == std::istream? but done this way because of fastinput +template +U& operator>>(U& stream, Modular& number) { + typename common_type::Type, long long>::type x; + stream >> x; + number.value = Modular::normalize(x); + return stream; +} + +/* +using ModType = int; + +struct VarMod { static ModType value; }; +ModType VarMod::value; +ModType& md = VarMod::value; +using Mint = Modular; +*/ + +constexpr int md = 998244353; +using Mint = Modular::type, md>>; + +/*vector fact(1, 1); +vector inv_fact(1, 1); + +Mint C(int n, int k) { + if (k < 0 || k > n) { + return 0; + } + while ((int) fact.size() < n + 1) { + fact.push_back(fact.back() * (int) fact.size()); + inv_fact.push_back(1 / fact.back()); + } + return fact[n] * inv_fact[k] * inv_fact[n - k]; +}*/ + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int tt; + cin >> tt; + while (tt--) { + int n, m; + cin >> n >> m; + vector a(n); + for (int i = 0; i < n; i++) { + cin >> a[i]; + --a[i]; + } + vector pos(n); + for (int i = 0; i < n; i++) { + pos[a[i]] = i; + } + vector b(m); + for (int i = 0; i < m; i++) { + cin >> b[i]; + --b[i]; + b[i] = pos[b[i]]; + } + vector pr(n); + vector ne(n); + for (int i = 0; i < n; i++) { + pr[i] = i - 1; + ne[i] = i + 1; + } + vector when(n, -1); + for (int i = 0; i < m; i++) { + when[b[i]] = i; + } + Mint ans = 1; + for (int it = 0; it < m; it++) { + int x = b[it]; + int ways = 0; + if (pr[x] != -1 && when[pr[x]] == -1) { + ++ways; + } + if (ne[x] != n && when[ne[x]] == -1) { + ++ways; + } + ans *= ways; + if (pr[x] != -1) { + ne[pr[x]] = ne[x]; + } + if (ne[x] != n) { + pr[ne[x]] = pr[x]; + } + } + cout << ans << '\n'; + } + return 0; +} + diff --git a/code/tourist/1441B b/code/tourist/1441B new file mode 100644 index 0000000..de826d5 --- /dev/null +++ b/code/tourist/1441B @@ -0,0 +1,274 @@ +/** + * author: tourist + * created: 01.11.2020 17:09:29 +**/ +#include + +using namespace std; + +template +T inverse(T a, T m) { + T u = 0, v = 1; + while (a != 0) { + T t = m / a; + m -= t * a; swap(a, m); + u -= t * v; swap(u, v); + } + assert(m == 1); + return u; +} + +template +class Modular { + public: + using Type = typename decay::type; + + constexpr Modular() : value() {} + template + Modular(const U& x) { + value = normalize(x); + } + + template + static Type normalize(const U& x) { + Type v; + if (-mod() <= x && x < mod()) v = static_cast(x); + else v = static_cast(x % mod()); + if (v < 0) v += mod(); + return v; + } + + const Type& operator()() const { return value; } + template + explicit operator U() const { return static_cast(value); } + constexpr static Type mod() { return T::value; } + + Modular& operator+=(const Modular& other) { if ((value += other.value) >= mod()) value -= mod(); return *this; } + Modular& operator-=(const Modular& other) { if ((value -= other.value) < 0) value += mod(); return *this; } + template Modular& operator+=(const U& other) { return *this += Modular(other); } + template Modular& operator-=(const U& other) { return *this -= Modular(other); } + Modular& operator++() { return *this += 1; } + Modular& operator--() { return *this -= 1; } + Modular operator++(int) { Modular result(*this); *this += 1; return result; } + Modular operator--(int) { Modular result(*this); *this -= 1; return result; } + Modular operator-() const { return Modular(-value); } + + template + typename enable_if::Type, int>::value, Modular>::type& operator*=(const Modular& rhs) { +#ifdef _WIN32 + uint64_t x = static_cast(value) * static_cast(rhs.value); + uint32_t xh = static_cast(x >> 32), xl = static_cast(x), d, m; + asm( + "divl %4; \n\t" + : "=a" (d), "=d" (m) + : "d" (xh), "a" (xl), "r" (mod()) + ); + value = m; +#else + value = normalize(static_cast(value) * static_cast(rhs.value)); +#endif + return *this; + } + template + typename enable_if::Type, long long>::value, Modular>::type& operator*=(const Modular& rhs) { + long long q = static_cast(static_cast(value) * rhs.value / mod()); + value = normalize(value * rhs.value - q * mod()); + return *this; + } + template + typename enable_if::Type>::value, Modular>::type& operator*=(const Modular& rhs) { + value = normalize(value * rhs.value); + return *this; + } + + Modular& operator/=(const Modular& other) { return *this *= Modular(inverse(other.value, mod())); } + + friend const Type& abs(const Modular& x) { return x.value; } + + template + friend bool operator==(const Modular& lhs, const Modular& rhs); + + template + friend bool operator<(const Modular& lhs, const Modular& rhs); + + template + friend V& operator>>(V& stream, Modular& number); + + private: + Type value; +}; + +template bool operator==(const Modular& lhs, const Modular& rhs) { return lhs.value == rhs.value; } +template bool operator==(const Modular& lhs, U rhs) { return lhs == Modular(rhs); } +template bool operator==(U lhs, const Modular& rhs) { return Modular(lhs) == rhs; } + +template bool operator!=(const Modular& lhs, const Modular& rhs) { return !(lhs == rhs); } +template bool operator!=(const Modular& lhs, U rhs) { return !(lhs == rhs); } +template bool operator!=(U lhs, const Modular& rhs) { return !(lhs == rhs); } + +template bool operator<(const Modular& lhs, const Modular& rhs) { return lhs.value < rhs.value; } + +template Modular operator+(const Modular& lhs, const Modular& rhs) { return Modular(lhs) += rhs; } +template Modular operator+(const Modular& lhs, U rhs) { return Modular(lhs) += rhs; } +template Modular operator+(U lhs, const Modular& rhs) { return Modular(lhs) += rhs; } + +template Modular operator-(const Modular& lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(const Modular& lhs, U rhs) { return Modular(lhs) -= rhs; } +template Modular operator-(U lhs, const Modular& rhs) { return Modular(lhs) -= rhs; } + +template Modular operator*(const Modular& lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(const Modular& lhs, U rhs) { return Modular(lhs) *= rhs; } +template Modular operator*(U lhs, const Modular& rhs) { return Modular(lhs) *= rhs; } + +template Modular operator/(const Modular& lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(const Modular& lhs, U rhs) { return Modular(lhs) /= rhs; } +template Modular operator/(U lhs, const Modular& rhs) { return Modular(lhs) /= rhs; } + +template +Modular power(const Modular& a, const U& b) { + assert(b >= 0); + Modular x = a, res = 1; + U p = b; + while (p > 0) { + if (p & 1) res *= x; + x *= x; + p >>= 1; + } + return res; +} + +template +bool IsZero(const Modular& number) { + return number() == 0; +} + +template +string to_string(const Modular& number) { + return to_string(number()); +} + +// U == std::ostream? but done this way because of fastoutput +template +U& operator<<(U& stream, const Modular& number) { + return stream << number(); +} + +// U == std::istream? but done this way because of fastinput +template +U& operator>>(U& stream, Modular& number) { + typename common_type::Type, long long>::type x; + stream >> x; + number.value = Modular::normalize(x); + return stream; +} + +/* +using ModType = int; + +struct VarMod { static ModType value; }; +ModType VarMod::value; +ModType& md = VarMod::value; +using Mint = Modular; +*/ + +constexpr int md = 998244353; +using Mint = Modular::type, md>>; + +/*vector fact(1, 1); +vector inv_fact(1, 1); + +Mint C(int n, int k) { + if (k < 0 || k > n) { + return 0; + } + while ((int) fact.size() < n + 1) { + fact.push_back(fact.back() * (int) fact.size()); + inv_fact.push_back(1 / fact.back()); + } + return fact[n] * inv_fact[k] * inv_fact[n - k]; +}*/ + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n, m; + cin >> n >> m; + vector>> g(n); + for (int i = 0; i < m; i++) { + int x, y; + cin >> x >> y; + --x; --y; + g[x].emplace_back(y, 0); + g[y].emplace_back(x, 1); + } + const int inf = (int) 1e9; + { + const int MAX = 25; + vector dist(n, n); + int ans = inf; + int add = 0; + dist[0] = 0; + vector> at(n + 1); + for (int step = 0; step < MAX; step++) { + for (int i = 0; i <= n; i++) { + at[i].clear(); + } + for (int i = 0; i < n; i++) { + at[dist[i]].push_back(i); + } + for (int i = 0; i < n; i++) { + for (int v : at[i]) { + if (dist[v] == i) { + for (auto& e : g[v]) { + if (e.second == (step & 1)) { + int to = e.first; + if (i + 1 < dist[to]) { + dist[to] = i + 1; + at[i + 1].push_back(to); + } + } + } + } + } + } + if (dist[n - 1] < n) { + ans = min(ans, dist[n - 1] + add); + } + add += (1 << step); + } + if (ans != inf) { + cout << ans << '\n'; + return 0; + } + } + vector>> d(n, vector>(2, make_pair(inf, 0))); + d[0][0] = make_pair(0, 0); + set, pair>> s; + s.emplace(d[0][0], make_pair(0, 0)); + while (!s.empty()) { + int i = s.begin()->second.first; + int j = s.begin()->second.second; + s.erase(s.begin()); + for (auto& e : g[i]) { + int to = e.first; + int nj = j; + pair ft = d[i][j]; + ft.second += 1; + if (e.second != (ft.first & 1)) { + ft.first += 1; + nj ^= 1; + } + if (ft < d[to][nj]) { + s.erase(make_pair(d[to][nj], make_pair(to, nj))); + d[to][nj] = ft; + s.emplace(d[to][nj], make_pair(to, nj)); + } + } + } + int j = (d[n - 1][0].first < d[n - 1][1].first ? 0 : 1); + Mint res = power(Mint(2), d[n - 1][j].first) - 1; + res += d[n - 1][j].second; + cout << res << '\n'; + return 0; +} + diff --git a/code/tourist/1441C b/code/tourist/1441C new file mode 100644 index 0000000..9a09757 --- /dev/null +++ b/code/tourist/1441C @@ -0,0 +1,76 @@ +/** + * author: tourist + * created: 01.11.2020 17:14:09 +**/ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n, k; + cin >> n >> k; + vector> a(n); + vector sz(n); + vector sum(n); + for (int i = 0; i < n; i++) { + int foo; + cin >> foo; + a[i].resize(foo); + sz[i] = foo; + sum[i] = 0; + for (int j = 0; j < foo; j++) { + cin >> a[i][j]; + sum[i] += a[i][j]; + } + } + const long long inf = (long long) 1e18; + vector> save(30, vector(k + 1, -inf)); + vector dp(k + 1, -inf); + dp[0] = 0; + long long ans = 0; + function Solve = [&](int level, int from, int to) { + if (from == to) { + long long took = 0; + for (int i = 0; i <= k; i++) { + ans = max(ans, took + dp[k - i]); + if (i == (int) a[from].size()) { + break; + } + took += a[from][i]; + } + return; + } + for (int j = 0; j <= k; j++) { + save[level][j] = dp[j]; + } + int mid = (from + to) >> 1; + { + for (int i = from; i <= mid; i++) { + for (int j = k - sz[i]; j >= 0; j--) { + dp[j + sz[i]] = max(dp[j + sz[i]], dp[j] + sum[i]); + } + } + Solve(level + 1, mid + 1, to); + for (int j = 0; j <= k; j++) { + dp[j] = save[level][j]; + } + } + { + for (int i = mid + 1; i <= to; i++) { + for (int j = k - sz[i]; j >= 0; j--) { + dp[j + sz[i]] = max(dp[j + sz[i]], dp[j] + sum[i]); + } + } + Solve(level + 1, from, mid); + for (int j = 0; j <= k; j++) { + dp[j] = save[level][j]; + } + } + }; + Solve(0, 0, n - 1); + cout << ans << '\n'; + return 0; +} + diff --git a/code/tourist/1441D b/code/tourist/1441D new file mode 100644 index 0000000..07fd809 --- /dev/null +++ b/code/tourist/1441D @@ -0,0 +1,95 @@ +/** + * author: tourist + * created: 01.11.2020 17:47:09 +**/ +#include + +using namespace std; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int tt; + cin >> tt; + while (tt--) { + int n; + cin >> n; + vector color(n); + for (int i = 0; i < n; i++) { + cin >> color[i]; + --color[i]; + } + vector> g(n); + for (int i = 0; i < n - 1; i++) { + int x, y; + cin >> x >> y; + --x; --y; + g[x].push_back(y); + g[y].push_back(x); + } + vector order; + vector pv(n, -1); + function Dfs = [&](int v, int pr) { + pv[v] = pr; + order.push_back(v); + for (int u : g[v]) { + if (u == pr) { + continue; + } + Dfs(u, v); + } + }; + Dfs(0, -1); + const int inf = (int) 1e9; + vector> dp(n, vector(2)); + int low = 0, high = n; + while (low < high) { + int mid = (low + high) >> 1; + for (int it = n - 1; it >= 0; it--) { + int v = order[it]; + for (int j = 0; j < 2; j++) { + dp[v][j] = -1; + if (color[v] == 1 - j) { + continue; + } + int mx1 = 0; + int mx2 = 0; + bool ok = true; + for (int u : g[v]) { + if (pv[u] == v) { + int mn = inf; + for (int jj = 0; jj < 2; jj++) { + if (dp[u][jj] != -1) { + mn = min(mn, dp[u][jj] + (j != jj)); + } + } + if (mn == inf) { + ok = false; + break; + } + if (mn > mx1) { + mx2 = mx1; + mx1 = mn; + } else { + if (mn > mx2) { + mx2 = mn; + } + } + } + } + if (ok && mx1 + mx2 <= mid) { + dp[v][j] = mx1; + } + } + } + if (dp[0][0] != -1 || dp[0][1] != -1) { + high = mid; + } else { + low = mid + 1; + } + } + cout << (low + 3) / 2 << '\n'; + } + return 0; +} + diff --git a/code/tourist/1441E b/code/tourist/1441E new file mode 100644 index 0000000..5e63818 --- /dev/null +++ b/code/tourist/1441E @@ -0,0 +1,186 @@ +/** + * author: tourist + * created: 01.11.2020 18:46:06 +**/ +#include + +using namespace std; + +const int MAX = 20; + +template +class graph { + public: + struct edge { + int from; + int to; + T cost; + }; + + vector edges; + vector> g; + int n; + + graph(int _n) : n(_n) { + g.resize(n); + } + + virtual int add(int from, int to, T cost) = 0; +}; + +template +class digraph : public graph { + public: + using graph::edges; + using graph::g; + using graph::n; + + digraph(int _n) : graph(_n) { + } + + int add(int from, int to, T cost = 1) { + assert(0 <= from && from < n && 0 <= to && to < n); + int id = (int) edges.size(); + g[from].push_back(id); + edges.push_back({from, to, cost}); + return id; + } + + digraph reverse() const { + digraph rev(n); + for (auto &e : edges) { + rev.add(e.to, e.from, e.cost); + } + return rev; + } +}; + +template +vector find_topsort(const digraph &g) { + vector deg(g.n, 0); + for (int id = 0; id < (int) g.edges.size(); id++) { + deg[g.edges[id].to]++; + } + vector x; + for (int i = 0; i < g.n; i++) { + if (deg[i] == 0) { + x.push_back(i); + } + } + for (int ptr = 0; ptr < (int) x.size(); ptr++) { + int i = x[ptr]; + for (int id : g.g[i]) { + auto &e = g.edges[id]; + int to = e.to; + if (--deg[to] == 0) { + x.push_back(to); + } + } + } + if ((int) x.size() != g.n) { + return vector(); + } + return x; +} + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n, m, tt; + cin >> n >> m >> tt; + vector> g(n, vector(n, 0)); + digraph gr(n); + for (int i = 0; i < m; i++) { + int x, y; + cin >> x >> y; + --x; --y; + g[x][y] = 1; + gr.add(x, y); + } + vector order = find_topsort(gr); + reverse(order.begin(), order.end()); + int magic = min(n, MAX); + vector masks; + for (int bc = 0; bc <= magic; bc++) { + for (int mask = 0; mask < (1 << magic); mask++) { + if (__builtin_popcount(mask) == bc) { + masks.push_back(mask); + } + } + } + vector> ops; + for (int i = 0; i < magic; i++) { + for (int j = i + 1; j < magic; j++) { + if (g[order[j]][order[i]] == 0) { + ops.emplace_back('+', order[j], order[i]); + g[order[j]][order[i]] = 1; + } + } + } + vector used(1 << magic); + vector got(n, -1); + for (int i = magic; i < n; i++) { + int me = 0; + for (int j = 0; j < magic; j++) { + if (g[order[i]][order[j]]) { + me |= (1 << j); + } + } + int goal = -1; + for (int x : masks) { + if (!used[me ^ x]) { + goal = me ^ x; + used[me ^ x] = true; + break; + } + } + assert(goal != -1); + got[order[i]] = goal; + for (int j = 0; j < magic; j++) { + if ((me ^ goal) & (1 << j)) { + ops.emplace_back(g[order[i]][order[j]] == 1 ? '-' : '+', order[i], order[j]); + } + } + ops.emplace_back('+', order[i], order[i]); + g[order[i]][order[i]] = 1; + } + cout << ops.size() << endl; + for (auto& op : ops) { + cout << get<0>(op) << " " << get<1>(op) + 1 << " " << get<2>(op) + 1 << endl; + } + while (tt--) { + bool found = false; + int mask = 0; + for (int i = 0; i < magic; i++) { + cout << "? 1 " << order[i] + 1 << endl; + string foo; + cin >> foo; + if (foo == "Lose") { + cout << "! " << order[i] + 1 << endl; + string bar; + cin >> bar; + assert(bar == "Correct"); + found = true; + break; + } + if (foo == "Win") { + mask |= (1 << i); + } + } + if (!found) { + for (int i = 0; i < n; i++) { + if (got[i] == mask) { + cout << "! " << i + 1 << endl; + string bar; + cin >> bar; + assert(bar == "Correct"); + found = true; + break; + } + } + assert(found); + } + } + return 0; +} + diff --git a/code/tourist/818G b/code/tourist/818G new file mode 100644 index 0000000..7babfbf --- /dev/null +++ b/code/tourist/818G @@ -0,0 +1,200 @@ +/** + * author: tourist + * created: 13.10.2020 14:06:36 +**/ +#include + +using namespace std; + +#include + +template +class MCMF { + public: + static constexpr T eps = (T) 1e-9; + + struct edge { + int from; + int to; + T c; + T f; + C cost; + }; + + int n; + vector> g; + vector edges; + vector d; + vector pot; + __gnu_pbds::priority_queue> q; + vector its; + vector pe; + const C INF_C = numeric_limits::max() / 2; + + explicit MCMF(int n_) : n(n_), g(n), d(n), pot(n, 0), its(n), pe(n) {} + + int add(int from, int to, T forward_cap, T backward_cap, C edge_cost) { + assert(0 <= from && from < n && 0 <= to && to < n); + assert(forward_cap >= 0 && backward_cap >= 0); + int id = static_cast(edges.size()); + g[from].push_back(id); + edges.push_back({from, to, forward_cap, 0, edge_cost}); + g[to].push_back(id + 1); + edges.push_back({to, from, backward_cap, 0, -edge_cost}); + return id; + } + + void expath(int st) { + fill(d.begin(), d.end(), INF_C); + q.clear(); + fill(its.begin(), its.end(), q.end()); + its[st] = q.push({pot[st], st}); + d[st] = 0; + while (!q.empty()) { + int i = q.top().second; + q.pop(); + its[i] = q.end(); + for (int id : g[i]) { + const edge &e = edges[id]; + int j = e.to; + if (e.c - e.f > eps && d[i] + e.cost < d[j]) { + d[j] = d[i] + e.cost; + pe[j] = id; + if (its[j] == q.end()) { + its[j] = q.push({pot[j] - d[j], j}); + } else { + q.modify(its[j], {pot[j] - d[j], j}); + } + } + } + } + swap(d, pot); + } + + pair max_flow_min_cost(int st, int fin) { + T flow = 0; + C cost = 0; + bool ok = true; + for (auto& e : edges) { + if (e.c - e.f > eps && e.cost + pot[e.from] - pot[e.to] < 0) { + ok = false; + break; + } + } + if (ok) { + expath(st); + } else { + vector deg(n, 0); + for (int i = 0; i < n; i++) { + for (int eid : g[i]) { + auto& e = edges[eid]; + if (e.c - e.f > eps) { + deg[e.to] += 1; + } + } + } + vector que; + for (int i = 0; i < n; i++) { + if (deg[i] == 0) { + que.push_back(i); + } + } + for (int b = 0; b < (int) que.size(); b++) { + for (int eid : g[que[b]]) { + auto& e = edges[eid]; + if (e.c - e.f > eps) { + deg[e.to] -= 1; + if (deg[e.to] == 0) { + que.push_back(e.to); + } + } + } + } + fill(pot.begin(), pot.end(), INF_C); + pot[st] = 0; + if (static_cast(que.size()) == n) { + for (int v : que) { + if (pot[v] < INF_C) { + for (int eid : g[v]) { + auto& e = edges[eid]; + if (e.c - e.f > eps) { + if (pot[v] + e.cost < pot[e.to]) { + pot[e.to] = pot[v] + e.cost; + pe[e.to] = eid; + } + } + } + } + } + } else { + que.assign(1, st); + vector in_queue(n, false); + in_queue[st] = true; + for (int b = 0; b < (int) que.size(); b++) { + int i = que[b]; + in_queue[i] = false; + for (int id : g[i]) { + const edge &e = edges[id]; + if (e.c - e.f > eps && pot[i] + e.cost < pot[e.to]) { + pot[e.to] = pot[i] + e.cost; + pe[e.to] = id; + if (!in_queue[e.to]) { + que.push_back(e.to); + in_queue[e.to] = true; + } + } + } + } + } + } + while (pot[fin] < INF_C) { + T push = numeric_limits::max(); + int v = fin; + while (v != st) { + const edge &e = edges[pe[v]]; + push = min(push, e.c - e.f); + v = e.from; + } + v = fin; + while (v != st) { + edge &e = edges[pe[v]]; + e.f += push; + edge &back = edges[pe[v] ^ 1]; + back.f -= push; + v = e.from; + } + flow += push; + cost += push * pot[fin]; + expath(st); + } + return {flow, cost}; + } +}; + +int main() { + ios::sync_with_stdio(false); + cin.tie(0); + int n; + cin >> n; + vector a(n); + for (int i = 0; i < n; i++) { + cin >> a[i]; + } + MCMF g(2 * n + 3); + for (int i = 0; i < n; i++) { + g.add(2 * i, 2 * i + 1, 1, 0, -1); + g.add(2 * n + 1, 2 * i, 1, 0, 0); + g.add(2 * i + 1, 2 * n + 2, 1, 0, 0); + } + g.add(2 * n, 2 * n + 1, 4, 0, 0); + for (int i = 0; i < n; i++) { + for (int j = i + 1; j < n; j++) { + if (abs(a[i] - a[j]) == 1 || a[i] % 7 == a[j] % 7) { + g.add(2 * i + 1, 2 * j, 1, 0, 0); + } + } + } + cout << -g.max_flow_min_cost(2 * n, 2 * n + 2).second << '\n'; + return 0; +} + diff --git a/main.py b/main.py index d8af913..5cb7f17 100644 --- a/main.py +++ b/main.py @@ -2,6 +2,7 @@ from lxml import html from time import sleep # to reduce the number of requests per second import json +import sys # see start and end pages and specify in range @@ -49,12 +50,27 @@ def returnAcceptedSubmissions(Username, number_of_pages): code = tree2.xpath(address) - lines = code[0].split("\r\n") + lines = [code[0]] #saving the code in a outfile - save_data(lines, res[i][4]) + print_data(str(lines[0]), res[i][4]) + #for i in lines: + #print(str(lines[0])) sleep(5) +def uprint(*objects,file, sep=' ', end='\n'): + enc = file.encoding + if enc == 'UTF-8': + print(*objects, sep=sep, end=end, file=file) + else: + f = lambda obj: str(obj).encode(enc, errors='backslashreplace').decode(enc) + print(*map(f, objects), sep=sep, end=end, file=file) + +def print_data(sub_data, file_name): + open_file = open(file_name, 'w') + #for line in sub_data: + uprint(sub_data, file = open_file) + open_file.close() def save_data(sub_data, file_name): @@ -64,8 +80,8 @@ def save_data(sub_data, file_name): -Username = input("Enter the username: ") #Username -Number_of_pages = int(input("Enter the number of pages: ")) #specify the total number of pages to inspect. Should be less than or equal to the total number. +#Username = input("Enter the username: ") #Username +#Number_of_pages = int(input("Enter the number of pages: ")) #specify the total number of pages to inspect. Should be less than or equal to the total number. #running the program -returnAcceptedSubmissions(Username,Number_of_pages) +#returnAcceptedSubmissions(Username,Number_of_pages)