code_file1
stringlengths
80
4k
code_file2
stringlengths
91
4k
similar_or_different
int64
0
1
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long LL; LL n,d; LL col[2][500005] = {0}; LL v[4][500005],cnt[4] = {0}; LL id(LL x,LL y){ return x * n + y + 1; } // (x,y) -> id LL getx(LL x){ return (x - 1) / n; } // id(x,y) -> x LL gety(LL y){ return (y - 1) % n; } // id(x,y) -> y void dfs(LL u,LL idd){ LL tx = getx(u),ty = gety(u); for(LL dy,dx = 0;dx <= n;dx ++){ if(dx * dx > d) continue; dy = sqrt(d - dx * dx); if(dx * dx + dy * dy != d) continue; if(tx + dx < n && ty + dy < n) if(col[idd][id(tx + dx,ty + dy)] == -1) { col[idd][id(tx + dx,ty + dy)] = 1 - col[idd][u]; dfs(id(tx + dx,ty + dy),idd); } if(tx + dx < n && ty - dy >= 0) if(col[idd][id(tx + dx,ty - dy)] == -1) { col[idd][id(tx + dx,ty - dy)] = 1 - col[idd][u]; dfs(id(tx + dx,ty - dy),idd); } if(tx - dx >= 0 && ty + dy < n) if(col[idd][id(tx - dx,ty + dy)] == -1) { col[idd][id(tx - dx,ty + dy)] = 1 - col[idd][u]; dfs(id(tx - dx,ty + dy),idd); } if(tx - dx >= 0 && ty - dy >= 0) if(col[idd][id(tx - dx,ty - dy)] == -1) { col[idd][id(tx - dx,ty - dy)] = 1 - col[idd][u]; dfs(id(tx - dx,ty - dy),idd); } } } int main(){ memset(col,-1,sizeof(col)); cin >> n; n <<= 1; cin >> d; for(LL i = 0;i < n;i ++) for(LL j = 0;j < n;j ++) if(col[0][id(i,j)] == -1){ col[0][id(i,j)] = 1; dfs(id(i,j),0); } cin >> d; for(LL i = 0;i < n;i ++) for(LL j = 0;j < n;j ++) if(col[1][id(i,j)] == -1){ col[1][id(i,j)] = 1; dfs(id(i,j),1); } /* for(LL i = 0;i < n;i ++) for(LL j = 0;j < n;j ++) cout << col[0][id(i,j)] << (j == n - 1 ? '\n' : ' '); for(LL i = 0;i < n;i ++) for(LL j = 0;j < n;j ++) cout << col[1][id(i,j)] << (j == n - 1 ? '\n' : ' '); */ for(LL i = 1;i <= id(n - 1,n - 1);i ++){ LL co = col[0][i] + col[1][i] * 2; v[co][++ cnt[co]] = i; } for(LL i = 0;i < 4;i ++){ if(cnt[i] >= n * n / 4){ for(LL j = 1;j <= n * n / 4;j ++) cout << getx(v[i][j]) << ' ' << gety(v[i][j]) << '\n'; break; } } return 0; }
#include <iostream> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> #include <string> #include <cmath> #include <cstdio> #include <iomanip> #include <fstream> #include <cassert> #include <cstring> #include <unordered_set> #include <unordered_map> #include <numeric> #include <ctime> #include <bitset> #include <complex> #include <chrono> #include <random> #include <functional> using namespace std; #define int long long typedef long double ld; const ld PI = acos(-1); struct Point { int x, y; Point() {} Point(int x_, int y_) { x = x_; y = y_; } Point operator-(const Point &other) const { return Point(x - other.x, y - other.y); } bool operator<(const Point &other) const { return make_pair(x, y) < make_pair(other.x, other.y); } int operator*(const Point &other) const { return x * other.y - y * other.x; } int operator%(const Point &other) const { return x * other.x + y * other.y; } int dist(const Point &other) const { int dx = x - other.x; int dy = y - other.y; return dx * dx + dy * dy; } }; ld ag(const Point &a, const Point &b, const Point &c) { Point f = (a - b); Point s = (c - b); ld ans = atan2(f * s, f % s); if (ans < 0) ans += PI; return ans; } vector<Point> convex_hull(vector<Point> a) { int mn = min_element(a.begin(), a.end()) - a.begin(); Point to = a[mn]; a.erase(a.begin() + mn); auto cmp = [&](const Point &f, const Point &s) { Point ff = (f - to); Point ss = (s - to); if (ff * ss != 0) { return ff * ss > 0; } else { return to.dist(f) < to.dist(s); } }; auto ok_ang = [&](const Point &f, const Point &s, const Point &t) { Point ff = (s - f); Point ss = (t - s); return ff * ss > 0; }; sort(a.begin(), a.end(), cmp); vector<Point> st = {to}; for (auto t : a) { while (st.size() > 1 && !ok_ang(st.rbegin()[1], st.rbegin()[0], t)) { st.pop_back(); } st.push_back(t); } return st; } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<Point> a(n); vector<ld> ans(n); map<Point, int> ind; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; ind[a[i]] = i; } a = convex_hull(a); n = (int)a.size(); for (int i = 0; i < n; i++) { ld c = ag(a[(i - 1 + n) % n], a[i], a[(i + 1) % n]); if (n == 2) c = PI; ans[ind[a[i]]] = c / (2 * PI); } cout << fixed << setprecision(16); for (auto t : ans) { cout << t << '\n'; } }
0
#include <bits/stdc++.h> #define ll long long int #define yorn(f) cout<<((f)?"Yes":"No")<<endl; #define YORN(f) cout<<((f)?"YES":"NO")<<endl; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define put(x) cout << x << endl; using namespace std; int main() { ll x; cin >> x; put(1000 * (x / 500) + 5 * ((x % 500) / 5)) return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using ll=unsigned long long; using namespace std; int main() { string s; cin>>s; int minv=1000000005; size_t l=s.length(); rep(i,l-2){ string ss=s.substr(i,3); int num=stoi(ss); minv=min(minv,abs(num-753)); } cout<<minv<<"\n"; return 0; }
0
#include <map> #include <cstdio> #include <string> #include <iostream> #include <algorithm> typedef long long ll; int n; char s[40]; std::map<std::pair<std::string,std::string>,int> C; int main() { scanf("%d%s",&n,s); std::reverse(s+n,s+(n<<1)); for(int i=0;i<(1<<n);++i) { std::string a,b; for(int j=0;j<n;++j) if(i>>j&1)a+=s[j]; else b+=s[j]; ++C[std::make_pair(a,b)]; } ll Ans=0; for(int i=0;i<(1<<n);++i) { std::string a,b; for(int j=0;j<n;++j) if(i>>j&1)a+=s[n+j]; else b+=s[n+j]; Ans+=C[std::make_pair(a,b)]; } printf("%lld\n",Ans); return 0; }
#include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <string> #include <math.h> #include <iomanip> #include <limits> #include <list> #include <queue> #include <tuple> #include <map> #include <stack> #include <set> #include <bitset> #include <functional> using namespace std; #define fast_io ios_base::sync_with_stdio (false) ; cin.tie(0) ; cout.tie(0) ; #define ll long long int #define rep(i,n) for(int i=0; i<(int)(n); i++) #define reps(i,n) for(int i=1; i<=(int)(n); i++) #define REP(i,n) for(int i=n-1; i>=0; i--) #define REPS(i,n) for(int i=n; i>0; i--) #define MOD (long long int)(1e15+7) #define INF (int)(1123456789) #define LINF (long long int)(112345678901234567) #define chmax(a, b) a = (((a)<(b)) ? (b) : (a)) #define chmin(a, b) a = (((a)>(b)) ? (b) : (a)) #define all(v) v.begin(), v.end() typedef pair<int, int> Pii; typedef pair<ll, ll> Pll; ll mpow(ll a, ll b){ if(b==0) return 1; else if(b%2==0){ll memo = mpow(a,b/2); return memo*memo%MOD;} else return mpow(a,b-1) * a % MOD; } ll lpow(ll a, ll b){ if(b==0) return 1; else if(b%2==0){ll memo = lpow(a,b/2); return memo*memo;} else return lpow(a,b-1) * a; } ll gcd(ll a, ll b){ if(b==0) return a; else return gcd(b, a%b); } vector<ll> kaijo_memo; ll kaijo(ll n){ if(kaijo_memo.size() > n) return kaijo_memo[n]; if(kaijo_memo.size() == 0) kaijo_memo.push_back(1); while(kaijo_memo.size() <= n) kaijo_memo.push_back(kaijo_memo[kaijo_memo.size()-1] * kaijo_memo.size() % MOD); return kaijo_memo[n]; } vector<ll> gyaku_kaijo_memo; ll gyaku_kaijo(ll n){ if(gyaku_kaijo_memo.size() > n) return gyaku_kaijo_memo[n]; if(gyaku_kaijo_memo.size() == 0) gyaku_kaijo_memo.push_back(1); while(gyaku_kaijo_memo.size() <= n) gyaku_kaijo_memo.push_back(gyaku_kaijo_memo[gyaku_kaijo_memo.size()-1] * mpow(gyaku_kaijo_memo.size(), MOD-2) % MOD); return gyaku_kaijo_memo[n]; } ll nCr(ll n, ll r){ if(n == r) return 1;//0個の丸と-1個の棒みたいな時に時に効く?不安. if(n < r || r < 0) return 0; ll ret = 1; if(n <= 1e7){ ret *= kaijo(n); ret %= MOD; ret *= gyaku_kaijo(r); ret %= MOD; ret *= gyaku_kaijo(n-r); ret %= MOD; }else{ rep(i,r){ ret *= n-i; ret %= MOD; ret *= mpow(r-i, MOD-2); ret %= MOD; } } return ret; } int main(void){ fast_io cout<<fixed<<setprecision(15); ll n;cin>>n; string s="", t="", inp; cin>>inp; rep(i,n){ s += inp[i]; t += inp[2*n-1-i]; } ll ans = 0; rep(bit, 1<<n){ bitset<18> bs(bit); string x = "", y = ""; rep(i,n){ if(bs[i]){ x += s[i]; }else{ y += s[i]; } } rep(i,n){ x += "X"; y += "X"; } ll dp[20][20] = {}; dp[0][0] = 1; rep(i,n){ rep(j,n+1){ if(x[j] == t[i]){ dp[i+1][j+1] += dp[i][j]; dp[i+1][j+1] %= MOD; } if(i-j < 0) continue; if(y[(i-j)] == t[i]){ dp[i+1][j] += dp[i][j]; dp[i+1][j] %= MOD; } } } ans += dp[n][bs.count()]; /*if(dp[n][bs.count()] > 0 bit == 9){ rep(i,n){ cout<<bs[i]; } cout<<endl; cout<<x<<" "<<y<<endl; cout<<dp[2][0]<<" "<<x[0]<<" "<<t[2]<<endl; }*/ ans %= MOD; } cout<<ans<<endl; }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; typedef long long int ll; pair<ll,int> p[100005]; int main(){ ll n,x; cin >> n >> x; vector<ll> b(n),l(n),u(n); for(int i=0;i<n;i++){ cin >> b[i] >> l[i] >> u[i]; } ll sum=0; for(int i=0;i<n;i++){ p[i]=pair<ll,int>((x-b[i])*u[i]+b[i]*l[i],i); sum-=b[i]*l[i]; } sort(p,p+n); ll ret=0;//満点を取る教科を数える int last=0; for(int i=n-1;i>=0;i--){ if(sum+p[i].first>=0){ ret+=(ll)(n-i-1)*(ll)x;//満点を取る教科はn-i-1個である last=i+1; break; } sum+=p[i].first; } ll Min=x;//中途半端に解く科目の得点の最小値 sum=-sum; for(int i=0;i<last;i++){ int v=p[i].second; Min=min(Min,(sum+l[v]-1)/l[v]);//中途半端に解く科目に最小の重みをつけた場合 ll nsum=sum+b[v]*(u[v]-l[v]);//最大の重みをつける場合には更にディスアドバンテージが広がる Min=min(Min,(nsum+u[v]-1)/u[v]); } //貪欲に見ると満点を取る科目になる科目のうち中途半端に解くものを探索する場合、 //満点を取る科目としてi=last-1も追加される for(int i=last;i<n;i++){ int v=p[i].second; ll nsum=sum+p[i].first-p[last-1].first; Min=min(Min,(nsum+l[v]-1)/l[v]); ll nsum2=nsum+b[v]*(u[v]-l[v]); Min=min(Min,(nsum2+u[v]-1)/u[v]); } cout << ret+Min << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,x,y) for(ll i=(x);i<(y);i++) #define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--) #define all(x) (x).begin(),(x).end() #define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");} #ifdef LOCAL #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl #define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl #define dassert(...) assert(__VA_ARGS__) #else #define debug(x) #define itrdebug(x) #define dassert(...) #endif //#define int long long typedef long long ll; const ll MOD = 1e9 + 7; const long double EPS = 1e-8; void solve(long long N, std::vector<long long> X, std::vector<long long> Y, std::vector<int> U){ const int MAX_LEN = 2e5; const int OFFSET = 2e5; int answer = INT_MAX; rep(u,0,4) { vector<set<int>> lineL(MAX_LEN+1); // lineL[y when x is 0] = all x whose direction is u+2 vector<set<int>> lineD(2*MAX_LEN+1); // lineD[y when x is 0] = all x whose direction is u+1 rep(i,0,N) { if (U[i] == (u+2) % 4) { lineL[Y[i]].insert(X[i]); } else if (U[i] == (u+1) % 4) { lineD[OFFSET + Y[i]-X[i]].insert(X[i]); } } rep(i,0,N) { if (U[i] != u) continue; // lineL auto itr = lineL[Y[i]].lower_bound(X[i]); if (itr != lineL[Y[i]].end()) { answer = min<int>(answer, (*itr - X[i]) * 5); } // lineD itr = lineD[OFFSET + Y[i]-X[i]].lower_bound(X[i]); if (itr != lineD[OFFSET + Y[i]-X[i]].end()) { answer = min<int>(answer, (*itr - X[i]) * 10); } } // rotate to left rep(i,0,N) { ll x = MAX_LEN - Y[i], y = X[i]; X[i] = x; Y[i] = y; } } cout << (answer == INT_MAX ? "SAFE" : to_string(answer)) << endl; } signed main(){ // ios_base::sync_with_stdio(false); // cin.tie(NULL); long long N; scanf("%lld",&N); std::vector<long long> X(N); std::vector<long long> Y(N); std::vector<int> U(N); unordered_map<char, int> direction = {{'R', 0}, {'D', 1}, {'L', 2}, {'U', 3}}; for(int i = 0 ; i < N ; i++){ scanf("%lld",&X[i]); scanf("%lld",&Y[i]); char c; std::cin >> c; U[i] = direction[c]; } solve(N, std::move(X), std::move(Y), std::move(U)); return 0; }
0
#include <bits/stdc++.h> #include <boost/range/algorithm.hpp> #include <boost/range/numeric.hpp> #include <boost/integer/common_factor.hpp> #include <boost/integer/common_factor_rt.hpp> #include <boost/multiprecision/cpp_dec_float.hpp> #include <boost/multiprecision/cpp_int.hpp> using std::string; using std::vector; using std::set; using std::multiset; using std::unordered_set; using std::map; using std::multimap; using std::unordered_map; using std::pair; using std::cin; using std::cout; using boost::multiprecision::cpp_int; using cpp_dec_float_1000 = boost::multiprecision::number<boost::multiprecision::cpp_dec_float<1000, boost::int128_type>>; typedef uintmax_t ull; typedef intmax_t ll; typedef uint64_t ul; typedef uint32_t ui; typedef uint8_t uc; constexpr char CRLF = '\n'; constexpr char SPACE = ' '; constexpr char VECTOR_COUT_SEPARATOR = SPACE; constexpr ll INF = 1000'000'007; constexpr int MOD = 1000'000'007; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> std::ostream& operator<< (std::ostream& os, const std::vector<T>& vc) { for(auto it = vc.begin(); it != vc.end(); ++it) { if (std::next(it) == vc.end()) os << *it; else os << *it << VECTOR_COUT_SEPARATOR; } return os; } template<class T1, class T2> inline std::ostream & operator<< (std::ostream & os, const std::pair<T1, T2> & p) { return os << p.first << ' ' << p.second; } template<class T> T modinv(T a, T m) { T b = m, u = 1, v = 0; while (b) { T t = a / b; a -= t * b; boost::swap(a, b); u -= t * v; boost::swap(u, v); } u %= m; if (u < 0) u += m; return u; } template<class T> inline bool isSosuu(const T& n) { if (n == T(1)) { return false; } for (T i = 2; i*i <= n; ++i) { if (n % i == 0) { return false; } } return true; } template<class T> vector<vector<T>> split (const vector<T>& S, T delim) { vector<vector<T>> ret{}; vector<T> buff{}; for (auto c : S) { if (c == delim) { if (!buff.empty()) ret.push_back(buff); buff.clear(); } else { buff.push_back(c); } } if (!buff.empty()) ret.push_back(buff); return ret; } template<class T> inline void printYesNo(const T& t) { cout << (t ? "Yes" : "No") << CRLF; } template<class T = int64_t> T modpow(T N, T P, T M) { T ret{1}; N %= M; while (P) { if (P & 0x01) { ret *= N; ret %= M; } P >>= 1; N *= N; N %= M; } return ret; } /** code for assert debug begin */ int gErr = 0; #define ASSERT(p) if(!(p)){ gErr = 1; }else{ ; } /** code for assert debug end */ struct Tmp { public: int N; Tmp(int N) : N(N) {} }; void solve(void) { int N; cin >> N; int d; cin >> d; if (d) { cout << 0 << CRLF; return; } vector<int> D(N+1); int max = 0; int min = N; for (int _ = 0; _ < N-1; ++_) { int d; cin >> d; ++D[d]; chmax(max, d); chmin(min, d); } if (min < 1) { cout << 0 << CRLF; return; } if (max < 1) { cout << 0 << CRLF; } ll res = 1; D[0] = 1; constexpr ll mod = 998244353; for (int i = 1; i <= max; ++i) { ll pre = D[i-1]; ll cur = D[i]; ll now = modpow(pre, cur, mod); res *= now; res %= mod; } cout << res << CRLF; return; } int main(void) { std::ios_base::sync_with_stdio(false); std::cin.tie(0); solve(); return 0; }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<long long> v(n, 0); bool fail = false; int tmp; cin >> tmp; if (tmp != 0) fail = true; for (int i = 1; i < n; i++) { cin >> tmp; if (tmp == 0) fail = true; v[tmp]++; } if (fail) cout << 0 << endl; else { long long ans = 1; for (int i = 1; i < n-1; i++) { for (int j = 0; j < v[i+1]; j++) { ans *= v[i]; ans %= 998244353; } } cout << ans << endl; } }
1
#include <bits/stdc++.h> using namespace std; //#define cerr if (false) cerr #define db(x) cerr << #x << "=" << x << endl #define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl #define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl #define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl #define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl template <typename A, typename B> ostream& operator<<(ostream& os, const pair<A, B>& x) { return os << "(" << x.first << "," << x.second << ")"; } typedef long long ll; typedef long double ld; short S[105][105]; struct Rect { int x1, x2, y1, y2; inline bool valid() { return x2 >= x1 && y2 >= y1; } inline short gain() { return S[y2][x2] - S[y1 - 1][x2] - S[y2][x1 - 1] + S[y1 - 1][x1 - 1]; } }; Rect inter(const Rect& a, const Rect& b) { return {max(a.x1, b.x1), min(a.x2, b.x2), max(a.y1, b.y1), min(a.y2, b.y2)}; } char G[105][105]; int H, W; int ex, ey; short dp[101][101][101][101]; short f(int x1, int x2, int y1, int y2) { short& ret = dp[x1][x2][y1][y2]; if (ret != -1) return ret; int l = x2 - ex + 1; int r = x1 + (W - ex); int u = y2 - ey + 1; int d = y1 + (H - ey); ret = 0; Rect allow{l, r, u, d}; Rect slice; slice = inter({x1 - 1, x1 - 1, y1, y2}, allow); if (slice.valid()) { ret = max((int)ret, f(x1 - 1, x2, y1, y2) + slice.gain()); } slice = inter({x2 + 1, x2 + 1, y1, y2}, allow); if (slice.valid()) { ret = max((int)ret, f(x1, x2 + 1, y1, y2) + slice.gain()); } slice = inter({x1, x2, y1 - 1, y1 - 1}, allow); if (slice.valid()) { ret = max((int)ret, f(x1, x2, y1 - 1, y2) + slice.gain()); } slice = inter({x1, x2, y2 + 1, y2 + 1}, allow); if (slice.valid()) { ret = max((int)ret, f(x1, x2, y1, y2 + 1) + slice.gain()); } return ret; } int main() { scanf("%d%d", &H, &W); for (int i = 0; i < H; ++i) { scanf("%s", G[i]); for (int j = 0; j < W; ++j) { S[i + 1][j + 1] = S[i + 1][j] + S[i][j + 1] - S[i][j] + (G[i][j] == 'o'); if (G[i][j] == 'E') { ex = j + 1; ey = i + 1; } } } memset(dp, -1, sizeof(dp)); int ans = f(ex, ex, ey, ey); printf("%d\n", ans); }
#include<stdio.h> #include<queue> #include<vector> #include<iostream> #include<algorithm> using namespace std; int main() { int n,k,i,a,b,c,f[1000],sum_max=-999999999,sum; scanf("%d%d",&n,&k); for(i=0;i<n;i++) { scanf("%d",&f[i]); } for(a=0;a<=k;a++) { for(b=0;b<=k;b++) { for(c=0;c<=k;c++) { priority_queue<int>q; if(a+b+c<=k&&a+b>=c&&a+b<=n) { for(i=0;i<a;i++) { q.push(f[i]); } for(i=0;i<b;i++) { q.push(f[n-i-1]); } int sum=0; for(i=0;i<a+b-c;i++) { sum=sum+q.top(); q.pop(); } sum_max=max(sum_max,sum); } } } } printf("%d",sum_max); return 0; }
0
#include<algorithm> #include<iostream> #include<string> #include<vector> #include<cmath> #include<stack> using namespace std; int main(){ char x[52]; for(int i='a';i<='z';i++){ x[i-'a']=i; } for(int i='A';i<='Z';i++){ x[26+i-'A']=i; } int n; while(cin>>n,n!=0){ int k[n]; string s; for(int i=0;i<n;i++){ cin>>k[i]; } cin>>s; for(int i=0;i<s.size();i++){ int b=distance(x,find(x,x+52,s[i])); int c=(b-k[i%n]+52)%52; cout<<x[c]; } cout<<endl; } return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <functional> #include <cmath> #include <map> #define reps(i,s,n) for(int (i) = (s); (i) < (n); (i)++) #define rep(i,n) reps(i,0,n) using namespace std; using ll = long long; int dp[3][100010]; int main(){ rep(i,3){ rep(j,100010){ dp[i][j] = 0; } } int n; int tmp ; ll ans = 1; ll mod = 1e9+7; cin >> n; vector<int> vec(n); rep(j,n){ cin >> tmp; vec[j] = tmp; rep(i,3){ dp[i][j+1] = dp[i][j]; } rep(i,3){ if(dp[i][j+1] == tmp) { dp[i][j+1] = tmp+1; break; } } } rep(j,n){ ll cnt = 0; rep(i, 3){ if(dp[i][j] == vec[j]){ cnt++; } } ans = (ans*cnt) % mod; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i, m, n) for(int(i) = (int)(m); i < (int)(n); ++i) #define rep2(i, m, n) for(int(i) = (int)(n)-1; i >= (int)(m); --i) #define REP(i, n) rep(i, 0, n) #define REP2(i, n) rep2(i, 0, n) #define all(hoge) (hoge).begin(), (hoge).end() #define en '\n' using ll = long long; using ull = unsigned long long; template <class T> using vec = vector<T>; template <class T> using vvec = vector<vec<T>>; typedef pair<ll, ll> P; constexpr long long INF = 1LL << 60; constexpr int INF_INT = 1 << 25; constexpr long long MOD = (ll)1e9 + 7; //constexpr long long MOD = 998244353LL; using ld = long double; static const ld pi = 3.141592653589793L; typedef vector<ll> Array; typedef vector<Array> Matrix; template <class T> inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } template <class T> inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } struct Edge { ll to, rev; long double cap; Edge(ll _to, long double _cap, ll _rev) { to = _to; cap = _cap; rev = _rev; } }; using Edges = vector<Edge>; using Graph = vector<Edges>; void add_edge(Graph &G, ll from, ll to, long double cap, bool revFlag, long double revCap) { G[from].push_back(Edge(to, cap, (ll)G[to].size())); if(revFlag) G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1)); } void solve() { ll n, w, h; cin >> n >> h >> w; cout << (n - w + 1) * (n - h + 1) << en; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); /* ll t; cin >> t; REP(i, t) solve(); */ return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,c,p,q,r; cin >> a >> b >> c; p = a+b; q = a+c; r = b+c; if(p>=q){ if(q >= r){ cout << r; }else{ cout << q; } }else{ if(p >= r){ cout << r; }else{ cout << p; } } }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define agewari(a, b) ((ll)a + ((ll)b - 1)) / b const int MOD = 1000000007; const long long INF = 1LL << 60; using Graph = vector<vector<ll>>; int main() { ll n, m; cin >> n >> m; ll maL = -1; ll miR = n + 10; rep(i, m) { ll l, r; cin >> l >> r; maL = max(maL, l); miR = min(miR, r); } ll ans = max((ll)0, miR - maL + 1); cout << ans << endl; }
#include <iostream> #include <string> #include <vector> #include <utility> #include <algorithm> #include <set> #include <map> #include <stack> #include <cassert> #include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> using namespace std; typedef unsigned long long ull; typedef long long ll; template<typename t1, class t2> void input(t1 count,t2 array) { for(t1 i = 0;i < count;i++) { cin >> array[i]; } } template<typename t1, class t2> void output(t1 count,t2 array) { for(t1 i = 0;i < count;i++) { cout << array[i]; if(i != count - 1) cout << " "; } cout << endl; } int n,s[10000],t[500],q,c; bool exists(int x) { for(int i = 0;i < n;i++) if(s[i] == x) return true; return false; } int main(int argc,char** argv) { cin >> n; input(n,s); cin >> q; input(q,t); for(int i = 0;i < q;i++) { if(exists(t[i]))c++; } cout << c << endl; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define agewari(a, b) ((ll)a + ((ll)b - 1)) / b const int MOD = 1000000007; const long long INF = 1LL << 60; using Graph = vector<vector<ll>>; Graph G; //2部グラフ判定 vector<ll> color; //1と-1に塗り分ける bool ok = true; int dfs(ll v, ll c) { color[v] = c; rep(i, G[v].size()) { if (color[G[v][i]] == 0) dfs(G[v][i], -c); else if (color[G[v][i]] == c) ok = false; } return 0; } int main() { ll n, m; cin >> n >> m; G.resize(n); color.resize(n); rep(i, m) { ll a, b; cin >> a >> b; a--; b--; G[a].push_back(b); G[b].push_back(a); } dfs(0, 1); ll b = 0; rep(i, n) if (color[i] == 1) b++; if (ok) cout << b * (n - b) - m << endl; else cout << n * (n - 1) / 2 - m << endl; }
#include<bits/stdc++.h> #define ll long long #define ld long double #define pb push_back #define vl vector #define ff first #define ss second using namespace std; int main() { ll a,b,i,j,n,t,c,m; cin>>n>>m; ll l,r; ll arr[n+1]={0}; for(i=0;i<m;i++) { cin>>l>>r; l--; arr[l]++; arr[r]--; } for(i=1;i<n+1;i++) arr[i]+=arr[i-1]; ll count=0; for(i=0;i<n+1;i++) if(arr[i]==m) count++; cout<<count; }
0
#include <stdio.h> int check(int a,int b,int c) { //printf("[%d][%d][%d]\n",a,b,c); if(a != 14)return 0; else if(b == 23)return 1; else if(b == 1 && c == 10)return 1; else if(b == 19 && c == 19)return 1; else return 0; } int main() { int longth,mem = 0; while(1) { char str[90] = {0}; int mem1[90] = {0},mem2[90] = {0}; if(scanf("%c",&str[0]) == -1)return 0; if(str[0] >= 'a' && 'z' >= str[0])mem1[0] = str[0] - 'a' + 1; for(int i = 1; i < 80; i++) { scanf("%c",&str[i]); if(str[i] == '\n')break; longth = i; if(str[i] >= 'a' && 'z' >= str[i])mem1[i] = str[i] - 'a' + 1; } for(int i = 0; i < longth; i++) { if(mem1[i] == 0); else if(mem1[i + 1] != 0) { if(mem1[i] <= mem1[i + 1])mem2[i] = mem1[i + 1] - mem1[i]; else mem2[i] = 26 - mem1[i] + mem1[i + 1]; } } /*for(int i = 0; i < longth; i++) { printf("[%d]",mem2[i]); } printf("\n");*/ for(int i = 0; i < longth; i++) { if(mem2[i] != 0)/*printf("[%d]\n",i);*/if(check(mem2[i],mem2[i + 1],mem2[i + 2]))if(mem1[i] <= 20)mem = 20 - mem1[i];else mem = 26 - mem1[i] + 20; //else printf("no\n"); } //printf("{{%d}}\n",mem); for(int i = 0; i < longth + 1; i++) { if(str[i] >= 'a' && 'z' >= str[i])printf("%c",(str[i] - 'a' + mem) % 26 + 'a'); else printf("%c",str[i]); } printf("\n"); } }
#include <cstdio> #include <cstdlib> #include <cstdint> #include <cstring> #include <cctype> #include <iostream> #include <sstream> #include <vector> #include <list> #include <deque> #include <map> #include <set> #include <stack> #include <queue> #include <unordered_map> #include <unordered_set> #include <algorithm> #include <functional> #include <complex> using namespace std; typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef long long ll; const double PI = 3.141592653589793238462643383279502884L; const int LARGE_PRIME = 1000000007; #define all(c) (c).begin(), (c).end() #define rall(c) (c).rbegin(), (c).rend() #define tr(c, i) for(auto i = (c).begin(); i != (c).end(); i++) #define rtr(c, i) for(auto i = (c).rbegin(); i != (c).rend(); i++) #define repfn(i,a,cont,next) \ for(auto i = (a); [=](){return(cont);}(); i = [=](){return(next);}()) #define repby(i,a,b,s) repfn(i,a,i<b,i+s) #define repab(i,a,b) repby(i,a,b,1) #define rep(i,b) repab(i,0,b) #define pb push_back #define sz(c) int((c).size()) char rot13(char c, int n) { return 'a' + (c + n) % 26; } string rot13(string &s, int n) { n %= 26; string res; tr (s, p) { if (islower(*p)) res.pb(rot13(*p, n)); else res.pb(*p); } return res; } int main(int argc, char **argv) { string enc; while (!getline(cin, enc).eof()) { for (int i = 0; i < 26; i++) { string s = rot13(enc, i); if ((int)s.find("the ") >= 0 || (int)s.find("this ") >= 0 || (int)s.find("that ") >= 0) { cout << s << endl; break; } } } return 0; }
1
//Author:xht37 #include <bits/stdc++.h> #define ui unsigned int #define ll long long #define ul unsigned ll #define ld long double #define pi pair <int, int> #define fi first #define se second #define mp make_pair #define ls (p << 1) #define rs (ls | 1) #define md ((t[p].l + t[p].r) >> 1) #define vi vector <int> #define pb push_back #define pq priority_queue #define dbg(x) cerr << #x" = " << x << endl #define debug(...) fprintf(stderr, __VA_ARGS__) #define fl(x) freopen(x".in", "r", stdin), freopen(x".out", "w", stdout) using namespace std; namespace io { const int SI = 1 << 21 | 1; char IB[SI], *IS, *IT, OB[SI], *OS = OB, *OT = OS + SI - 1, c, ch[100]; int f, t; #define gc() (IS == IT ? (IT = (IS = IB) + fread(IB, 1, SI, stdin), IS == IT ? EOF : *IS++) : *IS++) inline void flush() { fwrite(OB, 1, OS - OB, stdout), OS = OB; } inline void pc(char x) { *OS++ = x; if (OS == OT) flush(); } template <class I> inline void rd(I &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + (c & 15), c = gc()); x *= f; } template <class I> inline void rd(I &x, I &y) { rd(x), rd(y); } template <class I> inline void rd(I &x, I &y, I &z) { rd(x), rd(y), rd(z); } template <class I> inline void rda(I *a, int n) { for (int i = 1; i <= n; i++) rd(a[i]); } inline void rdc(char &c) { for (c = gc(); c < 33 || c > 126; c = gc()); } inline void rds(char *s, int &n) { for (c = gc(); c < 33 || c > 126; c = gc()); for (n = 0; c >= 33 && c <= 126; s[++n] = c, c = gc()); s[n+1] = '\0'; } inline void rds(string &s) { for (c = gc(); c < 33 || c > 126; c = gc()); for (s.clear(); c >= 33 && c <= 126; s.pb(c), c = gc()); } template <class I> inline void print(I x, char k = '\n') { if (!x) pc('0'); if (x < 0) pc('-'), x = -x; while (x) ch[++t] = x % 10 + '0', x /= 10; while (t) pc(ch[t--]); pc(k); } template <class I> inline void print(I x, I y) { print(x, ' '), print(y); } template <class I> inline void print(I x, I y, I z) { print(x, ' '), print(y, ' '), print(z); } template <class I> inline void printa(I *a, int n) { for (int i = 1; i <= n; i++) print(a[i], " \n"[i==n]); } inline void printc(char c) { pc(c); } inline void prints(char *s, int n) { for (int i = 1; i <= n; i++) pc(s[i]); pc('\n'); } inline void prints(string s) { int n = s.length(); while (t < n) pc(s[t++]); pc('\n'), t = 0; } struct Flush { ~Flush() { flush(); } } flusher; } using io::rd; using io::rda; using io::rdc; using io::rds; using io::print; using io::printa; using io::printc; using io::prints; const int N = 1e5 + 7; int n; ll s, x[N], p[N]; ll f(int l, int r, int o) { if (s < x[l] || x[r] < s) return abs(s - x[o]); if (p[l] >= p[r]) return p[l] += p[r], f(l, r - 1, l) + abs(x[l] - x[o]); return p[r] += p[l], f(l + 1, r, r) + abs(x[r] - x[o]); } int main() { rd(n), rd(s); for (int i = 1; i <= n; i++) rd(x[i], p[i]); print(f(1, n, s < x[1] ? n : x[n] < s ? 1 : p[1] >= p[n] ? n : 1)); return 0; }
#include<bits/stdc++.h> using namespace std; const int N = 1e5 + 11; #define LL long long int n; LL ans, s, x[N]; LL a[N]; LL sol(int l, int r){ //printf("l=%d r=%d al=%lld ar=%lld\n", l, r, a[l], a[r]); if(x[l] > s){ ans += x[r] - s; return x[r]; } if(x[r] < s){ ans += s - x[l]; return x[l]; } if(a[l] >= a[r]){ a[l] += a[r]; ans += abs(x[r] - sol(l, r - 1)); return x[r]; } else{ a[r] += a[l]; ans += abs(x[l] - sol(l + 1, r)); return x[l]; } } int main(){ cin>>n>>s; for(int i = 1;i <= n; i++){ scanf("%lld%lld", &x[i], &a[i]); } sol(1, n); cout<<ans<<endl; return 0; }
1
#include <bits/stdc++.h> #include <atcoder/fenwicktree> using namespace atcoder; using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<int, ll> pil; typedef pair<ll, int> pli; typedef pair<double,double> pdd; #define SQ(i) ((i)*(i)) #define MEM(a, b) memset(a, (b), sizeof(a)) #define SZ(i) int(i.size()) #define FOR(i, j, k, in) for (int i=j ; i<(k) ; i+=in) #define RFOR(i, j, k, in) for (int i=j ; i>=(k) ; i-=in) #define REP(i, j) FOR(i, 0, j, 1) #define REP1(i,j) FOR(i, 1, j+1, 1) #define RREP(i, j) RFOR(i, j, 0, 1) #define ALL(_a) _a.begin(),_a.end() #define mp make_pair #define pb push_back #define eb emplace_back #define X first #define Y second #ifdef jayinnn #define TIME(i) Timer i(#i) #define debug(...) do{\ fprintf(stderr,"%s - %d (%s) = ",__PRETTY_FUNCTION__,__LINE__,#__VA_ARGS__);\ _do(__VA_ARGS__);\ }while(0) template<typename T>void _do(T &&_x){cerr<<_x<<endl;} template<typename T,typename ...S> void _do(T &&_x,S &&..._t){cerr<<_x<<", ";_do(_t...);} template<typename _a,typename _b> ostream& operator << (ostream &_s,const pair<_a,_b> &_p){return _s<<"("<<_p.X<<","<<_p.Y<<")";} template<typename It> ostream& _OUTC(ostream &_s,It _ita,It _itb) { _s<<"{"; for(It _it=_ita;_it!=_itb;_it++) { _s<<(_it==_ita?"":",")<<*_it; } _s<<"}"; return _s; } template<typename _a> ostream &operator << (ostream &_s,vector<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a> ostream &operator << (ostream &_s,set<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a> ostream &operator << (ostream &_s,deque<_a> &_c){return _OUTC(_s,ALL(_c));} template<typename _a,typename _b> ostream &operator << (ostream &_s,map<_a,_b> &_c){return _OUTC(_s,ALL(_c));} template<typename _t> void pary(_t _a,_t _b){_OUTC(cerr,_a,_b);cerr<<endl;} #define IOS() class Timer { private: string scope_name; chrono::high_resolution_clock::time_point start_time; public: Timer (string name) : scope_name(name) { start_time = chrono::high_resolution_clock::now(); } ~Timer () { auto stop_time = chrono::high_resolution_clock::now(); auto length = chrono::duration_cast<chrono::microseconds>(stop_time - start_time).count(); double mlength = double(length) * 0.001; debug(scope_name, mlength); } }; #else #define TIME(i) #define debug(...) #define pary(...) #define endl '\n' #define IOS() ios_base::sync_with_stdio(0);cin.tie(0) #endif const ll MOD = 1000000007; const ll INF = 0x3f3f3f3f3f3f3f3f; const int iNF = 0x3f3f3f3f; const ll MAXN = 100005; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); /********** Good Luck :) **********/ int main () { TIME(main); IOS(); int n, q; cin >> n >> q; fenwick_tree<ll> bit(n); for(int i=0;i<n;i++){ int p; cin >> p; bit.add(i, p); } while(q--){ int t; cin >> t; if(t == 0){ int p, x; cin >> p >> x; bit.add(p, x); } else { int l, r; cin >> l >> r; cout << bit.sum(l, r) << endl; } } return 0; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)n;i++) #define Rep(i,s,f) for(int i=(int)s;i<(int)f;i++) using ll=long long; using namespace std; int bfs(vector<string> A,int H,int W) { queue<pair<int,int>> que; vector<vector<int>> cost(H,vector<int>(W,-1)); int lp=H*W; rep(i,H) { rep(j,W) { if(A[i][j]=='#') { que.push(make_pair(j,i)); cost[i][j]=0; lp--; } } } int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}}; int ret=0; while(lp!=0) { int n=que.size(); rep(i,n) { pair<int,int> p=que.front();que.pop(); for(auto d : dir) { int x=d[0]+p.first; int y=d[1]+p.second; if(0>x||x>=W||0>y||y>=H||cost[y][x]!=-1) continue; cost[y][x]=0; que.push(make_pair(x,y)); } } lp-=que.size(); ret++; } return ret; } int main() { int H,W; cin>>H>>W; vector<string> A(H); rep(i,H) { cin>>A[i]; } cout<<bfs(A,H,W)<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int N; int out=0; cin>>N; N*=3; string str; int ans=0; int run=0; while(out<N){ cin>>str; if(str=="HIT"){ run++; if(run>=4){ ans++; run--; } } else if(str=="OUT"){ out++; if(out%3==0){ cout<<ans<<endl; ans=0; run=0; } } else if(str=="HOMERUN"){ run++; ans+=run; run=0; } } return 0; }
#include <iostream> #include <cstring> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int score = 0, out = 0; int bases[3] = {}; while (true) { char event[8]; cin >> event; if (strcmp(event, "OUT") == 0) { out++; if (out == 3) { break; } } else if (strcmp(event, "HIT") == 0) { score += bases[2]; bases[2] = bases[1]; bases[1] = bases[0]; bases[0] = 1; } else if (strcmp(event, "HOMERUN") == 0) { score += bases[0] + bases[1] + bases[2] + 1; bases[0] = bases[1] = bases[2] = 0; } } cout << score << endl; } }
1
#include <bits/stdc++.h> using namespace std; #define int long long #define Rep(i, N) for(int i = 0; i < N; i++) #define Reps(i, x, N) for(int i = x; i < N; i++) const int LLINF = 1LL << 60; signed main() { int N, P; while(cin >> N >> P, N || P) { int w = P; int have[55] = {0}; int i = 0; while(true) { if(w) have[i]++, w--; else w += have[i], have[i] = 0; if(have[i] == P) { cout << i << endl; break; } i++; if(i == N) i = 0; } } return 0; }
/*** ?¬?????????? ***/ #include <bits/stdc++.h> using namespace std; int main(){ int n, p; int i; int count, size; int max, num; cin >> n >> p; while(n!=0 || p!=0){ vector<int> v; for(i=0; i<n; ++i) v.push_back(0); count=0; size=p; while(1){ for(i=size; 0<i; --i){ ++v[count]; count=(count+1)%n; } max=0; num=-1; for(i=0; i<n; ++i){ if(max<v[i]){ max=v[i]; num=i; } } if(max==p) break; while(v[count]==0) count=(count+1)%n; size=v[count]; v[count]=0; count=(count+1)%n; } cout << num << endl; cin >> n >> p; } return 0; }
1
#include <bits/stdc++.h> #include <iostream> using namespace std; #define EACH(i, a) for (auto &&i : a) #define FOR(i, a, b) for (int i = (int)a; i < (int)b; ++i) #define RFOR(i, a, b) for (int i = (int)b - 1; i >= (int)a; --i) #define REP(i, n) FOR(i, 0, n) #define REPS(i, n) FOR(i, 1, int(n) + 1) #define RREP(i, n) RFOR(i, 0, n) #define RREPS(i, n) RFOR(i, 1, int(n) + 1) #define ALL(x) (x).begin(), (x).end() #define MEMSET(v, h) memset((v), h, sizeof(v)) #define debug(x) cout << #x << ": " << x << endl #define pb push_back #define mp make_pair #define F first #define S second #define y0 y3487465 #define y1 y8687969 #define j0 j1347829 #define j1 j234892 #define next asdnext #define prev asdprev #define INF (1L << 30) #define MOD (1000000007) int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void pvp(T a, T b) { for (T i = a; i != b; ++i) cout << "(" << i->first << ", " << i->second << ") "; cout << endl; } template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; } template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; } int in() { int x; scanf("%d", &x); return x; } using ll = long long; #define int ll //********** signed main(void) { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; // A[i]: member i が j 番目に好きなスポーツ vector<int> A[301]; REP(i, n) { REP(j, m) { int sp; cin >> sp; A[i].pb(sp - 1); } } int best_members = 999; if (m == 1) { best_members = n; } REP(ii, m - 1) { int popular[301]; MEMSET(popular, 0); REP(i, n) { // popular[m]はm番目のスポーツの参加人数 popular[A[i][0]] += 1; } int max_sp = 0; int max_members = 0; REP(sp, m) { if (max_members < popular[sp]) { max_sp = sp; max_members = popular[sp]; } } // debug(max_sp + 1); // debug(max_members); REP(i, n) { // 最大人気スポーツを除外 A[i].erase(remove(ALL(A[i]), max_sp), A[i].end()); } if (best_members > max_members) { // もし最大人数が、これまでで最小だったら更新 best_members = max_members; } } cout << best_members << endl; return 0; };
#define C typedef long long readtype; /* Header {{{ */ #ifdef C #include <stdio.h> #include <math.h> #include <string.h> #define isdigit(a) ((a) >= '0' && (a) <= '9' ? true : false) template<typename type> type abs(type a) { return a < 0 ? -a : a; } template<typename type> type min(type a, type b) { return a < b ? a : b; } template<typename type> type beMin(type &a, type b) { return a = (a < b ? a : b); } template<typename type> type max(type a, type b) { return a > b ? a : b; } template<typename type> type beMax(type &a, type b) { return a = (a > b ? a : b); } template<typename type> void swap(type &a, type &b) { type c; c = a; a = b; b = c; } #endif #ifdef Cpp #include <bits/stdc++.h> using namespace std; #endif typedef long long var; readtype read() { readtype a = 0, c = getchar(), s = 0; while (!isdigit(c)) s |= c == '-', c = getchar(); while (isdigit(c)) a = a * 10 + c - 48, c = getchar(); return s ? -a : a; } /* }}} */ const int N = 311; int n, m; int a[N][N], have[N], pos[N]; bool ban[N]; bool check(int t); int main() { /* #ifndef ONLINE_JUDGE freopen("source.in", "r", stdin); freopen("source.out", "w", stdout); #endif */ n = read(), m = read(); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) a[i][j] = read(); } int l = 1, r = n; while (l < r) { int mid = (l + r) >> 1; if (!check(mid)) l = mid + 1; else r = mid; } printf("%d\n", l); return 0; } bool check(int t) { memset(ban, false, sizeof(ban)); memset(have, 0, sizeof(have)); memset(pos, 0, sizeof(pos)); for (int i = 1; i <= n; ++i) have[a[i][++pos[i]]]++; while (true) { bool ok = true; for (int i = 1; i <= m; ++i) { if (have[i] <= t) continue; ok = false; ban[i] = true; } if (ok) break; for (int i = 1; i <= n; ++i) { while (pos[i] <= m && ban[a[i][pos[i]]]) { have[a[i][pos[i]]]--; have[a[i][++pos[i]]]++; } if (pos[i] > m) return false; } } return true; }
1
#include<bits/stdc++.h> using namespace std; typedef long long ll; using vll=vector<ll>; using vvll=vector<vll>; using vi=vector<int>; using vvi=vector<vector<int>>; using vb=vector<bool>; using pii=pair<int,int>; using vpii=vector<pair<int,int>>; template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;} template<class T> inline bool chmay(T& a, T b) {if (a < b) {a = b;return true;}return false;} //pow(llpow,modpow) long long modinv(long long a, long long m) {long long b = m, u = 1, v = 0;while (b) {long long t = a / b;a -= t * b; swap(a, b);u -= t * v; swap(u, v);}u %= m;if (u < 0) u += m;return u;} #define rep(i, begin_i, end_i) for (ll i = (ll)begin_i; i < (ll)end_i; i++) //試験導入 #define irep(i, end_i, begin_i) for (ll i = (ll)begin_i-1; i >= (ll)end_i; i--) long long INF = 1LL<<60; int main( ){ int n; cin>>n; vpii rxy(n),byx(n); rep(i,0,n){ int x,y; cin>>x>>y; pii p=make_pair(x,y); rxy[i]=p; } rep(i,0,n){ int x,y; cin>>x>>y; pii p=make_pair(y,x); byx[i]=p; } vb fixedr(n,false); int cnt=0; sort(rxy.begin(),rxy.end()); sort(byx.begin(),byx.end()); rep(bi,0,n){ int bx=byx[bi].second; int by=byx[bi].first; int rfi=-1,vrf=0; rep(ri,0,n){ if(fixedr[ri])continue; int rx,ry; rx=rxy[ri].first; ry=rxy[ri].second; if(rx<bx&&ry<by){ rfi=ri; } } if(rfi!=-1){ cnt++; fixedr[rfi]=true; } } cout<<cnt<<endl; return 0; }
#include<bits/stdc++.h> #define Fst first #define Snd second #define RG register #define mp make_pair #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long LL; typedef long double LD; typedef unsigned int UI; typedef unsigned long long ULL; template<typename T> inline void read(T& x) { char c = getchar(); bool f = false; for (x = 0; !isdigit(c); c = getchar()) { if (c == '-') { f = true; } } for (; isdigit(c); c = getchar()) { x = x * 10 + c - '0'; } if (f) { x = -x; } } template<typename T, typename... U> inline void read(T& x, U& ... y) { read(x), read(y...); } const int N=1e5+10; int n,m,p; int head[N],vis[N],In[N]; bool Flag; struct Edge { int to,last; Edge () {} Edge (int a,int b) :to(a),last(b) {} }edge[N<<2]; void ADD(int a,int b) { edge[++p]=Edge(b,head[a]); head[a]=p; edge[++p]=Edge(a,head[b]); head[b]=p; } void DFS(int u,int c) { if(vis[u]) { if(vis[u]!=c) Flag=true; return; } vis[u]=c; for(int i=head[u];i;i=edge[i].last) { int v=edge[i].to; DFS(v,c^1); } } //#define rua int main() { // ios::sync_with_stdio(false); #ifdef rua freopen("GG.in","r",stdin); #endif read(n,m); for(int i=1;i<=m;++i) { int u,v; read(u,v); ADD(u,v); ++In[u]; ++In[v]; } LL A=0,B=0,C=0; for(int i=1;i<=n;++i) if(!vis[i]) { if(!In[i]) ++A; else { Flag=false; DFS(i,2); if(Flag) ++C; else ++B; } } printf("%lld\n",A*n+A*(n-A)+B*B*2+C*C+C*B*2); return 0; }
0
#include<iostream> #include<cctype> #include<string> using namespace std; int main(int argc, char const *argv[]) { string str,str1,str2; int a=0, b=0, n; cin >> str; cin >> n; for (int i = 0; i < n; ++i) { cin >> str1; cin >> a >> b; if(str1 == "print"){ str2 = str.substr(a,b-a+1); cout << str2 << endl; } if(str1 == "reverse"){ str2 = str.substr(a,b-a+1); for (int i = 0; i < str2.size(); ++i) { str[a+i] = str2[str2.size()-i-1]; } } if(str1 == "replace"){ cin >> str2; for (int i = 0; i < str2.size(); ++i) { str[a+i] = str2[i]; } } } return 0; }
#include <bits/stdc++.h> #define int long long #define x first #define y second #define getbit(x, i) (((x) >> (i)) & 1) using namespace std; typedef pair<int, int> pii; #define hashset unordered_set #define hashmap unordered_map #define newline fast_writechar('\n') #define unify(arr) arr.resize(unique(arr.begin(), arr.end()) - arr.begin()) #define getbit(x, i) (((x) >> (i)) & 1) template <typename T> vector<T> readvector(size_t sz) { vector<T> res(sz); for (size_t i = 0; i < sz; ++i) { cin >> res[i]; } return res; } template <typename T> std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) { std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, " ")); return out; } int mylog(int n) { return 63 - __builtin_clzll(n); } inline int binPow(int x, int deg, int mod) { int ans = 1; for (int i = sizeof(deg) * CHAR_BIT - 1; i >= 0; i--) { ans *= ans; ans %= mod; if (getbit(deg, i)) ans *= x; ans %= mod; } return ans; } /** Interface */ inline int readInt(); inline int readUInt(); inline void readWord(char *s); inline int fast_readchar(); // you may use readchar() instead of it inline void writeInt(int x); inline void fast_writechar(int x); // you may use putchar() instead of it inline void writeWord(const char *s); inline void fast_flush(); // ====================== END ====================== const int MAXN = 2e6 + 10; const int MOD = 1e9 + 7; const int INF = 1e18; void solve() { int n, k; cin >> n >> k; auto arr =readvector<int>(n); arr.insert(arr.begin(), 0); for (int i = 1; i <= n; i++) arr[i] += arr[i - 1]; int ans = 0; for (int i = 0; i + k <= n; i++) { ans = max(ans, arr[i + k] - arr[i] + k); } cout << fixed << setprecision(1) << ans / 2. << endl; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t = 1; // t = readInt(); // cin >> t; for (int i = 1; i <= t; ++i) { // cout << "Case #" << i << ": "; solve(); } // fast_flush(); return 0; } /** Read */ static const int buf_size = 4096; inline int fast_readchar() { static char buf[buf_size]; static int len = 0, pos = 0; if (pos == len) pos = 0, len = fread(buf, 1, buf_size, stdin); if (pos == len) return -1; return buf[pos++]; } inline int readUInt() { int c = fast_readchar(), x = 0; while (c <= 32) c = fast_readchar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar(); return x; } inline int readInt() { int s = 1, c = fast_readchar(); int x = 0; while (c <= 32) c = fast_readchar(); if (c == '-') s = -1, c = fast_readchar(); while ('0' <= c && c <= '9') x = x * 10 + c - '0', c = fast_readchar(); return x * s; } inline void readWord(char *s) { int c = fast_readchar(); while (c <= 32) c = fast_readchar(); while (c > 32) *s++ = c, c = fast_readchar(); *s = 0; } /** Write */ static int write_pos = 0; static char write_buf[buf_size]; inline void fast_writechar(int x) { if (write_pos == buf_size) fwrite(write_buf, 1, buf_size, stdout), write_pos = 0; write_buf[write_pos++] = x; } inline void fast_flush() { if (write_pos) fwrite(write_buf, 1, write_pos, stdout), write_pos = 0; } inline void writeInt(int x) { if (x < 0) fast_writechar('-'), x = -x; char s[24]; int n = 0; while (x || !n) s[n++] = '0' + x % 10, x /= 10; while (n--) fast_writechar(s[n]); } inline void writeWord(const char *s) { while (*s) fast_writechar(*s++); }
0
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<long long> VL; typedef vector<vector<long long>> VVL; typedef vector<string> VS; typedef pair<int, int> P; typedef tuple<int,int,int> tpl; #define ALL(a) (a).begin(),(a).end() #define SORT(c) sort((c).begin(),(c).end()) #define REVERSE(c) reverse((c).begin(),(c).end()) #define LB(a,x) lower_bound((a).begin(), (a).end(), x) - (a).begin() #define UB(a,x) upper_bound((a).begin(), (a).end(), x) - (a).begin() #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define RFOR(i,a,b) for(int i=(a)-1;i>=(b);--i) #define RREP(i,n) RFOR(i,n,0) #define en "\n" constexpr double EPS = 1e-9; constexpr double PI = 3.141592653589793238462643383279; constexpr int INF = 2147483647; constexpr long long LINF = 1LL<<60; constexpr long long MOD = 1000000007; // 998244353 #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } int main(void){ int N; cin >> N; string S; cin >> S; VI nd(N,0), nm(N,0), nc(N,0); REP(i,N){ if(S[i]=='D') nd[i] = 1; else if(S[i]=='M') nm[i] = 1; else if(S[i]=='C') nc[i] = 1; } REP(i,N-1) nd[i+1] += nd[i]; REP(i,N-1) nm[i+1] += nm[i]; REP(i,N-1) nc[i+1] += nc[i]; ll ndmc = 0; REP(i,N) if(S[i]=='M') ndmc += (ll)nd[i]*(nc[N-1]-nc[i]); VL sc(nc[N-1]+1); REP(i,N) if(S[i]=='C') sc[nc[i]] = nm[i]; REP(i,nc[N-1]) sc[i+1] += sc[i]; int Q; cin >> Q; REP(i,Q){ int k; cin >> k; ll ans = 0; REP(i,N-k){ if(S[i] != 'D') continue; ll M = nm[i]; ll t = nc.back() - nc[i+k-1]; ans += sc.back() - sc[nc.back()-t] - M*t; } cout << ndmc - ans << en; } return 0; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll n,q,k; char s[1000005]; int main(){ scanf("%lld%s%lld",&n,s,&q); while(q--){ scanf("%lld",&k); ll D=0,M=0,DM=0,DMC=0; for(ll i=0;i<n;i++){ if(i>=k){ if(s[i-k]=='D'){ DM-=M; D--; } if(s[i-k]=='M') M--; } if(s[i]=='D') D++; if(s[i]=='M'){ M++; DM+=D; } if(s[i]=='C') DMC+=DM; } printf("%lld\n",DMC); } }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vl; typedef pair<int,int> pii; typedef int _loop_int; #define REP(i,n) for(_loop_int i=0;i<(_loop_int)(n);++i) #define FOR(i,a,b) for(_loop_int i=(_loop_int)(a);i<(_loop_int)(b);++i) #define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1;i>=(_loop_int)(a);--i) #define DEBUG(x) cout<<#x<<": "<<x<<endl #define DEBUG2(x,y) cout<<#x<<": "<<x<<" "<<#y<<": "<<y<<endl #define DEBUG_VEC(v) cout<<#v<<":";REP(i,v.size())cout<<" "<<v[i];cout<<endl #define DEBUG_ARR(v,n) cout<<#v<<":";REP(i,n)cout<<" "<<v[i];cout<<endl #define ALL(a) (a).begin(),(a).end() const ll MOD = 1000000007ll; #define FIX(a) ((a)%MOD+MOD)%MOD template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> void DEBUG_DP(T* viewdp, int ilen, int jlen){ REP(i,ilen){ REP(j,jlen){ if(viewdp[i][j] != -1) printf("[% 4d]", viewdp[i][j]); else printf("[ ]"); } puts(""); } } const int N=81; const int C=6400; const int K=C*2+1; int h,w; int a[N][N], b[N][N]; bitset<K> dp[N][N]; int ans = INT_MAX; int main() { ios::sync_with_stdio(false); cin.tie(0); REP(i,N){ fill(a[i], a[i]+N, -1); fill(b[i], b[i]+N, -1); } cin>>h>>w; REP(i,h) REP(j,w) cin>>a[i][j]; REP(i,h) REP(j,w) cin>>b[i][j]; //int sx=0,sy=0, gx=w-1, gy=h-1; dp[0][0][abs(a[0][0]-b[0][0]) + 6400]=true; REP(i,h) REP(j,w){ int ab =abs(a[i+1][j]-b[i+1][j]); dp[i+1][j] |= dp[i][j] << ab; dp[i+1][j] |= dp[i][j] >> ab; ab =abs(a[i][j+1]-b[i][j+1]); dp[i][j+1] |= dp[i][j] << ab; dp[i][j+1] |= dp[i][j] >> ab; } REP(i,K){ if(dp[h-1][w-1][i]) chmin(ans, abs(6400-i)); } cout << ans << endl; return 0; }
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<cmath> #include<bitset> #include<deque> #include<functional> #include<iterator> #include<map> #include<set> #include<stack> #include<queue> #include<utility> using namespace std; typedef long long ll; typedef pair<ll,ll> P; #define a first #define b second #define sz(x) (ll)((x).size()) #define pb push_back #define mp make_pair #define bg begin() #define ed end() #define all(x) (x).bg,(x).ed #define rep(i,n) for(ll i=0;i<(n);i++) #define rep1(i,n) for(ll i=1;i<=(n);i++) #define rrep(i,n) for(ll i=(n)-1;i>=0;i--) #define rrep1(i,n) for(ll i=(n);i>=1;i--) #define FOR(i,a,b) for(ll i=(a);i<(b);i++) const ll MOD=1000000007; const ll INF=1000000000000000; template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;} template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;} ll maxx(ll x,ll y,ll z){return max(max(x,y),z);} ll minn(ll x,ll y,ll z){return min(min(x,y),z);} ll gcd(ll x,ll y){if(x%y==0) return y;else return gcd(y,x%y);} ll lcm(ll x,ll y){return x*(y/gcd(x,y));} ll digsz(ll x){if(x==0) return 1;else{ll ans=0;while(x){x/=10;ans++;}return ans;}} ll digsum(ll x){ll sum=0;while(x){sum+=x%10;x/=10;}return sum;} vector<ll> pw2(62,1);vector<ll> pw10(19,1); int main(){ {rep1(i,61) pw2[i]=2*pw2[i-1];} {rep1(i,18) pw10[i]=10*pw10[i-1];} ll K,T; cin>>K>>T; vector<ll> A(T,0); rep(i,T){ cin>>A[i]; } sort(all(A)); ll L=A[sz(A)-1]; rep(i,sz(A)-1){ L-=A[i]; } cout<<max(L-1,0ll)<<endl; }
0
#include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <vector> #include <sstream> #include <memory> #include <iomanip> std::vector<int> parse_line(std::string const& line) { using namespace std; vector<int> result; istringstream s(line); string element; while (getline(s, element, ' ')) { stringstream buf; int value = 0; buf << element; buf >> value; result.push_back(value); } return std::move(result); } int main(int argc, char* argv[]) { using namespace std; while (true) { string line; getline(std::cin, line); auto elements = parse_line(line); auto length = elements[0]; if (length == 0) break; vector<int> total(length + 1); size_t width = length + 1; for (size_t i = 0; i < length; ++i) { getline(cin, line); elements = parse_line(line); int sum = 0; for (size_t n = 0; n < length; ++n) { int val = elements[n]; sum += val; total[n] += val; cout << setw(5) << val; } cout << setw(5) << sum << endl; total[length] += sum; } for (size_t i = 0; i < width; ++i) { cout << setw(5) << total[i]; } cout << endl; } return 0; }
#include <cstdio> int matrix[12][12]; int n; int main() { while(scanf("%d", &n) != EOF, n) { for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { scanf("%d", &matrix[i][j]); } } // output for (int i=0; i<n; i++) { int sum = 0; for (int j=0; j<n; j++) { sum += matrix[i][j]; printf("%5d", matrix[i][j]); } matrix[i][n] = sum; printf("%5d\n", sum); } for (int j=0; j<=n; j++) { int sum = 0; for (int k=0; k<n; k++) { sum += matrix[k][j]; } printf("%5d", sum); } printf("\n"); } }
1
#include <algorithm> #include <cstdio> #include <iostream> #include <map> #include <cmath> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <vector> #include <stdlib.h> #include <stdio.h> #include <bitset> #include <cstring> using namespace std; #define FOR(I,A,B) for(int I = (A); I < (B); ++I) #define CLR(mat) memset(mat, 0, sizeof(mat)) typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(0); int a, b, c; while(cin>>a>>b>>c,a||b||c) { int N; cin >> N; int info_[4][N]; FOR(i,0,N) FOR(j,0,4) cin >> info_[j][i]; int info[a+b+c+1]; FOR(i,0,a+b+c+1) info[i] = 2; // ???????????????????????¨????????? FOR(i,0,N) if(info_[3][i] == 1) FOR(j,0,3) info[info_[j][i]] = 1; // ?????????????????¨????????? FOR(i,0,N) { if(info_[3][i] == 0) { int cnt = 0; FOR(j,0,3) cnt += info[info_[j][i]]; if(cnt == 4) { FOR(j,0,3) if(info[info_[j][i]] == 2) info[info_[j][i]] = 1000; } } } FOR(i,1,a+b+c+1) cout << (info[i] == 1000 ? 0 : info[i]) << '\n'; } return 0; }
#include <cstdio> #include <algorithm> using namespace std; int a, b, c, N, i[1000], j[1000], k[1000], r[1000], h[301]; int main(){ while(scanf("%d%d%d", &a, &b, &c), a+b+c){ scanf("%d", &N); for(int x = 0; x < N; x++){ scanf("%d%d%d%d", i+x, j+x, k+x, r+x); } fill(h, h+301, 2); for(int x = 0; x < N; x++){ if(r[x] == 1){ h[i[x]] = h[j[x]] = h[k[x]] = 1; } } for(int x = 0; x < N; x++){ if(r[x]) continue; if(h[i[x]]%2 + h[j[x]]%2 + h[k[x]]%2 == 2){ if(h[i[x]]/2){ h[i[x]] = 0; } if(h[j[x]]/2){ h[j[x]] = 0; } if(h[k[x]]/2){ h[k[x]] = 0; } } } for(int x = 1; x <= a+b+c; x++){ printf("%d\n", h[x]); } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s; char c[3]={}; int k,ans=999; cin>>s; for(int i=0;i<s.size()-2;i++) { c[0]=s[i]; c[1]=s[i+1]; c[2]=s[i+2]; k=atoi(c); // cout<<"k = "<<k<<'\n'; ans=min(abs(k-753),ans); } cout<<ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main(void) { vector<ll> cost(5); ll N; cin >> N; for (int i = 0; i < 5; i++) { cin >> cost[i]; } ll result = (ll) ceil((double) N / *min_element(cost.begin(), cost.end())) + 4; cout << result << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main(){ long long N,M; cin>>N>>M; vector<long long> l(M); vector<long long> r(M); for(long long i=0;i<M;i++) cin>>l.at(i)>>r.at(i); sort(l.begin(),l.end()); sort(r.begin(),r.end()); long long a=r.at(0)-l.at(M-1)+1; if(a>0) cout<<a<<endl; else cout<<0<<endl; }
#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <climits> #include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #include <utility> #include <set> #include <map> #include <stack> #include <queue> #include <deque> #include <functional> using namespace std; #define fst first #define scd second #define PB push_back #define MP make_pair #define all(a) a.begin(),a.end() #define rall(a) a.rbegin(),a.rend() #define omajinai ios::sync_with_stdio(false);cin.tie(0) #define rep(i,x) for(int i=0;i<(int)(x);++i) #define rep1(i,x) for(int i=1;i<=(int)(x);++i) #define rrep(i,x) for(int i=(int)x-1;i>=0;--i) #define rrep1(i,x) for(int i=(int)x;i>=1;--i) using ll=long long; using ld=long double; using vi=vector<int>; using vvi=vector<vi>; using pii=pair<int, int>; using vpii=vector<pii>; template<class T,class U>ostream&operator<<(ostream&os,const pair<T,U>p){os<<"("<<p.fst<<", "<<p.scd<<")";return os;} template<class T>ostream&operator<<(ostream&os,const vector<T>v){rep(i,v.size()){if(i)os<<", ";os<<v[i];}return os;} template<typename T>T&max(T&a,T&b){if(a>=b)return a;return b;} template<typename T>T&min(T&a,T&b){if(a < b)return a;return b;} template<typename T>bool chmax(T&a,T b){if(a < b){a=b;return true;}return false;} template<typename T>bool chmin(T&a,T b){if(a > b){a=b;return true;}return false;} constexpr ll TEN(ll n){return n==0?1:10ll*TEN(n-1);} constexpr ll inf = TEN(9)+5; constexpr ll linf = 3*TEN(18); signed main() { int N, M; cin >> N >> M; vector<int> imos(N + 1); rep(i, M) { int l, r; cin >> l >> r; --l, --r; imos[l]++, imos[r + 1]--; } rep(i, N) imos[i + 1] += imos[i]; int ans = 0; rep(i, N) if (imos[i] == M) ans++; cout << ans << endl; }
1
#include "bits/stdc++.h" using namespace std; typedef unsigned long long li; int main() { cin.tie(0); ios::sync_with_stdio(false); li n; cin >> n; vector<li> xs(n); for (int i = 0; i < n; ++i) { cin >> xs[i]; } li xorsum = 0; for (int i = 0; i < n; ++i) { xorsum ^= xs[i]; } li ans = 0; for (unsigned int pos = 0; pos < 60; ++pos) { if ((xorsum & (1ULL << pos)) != 0) { ans += 1ULL << pos; for (int i = 0; i < n; ++i) { xs[i] &= ~(1ULL << pos); } } } vector<bool> used(n); for (unsigned int pos = 59; pos <= 59; --pos) { for (int pivot = 0; pivot < n; ++pivot) { if ((xs[pivot] & (1ULL << pos)) != 0 && !used[pivot]) { used[pivot] = true; for (int i = 0; i < n; ++i) { if (i == pivot) { continue; } if ((xs[i] & (1ULL << pos)) == 0) { continue; } xs[i] ^= xs[pivot]; } } } } li dupsum = 0; for (int i = 0; i < n; ++i) { dupsum ^= xs[i]; } cout << ans + dupsum * 2 << endl; return 0; }
#include<vector> #include<iostream> #include<stdio.h> #include<stdlib.h> #include <iostream> #include <algorithm> #include <map> #include <cmath> #include<queue> #include <sstream> #include <set> #include<stack> const long long MOD = 1000000007; using namespace std; typedef long long llong; //int isalpha(char ch): ch がアルファベットなら true を返す //int isdigit(char ch): ch が数字なら true を返す //int islower(char ch): ch が小文字なら true を返す //int isupper(char ch): ch が大文字なら true を返す //int tolower(char ch): ch の小文字を返す //int toupper(char ch): ch の大文字を返す //string型 //size() 文字数を返す //Insert() (指定した場所に)文字・文字列を挿入する //erase() (指定した場所の)文字・文字列を削除する //clear() すべての文字を削除する //substr() 文字列の(指定した)部分文字列を返す //replace() (指定した)部分文字列を新しい文字列に置換する //c_str()変換 //文字列の比較は、<=や==などを使え //replace関数を使い、簡単に文字列を置換 //リバース関数:reverse(str.begin(), str.end()); //map<type, type> dict;で宣言 //グラフ理論用変数 //vector<vector<llong> > graph(N); //ソート //降順sort(v.begin(), v.end(), std::greater<Type>()); //大文字から小文字へんかん //w[i] = w[i]-'A'+'a'; //vector //assignメソッド 引数:サイズ、値 //与えられたサイズと値でvectorを初期化する //queueクラス //find()次に取り出す値の表示をする。 //pop()値を取り出す。戻り値はなし //push()キューに値をプッシュする //priority_queueクラス //切り上げ //ceil //floor llong eval(char *s){ llong total; total = strtol(s, &s, 10); while(*s){ if(*s=='+'){ s++; total = total + strtol(s, &s, 10); continue; } if(*s=='-'){ s++; total = total - strtol(s, &s, 10); continue; } } return total; } int main(){ string S; cin >> S; llong N=S.size()-1; llong total = 0; string s; for(int i=0; i<pow(2, N); i++){ string temp; for(int j=0; j<N; j++){ if((i>>j)&1){ temp.push_back('+'); continue; } temp.push_back('-'); } s = ""; for(int i=0; i<N; i++){ s = s + S[i] + temp[i]; } s = s + S[S.size()-1]; if(eval((char*)s.c_str())==7){ cout << s << "=7" << endl; exit(0); }; } return 0; }
0
#include <iostream> using namespace std; using LL = long long; const LL INF = 1L << 32; LL G[101][101]; int main() { int V, E; cin >> V >> E; // for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) G[i][j] = 0; else G[i][j] = INF; } } for (int i = 0; i < E; i++) { int s, t, d; cin >> s >> t >> d; G[s][t] = d; } // bool negative_cycle = false; for (int k = 0; k < V; k++) { for (int i = 0; i < V; i++) { if (G[i][k] == INF) continue; for (int j = 0; j < V; j++) { if (G[k][j] == INF) continue; G[i][j] = min(G[i][j], G[i][k] + G[k][j]); if (i == j && G[i][j] < 0) { negative_cycle = true; goto outer; } } } } outer: // if (negative_cycle) { cout << "NEGATIVE CYCLE" << endl; } else { for (int i = 0; i < V; i++) { int k = 0; for (int j = 0; j < V; j++) { if (k++) cout << " "; cout << (G[i][j] != INF ? to_string(G[i][j]) : "INF"); } cout << endl; } } }
#include <iostream> #include <algorithm> using namespace std; bool isSmallAlphabet(char& ch) { /* a ~ z(ascii) : 97 ~ 122 */ if('a' <= ch && ch <= 'z') return true; else return false; } bool isHit(string& str) { if(str.find("the") != str.npos) return true; else if(str.find("this") != str.npos) return true; else if(str.find("that") != str.npos) return true; else return false; } char shiftIndex(char ch) { if(isSmallAlphabet(ch)) { ch += 1; /* ???????????????????°?????????§???????????£??????*/ if(!isSmallAlphabet(ch)) { ch -= 26; } } return ch; } /* ????????¶??????????§£??? */ void CaesarCipher(string str) { /* p??????????????¢??????????????????????????????????????? */ for(int p = 0; p < 26; ++p) { transform(str.begin(), str.end(), str.begin(), shiftIndex); /* Find "the" or "this" or "that" */ if(isHit(str)) { cout << str << endl; } } } int main() { string str; while(getline(cin, str)) { CaesarCipher(str); } return 0; }
0
#include <string> #include <vector> #include <iostream> using namespace std; vector<string> R; const vector<int> dx = { 0, 1, 0, -1 }; const vector<int> dy = { -1, 0, 1, 0 }; int rec(int x, int y) { R[y][x] = '0'; for (int dir = 0; dir < 4; dir++) { if (0 <= x + dx[dir] && x + dx[dir] < 12 && 0 <= y + dy[dir] && y + dy[dir] < 12) { if (R[y + dy[dir]][x + dx[dir]] == '1') { rec(x + dx[dir], y + dy[dir]); } } } } int solve() { int ret = 0; for (int i = 0; i < 12; i++) { for (int j = 0; j < 12; j++) { if (R[i][j] == '1') { rec(j, i); ret++; } } } return ret; } int main() { while (true) { R = vector<string>(12); for (int i = 0; i < 12; i++) { if (!(cin >> R[i])) { return 0; } } cout << solve() << endl; } }
#include <cstdio> #include <vector> using namespace std; typedef pair<int, int> P; int par[16][16]; int rank[16][16]; char map[16][16]; int cnt; void init(); void unite(int y1, int x1, int y2, int x2); int find(int y, int x); int main() { while (scanf("%s", &map[1][1]) != EOF){ for (int i = 2; i <= 12; i++) scanf("%s", &map[i][1]); init(); for (int i = 1; i <= 12; i++){ for (int j = 1; j <= 12; j++){ if (map[i][j] == '1'){ cnt++; unite(i, j, i - 1, j); unite(i, j, i + 1, j); unite(i, j, i, j - 1); unite(i, j, i, j + 1); } } } printf("%d\n", cnt); } return 0; } void init() { for (int i = 0; i < 16; i++){ for (int j = 0; j < 16; j++){ par[i][j] = i * 16 + j; rank[i][j] = 1; } } cnt = 0; } void unite(int y1, int x1, int y2, int x2) { if (map[y2][x2] == '1' && find(y1, x1) != find(y2, x2)){ if (rank[y1][x1] < rank[y2][x2]){ par[y1][x1] = y2 * 16 + x2; rank[y1][x1]++; } else { par[y2][x2] = y1 * 16 + x1; rank[y2][x1]++; } cnt--; } } int find(int y, int x) { if (par[y][x] != y * 16 + x){ par[y][x] = find(par[y][x] / 16, par[y][x] % 16); } return par[y][x]; }
1
#include <bits/stdc++.h> using namespace std; #define fi first #define se second #define all(a) a.begin(), a.end() #define pb push_back #define mp make_pair typedef long long ll; typedef pair<ll,ll> P; typedef pair<ll,int> pli; typedef pair<int,int> pii; #define rep(i,a,b) for(ll i=a ; i<b ; i++) #define qrep(que, ite) for(auto ite=begin(que) ; ite!=end(que) ; ite++) const int max_n = 1e5; const ll mod = 1e9+7; const ll INF = 1e17; const int inf = 1e5; typedef long double ld; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } ll gcd(ll a, ll b) { return a ? gcd(b%a, a) : b; } int dx[4] = {0,0,1,-1}; int dy[4] = {1,-1,0,0}; struct UnionFind { vector<int> par; UnionFind(int n) : par(n, -1) { } void init(int n) { par.assign(n, -1); } int root(int x) { if (par[x] < 0) return x; else return par[x] = root(par[x]); } bool issame(int x, int y) { return root(x) == root(y); } bool merge(int x, int y) { x = root(x); y = root(y); if (x == y) return false; if (par[x] > par[y]) swap(x, y); // merge technique par[x] += par[y]; par[y] = x; return true; } int size(int x) { return -par[root(x)]; } }; struct SegmentTree{ int N; vector<int> node; public : void intit(vector<int>v){ int sz = v.size(); N=1; while(N<sz) N*=2; node.resize(N); for(int i=0 ; i<sz ; i++) node[i+N-1] = v[i]; for(int i=N-2 ; i>=0 ; i--) node[i] = min(node[i*2+1], node[i*2+2]); } void update(int x, int val){ x += N-1; node[x+N-1] = val; while(x>0){ x = (x-1)/2; node[x] = min(node[x*2+1], node[x*2+2]); } } int getmin(int a, int b, int k, int l, int r){ if(b<=l || r<=a) return inf; else if(a<=l && r<=b) return node[k]; else{ int vl = getmin(a, b, 2*k+1, l, (l+r)/2); int vr = getmin(a, b, 2*k+2, (l+r)/2, r); return min(vl, vr); } } }; vector<ll> divisor(ll n){ vector<ll> res; for(ll i=1; i*i<=n ; i++){ while(n%i==0){ ++res[i]; n /= i; } } if(n!=1) res[n]=1; return res; } int main(){ ll n; cin >> n; ll k = n; int cnt = 1; while(true){ ll now = pow(26, cnt); if(k - now > 0){ k -= now; cnt++; }else{ break; } } string ans = ""; k--; for(int i=cnt-1 ; i>=0 ; i--){ ll now = pow(26, i); ll st = k / now; for(char c='a' ; c<='z' ; c++){ if(c - 'a' == st){ ans += c; } } k -= now * st; } cout << ans << endl; return 0; }
#include<iostream> #include<cstdlib> #include<utility> #include<tuple> #include<string> #include<vector> #include<numeric> #include<algorithm> #include<queue> #include<deque> #include<bitset> #include<cmath> #include<map> #include<iomanip> using namespace std; using ll = long long; const ll mod = 1e9 + 7; #define rep(i, a, b) for(ll i = a; i < b; i++) int main() { ll n; cin >> n; string ans; while (n) { --n; ans += (char)('a' + (n % 26)); n /= 26; } reverse(ans.begin(), ans.end()); cout << ans << endl; return 0; }
1
#include<cstring> #include<cstdlib> #include<cstdio> #include<cmath> #include<iostream> #define N 101 using namespace std; short n,m,f[N][N][N][N],sx,sy,h[N][N],L[N][N]; char s[N][N]; void upd(short &x,short y) { if(x<y) x=y; } int main() { cin>>n>>m; for(short i=1;i<=n;i++) { scanf("%s",s[i]+1); for(short j=1;j<=m;j++) if(s[i][j]=='E') sx=i,sy=j; } for(short i=1;i<=n;i++) for(short j=1;j<=m;j++) { L[i][j]=L[i-1][j]; h[i][j]=h[i][j-1]; if(s[i][j]=='o') h[i][j]++,L[i][j]++; } short ln=m-sy,rn=sy-1,un=n-sx,dn=sx-1; for(short i=0;i<=ln;i++) for(short j=0;j<=rn;j++) for(short x=0;x<=un;x++) for(short y=0;y<=dn;y++) { short t,now,l,r; if(i<ln) { now=sy+i+1;t=0; if(now+j<=m) { l=max(sx-y,x+1);r=min(sx+x,n-y); if(l<=r) t=L[r][now]-L[l-1][now]; } upd(f[i+1][j][x][y],f[i][j][x][y]+t); } if(j<rn) { now=sy-j-1;t=0; if(now-i>=1) { l=max(sx-y,x+1);r=min(sx+x,n-y); if(l<=r) t=L[r][now]-L[l-1][now]; } upd(f[i][j+1][x][y],f[i][j][x][y]+t); } if(i==1 && j==0 && x==0 && y==1) {short oo=1;} if(x<un) { now=sx+x+1;t=0; if(now+y<=n) { l=max(sy-j,i+1);r=min(sy+i,m-j); if(l<=r) t=h[now][r]-h[now][l-1]; } upd(f[i][j][x+1][y],f[i][j][x][y]+t); } if(y<dn) { now=sx-y-1;t=0; if(now-x>=1) { l=max(sy-j,i+1);r=min(sy+i,m-j); if(l<=r) t=h[now][r]-h[now][l-1]; } upd(f[i][j][x][y+1],f[i][j][x][y]+t); } if(f[1][0][1][1]==4) {short oo=1;} } cout<<f[ln][rn][un][dn]<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int w,h; bool isRange(int i,int j){ return 0<=i && i<w && 0<=j && j<h; } int dw[]={-1,0,1,0}; int dh[]={0,1,0,-1}; int dfs(pair<int,int> p,const pair<int,int> g,vector<vector<int>>& grid,int turn){ if(turn==11) return 100; int res=100; for(int i=0;i<4;i++){ if(isRange(p.first+dw[i],p.second+dh[i]) && grid[p.first+dw[i]][p.second+dh[i]]==1) continue; for(auto j=p;isRange(j.first,j.second);j.first+=dw[i] ,j.second+=dh[i]){ if(j==g){ return turn; } if(grid[j.first][j.second]==1){ grid[j.first][j.second]=0; j.first-=dw[i]; j.second-=dh[i]; res=min(res,dfs(j,g,grid,turn+1)); grid[j.first+dw[i]][j.second+dh[i]]=1; break; } } } return res; } int main(){ while(cin>>w>>h,w){ pair<int,int> s; pair<int,int> g; vector<vector<int>> grid(w,vector<int>(h,0)); for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ int buf; cin>>buf; grid[j][i]=buf; if(buf==2) s=make_pair(j,i); if(buf==3) g=make_pair(j,i); } } int res=dfs(s,g,grid,1); if(res>11) res=-1; cout<<res<<endl; } return 0; }
0
#include <iostream> #include <iomanip> #include <string> #include <vector> typedef long long ll; using namespace std; const ll INF = 1e9; const ll MOD = 1e9 + 7; #define repi(i,n,init) for(ll i=init;i<(n);i++) int main() { int n,q; string s; cin >> n >> q >> s; vector<int> cnt(n,0); repi(i,n,1){ if(s[i-1] == 'A' && s[i] == 'C')cnt[i]++; cnt[i] += cnt[i - 1]; } repi(i,q,0){ int l,r; cin >> l >> r; l--; r--; cout << cnt[r] - cnt[l] << endl; } return 0; }
/* ID: hafiz.i1 TASK: milk2 LANG: C++ */ #include<bits/stdc++.h> #define ll long long #define debug(x) cout<<x<<"DE"<<endl; using namespace std; int main() { // freopen("milk2.in", "r", stdin); // freopen("milk2.out", "w", stdout); ios_base::sync_with_stdio(false); cin.tie(NULL); ll n,q; cin>>n>>q;ll l[n+1];l[1]=0,l[0]=0; string s; cin>>s; for(int i=2;i<=n;i++){ if(s[i-2]=='A'&&s[i-1]=='C')l[i]=l[i-1]+1; else l[i]=l[i-1]; } for(int i=1;i<=q;i++){ ll a,b; cin>>a>>b; cout<<l[b]-l[a]<<endl; } }
1
#include <iostream> #include <vector> using namespace std; int main(){ int n,t; cin >> n >> t; vector<int> v(n); int i; for(i=0;i<n;i++) cin >> v[i]; long long cnt = 0; for(i=0;i<n-1;i++){ if(v[i+1]-v[i]<=t) cnt+= v[i+1]-v[i]; else cnt += t; } cnt += t; cout << cnt << "\n"; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define rep(i, n) FOR(i, 0, n) #define whole(x) (x).begin(),(x).end() #define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) using P = pair<int, int>; #define debug(var) cerr << "[" << #var << "] " << var << endl const ll mod = 1000000007; const int INF = 1001001001; int main(){ int n, m; cin >> n >> m; int arr[9] = {2, 5, 5, 4, 5, 6, 3, 7, 6}; vector<int> a(m); rep(i, m) cin >> a[i]; sort(whole(a)); reverse(whole(a)); vector<int> x(m); rep(i, m) x[i] = arr[a[i]-1]; vector<P> dp(n+1, P(-INF, -1)); dp[0] = P(0, -1); rep(i, n+1) { for (int j=0; j<x.size(); j++) { int e = x[j]; if (i-e>=0 && dp[i-e].first>=0) { if (dp[i].first<dp[i-e].first+1) { dp[i].first = dp[i-e].first + 1; dp[i].second = a[j]; } } } } int keta = dp[n].first; int index = n; map<int, int> mp; while (index) { mp[dp[index].second]++; index -= arr[dp[index].second-1]; } string ans; for (auto m: mp) { rep(i, m.second) { ans += m.first+'0'; } } reverse(whole(ans)); cout << ans << endl; return 0; }
0
#include <iostream> #include <vector> #include <cmath> #include <iomanip> #include <algorithm> #include <queue> using namespace std; #define rep(i,n) for(int i=0; i<(n); i++) #define all(x) (x).begin(), (x).end() int main() { long long ans=0,n,a,b; cin>>n>>a>>b; vector<long long>p(n); rep(i,n)cin>>p[i]; rep(i,n-1){ ans+=min(a*(p[i+1]-p[i]),b); } cout<<ans; return 0; }
#include <cstdio> #include <algorithm> #define rep(i, n) for(int i = 0; i < (n); ++i) using namespace std; int x[6]; int main(){ rep(i, 6){ scanf("%d", &x[i]); } int s = 0; rep(i, 6){ s += x[i]; } printf("%d\n", s - *min_element(x, x + 4) - min(x[4], x[5])); return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < n; i++) int N; int sum = 0; int main() { cin >> N; vector<int> L(2 * N); for (int i = 0; i < 2 * N; i++) { cin >> L[i]; } sort(L.begin(), L.end()); for (int i = 0; i < 2 * N; i += 2) { sum += L[i]; } cout << sum << endl; return 0; }
#include<stdio.h> #define N 101 int main(){ int n; int p[N]; int m[N][N]; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%d%d",&p[i-1],&p[i]);} for(int i=1;i<=n;i++) m[i][i]=0; for(int l=2;l<=n;l++){ for(int i=1;i<=n-l+1;i++){ int j=i+l-1; /*2<<21*/ m[i][j]=2097152; for(int k=i;k<=j-1;k++){ /*さいしょう比較*/ if(m[i][j]<m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]) m[i][j]=m[i][j]; else m[i][j]=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]; } } } printf("%d\n",m[1][n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; // #define int ll typedef vector<int> vi; typedef vector<bool> vb; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef vector<vector<bool> > vvb; typedef map<int, bool> mib; typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll; typedef vector<string> vs; typedef long double ld; #define _GLIBCXX_DEBUG #define REP(i,n) for (int i = 0; i < (n); ++i) #define REPD(i,n) for (int i = (n-1); i >= 0; --i) #define FORE(i, s, n) for (int i = (s); i <= (int)(n); i++) #define FORED(i, s, n) for (int i = (s); i >= (int)(n); i--) #define debug(x) cerr << #x << ": " << x << '\n' #define debug2(x, y) cerr << #x << ": " << x << ", " << #y << ": " << y << '\n' #define debug3(x, y, z) cerr << #x << ": " << x << ", " << #y << ": " << y << ", " << #z << ": " << z << '\n' #define debugs(s) cerr << s << "\n" #define hyphen() cerr << "--\n" #define ALL(vec) (vec).begin(), (vec).end() #define REVALL(vec) (vec).rbegin(), (vec).rend() static const int dy[4] = {0,1,0,-1}, dx[4] = {1,0,-1,0}; #define fst first #define snd second #define pb push_back #define mk(x,y) make_pair((x),(y)) // http://ehafib.hatenablog.com/entry/2015/12/23/164137 int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a;a=1ll*a*a;}while(b>>=1);return ans;} int qpm(int a,ll b,int mo){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;} // gcd : O(logN) int gcd(int a,int b){return b?gcd(b,a%b):a;} int lcm(int a, int b) { return a / gcd(a, b) * b; } #define sz(v) int(v.size()) // x1, x2ライブラリとの被りを避ける #define x1 x192837465 #define x2 x123456789 #define y1 y192837465 #define y2 y123456789 // typo #define itn int #define reutrn return #define reutnr return #define srting string #define make(type, x) type x; cin>>x; #define make2(type, x, y) type x, y; cin>>x>>y; #define make3(type, x, y, z) type x, y, z; cin>>x>>y>>z; #define make4(type, x, y, z, t) type x, y, z, t; cin>>x>>y>>z>>t; int ctoi(const char c){ if('0' <= c && c <= '9') return (c-'0'); return -1; } #define uniq(v) (v).erase(unique(ALL((v))), (v).end()); // area of (0, 0)(a, b)(c, d) double area(double a, double b, double c, double d){ return abs(a * d - b * c) / 2.0; } const int PREC = 10; #define coutf(val) cout << fixed << setprecision(PREC) << (val) << endl; const int MOD = (int)1e9 + 7; const int INF = numeric_limits<int>::max(); string sweep(const string S) { // STの組を一掃する int scnt = 0; int tcnt = 0; string ans = ""; for(auto c : S) { if (c == 'S') { scnt++; } else { // c == 'T' // Sが残っている場合は消費 if (scnt > 0) { scnt--; } else { // 消し尽くしている場合はansに追加 ans += 'T'; } } } // 残っているscntを清算 ans += string(scnt, 'S'); return ans; } signed main() { make(string, S); debug(S); S = sweep(S); debug(S); int ans = S.size(); cout << ans << endl; }
#include "bits/stdc++.h" #define rep(i,n) for(int i = 0; i < (n); ++i) using namespace std; typedef long long int ll; typedef pair<ll, ll> P; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } int main(){ cin.tie(0); ios::sync_with_stdio(false); string s; cin >> s; ll n = s.size(); ll S = 0, P = 0; rep(i,n){ if(s[i] == 'S') ++S; else{ if(S > 0){ ++P; --S; } } chmax(S, 0LL); } ll ans = n - P*2; cout << ans << endl; return 0; }
1
#include<iostream> #include<algorithm> #include<vector> #include<numeric> #include<string> #include<cmath> #include<set> #include<queue> #include<deque> #include<bitset> #include<iomanip> #include<cctype> #include<map> #include<cstring> #include<bitset> #include<cassert> #define rep(i,n) for(int (i)=0;(i)<(n);(i)++) #define rep2(i,a,b) for (int (i)=a;(i)<(b);(i)++) #define all(x) (x).begin(),(x).end() using namespace std; using ll = long long int; const int inf = 1001001000; const long long int Inf = 1001001001001001000; void print(vector<vector<int>> a){ for (int i = 0; i < a.size(); i++) { for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<vector<long long int>> a){ for (int i=0;i<a.size();i++){ for (int j=0;j<a[i].size();j++){ cout << a[i][j] << " "; } cout << endl; } } void print(vector<int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(vector<long long int> a){ int n = a.size(); for (int j=0;j<n;j++) { if (j != n-1) cout << a[j] << " "; else cout << a[j] << endl; } } void print(set<int> a){ for (auto x:a) cout << x << " "; cout << endl; } //ワーシャルフロイト法のテンプレ.graphに最短経路が格納される. //負の重みがある場合inf + const = inf に気を付けること. //graphは隣接行列. void WF(vector<vector<int>> &graph){ int n = graph.size(); for (int k = 0; k < n; k++){ for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]); } } } } //素因数分解をする関数. map<long long int, int> prime_factorization(long long int a) { map<long long int, int>ans; for (long long i = 2; i*i <= a; ++i) { while (a%i == 0) { ans[i]++; a /= i; } } if (a != 1)ans[a]++; return ans; } int main() { ll x; cin >> x; ll ans = (x / 11) * 2; x = x % 11; if (x > 6) ans += 2; else if (x > 0) ans++; cout << ans << endl; return 0; }
#include<iostream> #include<algorithm> #include<vector> #include<string> #include<utility> #include<map> #include<set> #include <sstream> #include<queue> #include<stack> #include<functional> #include<math.h> #include <iomanip> #include <numeric> #include <iterator> #include <math.h> #include <list> using namespace std; int main(void){ long x; cin >> x; long neko = x / 11; long amari = x % 11; cout << neko*2 + ((amari == 0) ? 0 :((amari > 6)? 2 : 1 ))<< endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define dump(...) cout<<"# "<<#__VA_ARGS__<<'='<<(__VA_ARGS__)<<endl #define repi(i,a,b) for(int i=int(a);i<int(b);i++) #define peri(i,a,b) for(int i=int(b);i-->int(a);) #define rep(i,n) repi(i,0,n) #define per(i,n) peri(i,0,n) #define all(c) begin(c),end(c) #define mp make_pair #define mt make_tuple using uint=unsigned; using ll=long long; using ull=unsigned long long; using vi=vector<int>; using vvi=vector<vi>; using vl=vector<ll>; using vvl=vector<vl>; using vd=vector<double>; using vvd=vector<vd>; using vs=vector<string>; template<typename T1,typename T2> ostream& operator<<(ostream& os,const pair<T1,T2>& p){ return os<<'('<<p.first<<','<<p.second<<')'; } template<typename Tuple> void print_tuple(ostream&,const Tuple&){} template<typename Car,typename... Cdr,typename Tuple> void print_tuple(ostream& os,const Tuple& t){ print_tuple<Cdr...>(os,t); os<<(sizeof...(Cdr)?",":"")<<get<sizeof...(Cdr)>(t); } template<typename... Args> ostream& operator<<(ostream& os,const tuple<Args...>& t){ print_tuple<Args...>(os<<'(',t); return os<<')'; } template<typename Ch,typename Tr,typename C> basic_ostream<Ch,Tr>& operator<<(basic_ostream<Ch,Tr>& os,const C& c){ os<<'['; for(auto i=begin(c);i!=end(c);++i) os<<(i==begin(c)?"":" ")<<*i; return os<<']'; } constexpr int INF=1e9; constexpr int MOD=1e9+7; constexpr double EPS=1e-9; struct FenwickTree{ vector<ll> data; FenwickTree(int n):data(n+1){} void Add(int i,int x){ for(i++;i<data.size();i+=i&-i) data[i]+=x; } ll Sum(int i){ ll res=0; for(;i;i-=i&-i) res+=data[i]; return res; } ll Sum(int i,int j){ return Sum(j)-Sum(i); } }; ll InversionNumber(const vi& a) { FenwickTree ft(a.size()); ll res=0; rep(i,a.size()){ res+=ft.Sum(a[i],a.size()); ft.Add(a[i],1); } return res; } bool test(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst) { int n=src.size(); rep(i,n){ int x,y,z; tie(x,y,z)=src[i]; src[i]=x<z?mt(x,y,z):mt(z,y,x); tie(x,y,z)=dst[i]; dst[i]=x<z?mt(x,y,z):mt(z,y,x); } sort(all(src)); sort(all(dst)); return src==dst; } tuple<bool,bool> calc(vector<tuple<int,int,int>> src,vector<tuple<int,int,int>> dst) { int m=src.size(),flip=0; rep(i,m){ int x,y,z; tie(x,y,z)=src[i]; src[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x)); tie(x,y,z)=dst[i]; dst[i]=x<z?mt(x,y,z):(flip++,mt(z,y,x)); } map<tuple<int,int,int>,int> f; rep(i,m) f[dst[i]]=i; vi a(m); rep(i,m) a[i]=f[src[i]]; return mt(InversionNumber(a)&1,flip&1); } bool solve(int n,vvi grid) { array<bool,2> inv,flip; rep(i,2){ vector<tuple<int,int,int>> src,dst; rep(j,n) if(j%2==i){ if(j/2%2==0){ src.emplace_back(mt(3*j+1,3*j+2,3*j+3)); dst.emplace_back(mt(grid[0][j],grid[1][j],grid[2][j])); } else{ src.emplace_back(mt(3*j+3,3*j+2,3*j+1)); dst.emplace_back(mt(grid[2][j],grid[1][j],grid[0][j])); } } if(!test(src,dst)) return false; tie(inv[i],flip[i])=calc(src,dst); } return inv[0]==flip[1]&&inv[1]==flip[0]; } int main() { for(int n;cin>>n&&n;){ vvi grid(3,vi(n)); rep(i,3) rep(j,n) cin>>grid[i][j]; cout<<(solve(n,grid)?"Yes":"No")<<endl; } }
/* Author: QAQ-Automaton LANG: C++ PROG: e.cpp Mail: [email protected] */ #include<bits/stdc++.h> #define debug(...) fprintf(stderr,__VA_ARGS__) #define DEBUG printf("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define Debug debug("Passing [%s] in LINE %d\n",__FUNCTION__,__LINE__) #define all(x) x.begin(),x.end() #define x first #define y second using namespace std; typedef long long ll; typedef pair<int,int> pii; const signed inf=0x3f3f3f3f; const double eps=1e-8; const double pi=acos(-1.0); template<class T>int chkmin(T &a,T b){return a>b?a=b,1:0;} template<class T>int chkmax(T &a,T b){return a<b?a=b,1:0;} template<class T>T sqr(T a){return a*a;} template<class T>T mmin(T a,T b){return a<b?a:b;} template<class T>T mmax(T a,T b){return a>b?a:b;} template<class T>T aabs(T a){return a<0?-a:a;} template<class T>int dcmp(T a,T b){return a>b;} template<int *a>int cmp_a(int x,int y){return a[x]<a[y];} #define min mmin #define max mmax #define abs aabs namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; // getchar #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++) // print the remaining part inline void flush () { fwrite (obuf, 1, oS - obuf, stdout); oS = obuf; } // putchar inline void putc (char x) { *oS ++ = x; if (oS == oT) flush (); } // input a signed integer inline void read (signed &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } inline void read (long long &x) { for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15); x *= f; } inline void read (char &x) { x=gc(); } inline void read(char *x){ while((*x=gc())=='\n' || *x==' '||*x=='\r'); while(!(*x=='\n'||*x==' '||*x=='\r'))*(++x)=gc(); *x=0; } template<typename A,typename ...B> inline void read(A &x,B &...y){ read(x);read(y...); } // print a signed integer inline void write (signed x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); } inline void write (long long x) { if (!x) putc ('0'); if (x < 0) putc ('-'), x = -x; while (x) qu[++ qr] = x % 10 + '0', x /= 10; while (qr) putc (qu[qr --]); } inline void write (char x) { putc(x); } inline void write(const char *x){ while(*x){putc(*x);++x;} } inline void write(char *x){ while(*x){putc(*x);++x;} } template<typename A,typename ...B> inline void write(A x,B ...y){ write(x);write(y...); } //no need to call flush at the end manually! struct Flusher_ {~Flusher_(){flush();}}io_flusher_; } using io :: read; using io :: putc; using io :: write; int a[100005][4],qaq[2]; int n; struct BIT{ int s[100005]; void add(int x){ for(;x<=n;x+=x&-x)s[x]^=1; } int sum(int x){ int y=0;for(;x;x^=x&-x)y^=s[x];return y; } }; BIT bit[2]; void gg(){write("No\n");exit(0);} int main(){ #ifdef QAQAutoMaton freopen("e.in","r",stdin); freopen("e.out","w",stdout); #endif read(n); for(int j=1;j<=3;++j) for(int i=0;i<n;++i){read(a[i][j]);--a[i][j];} for(int i=0;i<n;++i) if(a[i][1]/3 != a[i][2]/3 || a[i][2]/3 != a[i][3]/3){ gg(); } else if((a[i][1]/3-i)&1){ gg(); } else if(a[i][2]%3 !=1){ gg(); } else{ qaq[i&1]^=a[i][1]%3/2; qaq[(i&1)^1]^=bit[i&1].sum(n-a[i][1]/3); bit[i&1].add(n-a[i][1]/3); } write(qaq[0]||qaq[1]?"No\n":"Yes\n"); return 0; }
1
//#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define rep(i, n) for(int i=0; i<n; ++i) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() using namespace std; using ll = int64_t; using P = pair<int, int>; using vi = vector<int>; using vvi = vector<vi>; int main() { int n; cin >> n; int a, b, c; a = 0; b = n/2; c = n; vector<char> st; printf("%d\n", 0); fflush(stdout); string S; cin >> S; if(S[0] == 'V') return 0; st.push_back(S[0]); printf("%d\n", b); fflush(stdout); cin >> S; if(S[0] == 'V') return 0; st.push_back(S[0]); rep(i, 18) { if((((b-a)&1) && st[0] != st[1]) || (!((b-a)&1) && st[0] == st[1])) { a = b; b = (c+b)/2; st[0] = st[1]; } else { c = b; b = (c+a)/2; } printf("%d\n", b); fflush(stdout); cin >> S; if(S[0] == 'V') return 0; st[1] = S[0]; } }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for (int i = 0; i < (n); ++i) #define ALL(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; const double EPS = 1e-10; const int INF = 1e9; const ll LINF = 1e15; const int MOD = 1000000007; const double PI = acos(-1); int dx[4] = {0,1,0,-1}; int dy[4] = {1,0,-1,0}; int main() { int n; cin >> n; cout << 0 << endl; string s; cin >> s; if (s == "Vacant") return 0; vector<string> v(n); v[0] = s; int l = 0; int r = n; int p = (l+r)/2; cout << p << endl; while (cin >> s, s != "Vacant") { v[p] = s; int x = p - l - 1; int y = r - p - 1; if ((v[l] == s && x % 2 == 0) || (v[l] != s && x % 2 == 1)) { r = p; p = (l+r)/2; } else { l = p; p = (l+r)/2; } cout << p << endl; } }
1
#include <iostream> #include <map> #include <set> #include <algorithm> #include <vector> #include <sstream> #include <string> #include <functional> #include <queue> #include <deque> #include <stack> #include <limits> #include <unordered_map> #include <unordered_set> #include <math.h> #include <fstream> #include <iterator> #include <random> #include <chrono> #define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i) #define set_map_includes(set, elt) (set.find((elt)) != set.end()) #define readint(i) int i; cin >> i #define readll(i) ll i; cin >> i #define readdouble(i) double i; cin >> i #define readstring(s) string s; cin >> s typedef long long ll; using namespace std; long long gcd(long long a, long long b) { if (a == 0) { return b; } return gcd(b % a, a); }; class Factorial { public: ll modd = 1000 * 1000 * 1000 + 7; vector<ll> bin; Factorial(int n) : bin(n+1, 0) { bin[0] = 1; forr(i,1,n) { bin[i] = i*bin[i-1]; bin[i] = bin[i] % modd; } } ll val(int i) { return bin[i]; } }; int main() { cout.precision(17); ll modd = 1000 * 1000 * 1000 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int> rand_gen(0, modd); // rand_gen(rng) gets the rand no ll infinit = 10000000000000000; // readint(test_cases); int test_cases = 1; forr(t, 1, test_cases) { readint(n); readstring(s); ll ret = 1; ll continuing_intervals = 0; forr(i,0,s.size()) { bool need_one = (s[i] == 'B'); if (need_one == continuing_intervals % 2) { ret *= continuing_intervals; ret = ret % modd; continuing_intervals--; if (continuing_intervals < 0) { ret = 0; break; } } else { continuing_intervals++; } } if (continuing_intervals != 0) {ret = 0;} Factorial fa(n+1); ret *= fa.val(n); ret = ret % modd; cout << ret << endl; } return 0; }
#include <algorithm> #include <cmath> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <tuple> #include <vector> using namespace std; using ll = int64_t; #define rep(i, j, n) for (int i = j; i < (int)n; ++i) #define rrep(i, j, n) for (int i = (int)n - 1; j <= i; --i) template <typename T> std::ostream& operator<<(std::ostream& os, std::vector<T>& a) { os << "{"; for (size_t i = 0; i < a.size(); ++i) os << (i > 0 ? "," : "") << a[i]; return os << "}"; } constexpr ll MOD = 1000000007; constexpr int INF = 0x3f3f3f3f; constexpr ll INFL = 0x3f3f3f3f3f3f3f3fLL; template <typename T, typename C> ostream& operator<<(ostream& os, const multiset<T, C>& v) { os << '{'; for (auto it = v.begin(); it != v.end(); it++) { if (it != v.begin()) os << ", "; os << *it; } os << '}'; return os; } template <typename T1, typename T2, typename C> ostream& operator<<(ostream& os, const map<T1, T2, C>& mp) { os << '['; for (auto it = mp.begin(); it != mp.end(); it++) { if (it != mp.begin()) os << ", "; os << it->first << ": " << it->second; } os << ']'; return os; } int main() { int h, w; cin >> h >> w; // 右へ動く最小値を保管 // ある点から始まった場合、 // 複数の最短経路結果を持ちたい。 // map<int, int> mp; multiset<int> st; rep(i, 0, w) { mp[i] = i; st.insert(0); } rep(i, 0, h) { int a, b; cin >> a >> b; --a; // [a, b]はいけない auto itr = mp.lower_bound(a); int most_right = -1; while (itr != mp.end() && itr->first <= b) { most_right = max(most_right, itr->second); int x = itr->first - itr->second; st.erase(st.find(x)); itr = mp.erase(itr); } if (most_right != -1 && b < w) { st.insert(b - most_right); mp[b] = most_right; } if (st.size() > 0) cout << *st.begin() + i + 1 << '\n'; else cout << -1 << '\n'; } return 0; }
0
#include<stdio.h> int d,t,s; int main() { scanf("%d %d %d", &d, &t, &s); d=d+s-1; if(t>=(d/s))printf("Yes"); else printf("No"); }
#include<bits/stdc++.h> using namespace std; int main(){ int d,t,s; cin>>d>>t>>s; float time = (d/(s*1.0)); if(time<=t) cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
1
#include<iostream> #include<sstream> #include<algorithm> #include<climits> #include<cmath> #include<cstdio> #include<cstdlib> #include<ctime> #include<cfloat> #include<functional> #include<map> #include<string> #include<cstring> #include<vector> #include<queue> #include<stack> #include<deque> #include<set> #include<bitset> #include<list> #include<numeric> #include<complex> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> i_i; typedef pair<long long, int> ll_i; typedef pair<double, int> d_i; typedef pair<long long, long long> ll_ll; typedef pair<double, double> d_d; typedef vector<int> Vint; #define PI 3.141592653589793238462643383279 #define mod 1000000007LL #define rep(i, n) for(i = 0;i < n;++i) #define rep1(i, n) for(i = 1;i < n;++i) #define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j) #define per(i, n) for(i = n - 1;i > -1;--i) #define int(x) int x; scanf("%d",&x) #define int2(x, y) int x, y; scanf("%d%d",&x, &y) #define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z) #define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z) #define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z) #define scn(n, a) rep(i, n)cin >> a[i] #define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i] #define pri(x) cout << x << "\n" #define pri2(x, y) cout << x << " " << y << "\n" #define pri3(x, y, z) cout << x << " " << y << " " << z << "\n" #define pb push_back #define mp make_pair #define all(a) (a).begin(),(a).end() #define endl "\n" #define kabe puts("---------------------------") #define kara puts("") #define debug(x) cout << " --- " << x << "\n" #define debug2(x, y) cout << " --- " << x << " " << y << "\n" #define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n" #define X first #define Y second #define eps 0.0001 #define prid(x) printf("%.15lf\n", x) signed main(void){ int i, j, k; for(int testcase = 0;testcase >= 0;testcase++){ int2(n, m); int a[n], cnt[n]; scn(n, a); rep(i, n)cnt[i] = 0; rep(i, m){ int(b); rep(j, n)if(b >= a[j]){ cnt[j]++; break; } } int ma = 0; rep1(i, n)if(cnt[ma] < cnt[i])ma = i; pri(ma + 1); /*/ //*/ break; } return 0; }
#include <bits/stdc++.h> #define rep(i,n)for(long long i=0;i<(n);i++) using namespace std; typedef unsigned long long ull; typedef long long ll; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } const ll MOD=1e9+7; const ll INF=1e18; const double pi=acos(-1); int dx[4]={1,0,-1,0}; int dy[4]={0,1,0,-1}; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n,m; cin >> n >> m; vector<int>a(n); rep(i,n) cin >> a[i]; vector<int>ans(n); rep(i,m){ int x; cin >> x; ll kyogi=n; rep(j,n){ if(a[j]<=x){ kyogi=min(kyogi,j); } } ans[kyogi]++; } cout << max_element(ans.begin(),ans.end())-ans.begin()+1 << endl; }
1
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;} return 0;} template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;} return 0;} #define rep(i,n) for(int i=0;i<(int)(n);i++) #define all(vec) vec.begin(),vec.end() typedef long long ll; typedef pair<ll,ll> l_l; typedef pair<int,int> i_i; const ll mod=1e9+7; const int inf=1<<30; vector<vector<int> > to; int color[100010]; bool isbi=true; void dfs(int v,int c=0){ color[v]=c; rep(i,to[v].size()){ int nv=to[v][i]; if(color[nv]==-1) dfs(nv,1-c); else if(color[nv]==1-c) continue; else isbi=false; } } int main(){ ll N,M; cin >> N >> M; to.resize(N); rep(i,N) color[i]=-1; rep(i,M){ int a,b; cin >> a >> b; a--; b--; to[a].push_back(b); to[b].push_back(a); } dfs(0); ll ans; if(isbi){ ll b=0,w=0; rep(i,N){ if(color[i]==0) b++; else w++; } ans=b*w-M; } else{ ans=N*(N-1)/2-M; } cout << ans << endl; }
#include <iostream> #include <string> #include <vector> #include <algorithm> #include <stack> #include <queue> #include <cmath> #include <tuple> #include <cstdio> #include <bitset> #include <sstream> #include <iterator> #include <numeric> #include <map> #include <cstring> #include <set> #include <functional> #include <iomanip> using namespace std; #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!! #ifdef DEBUG_ #define dump(x) cerr << #x << " = " << (x) << endl; #else #define dump(x) ; #endif #define equals(a,b) (fabs((a)-(b)) < EPS) #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define SZ(x) ((int)(x).size()) #define pb push_back #define eb emplace_back //#define int long long typedef long long LL; typedef vector<int> VI; typedef vector<LL> VL; typedef vector<VI> VVI; typedef vector<VL> VVL; typedef vector<string> VS; typedef pair<int, int> PII; typedef pair<LL, LL> PLL; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } template <typename T> std::string printVector(const std::vector<T> &data) { std::stringstream ss; std::ostream_iterator<T> out_it(ss, ", "); ss << "["; std::copy(data.begin(), data.end() - 1, out_it); ss << data.back() << "]"; return ss.str(); } template <typename T> void print_array(const T &ary, int size){ REP(i,size){ cout << ary[i] << " "; } cout << endl; } const int mod = 1e9+7; const LL LINF = 1001002003004005006ll; const int INF = 1001001001; const double EPS = (1e-10); const long double PI = 3.14159265358979323846264338327950288419716939937510582097494459230781640628620899; int dx[] = {0,0,-1,1}; int dy[] = {-1,1,0,0}; template< typename Monoid > struct SegmentTree { using F = function< Monoid(Monoid, Monoid) >; int sz; vector< Monoid > seg; const F f; const Monoid M1; SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) { sz = 1; while(sz < n) sz <<= 1; seg.assign(2 * sz, M1); } void set(int k, const Monoid &x) { seg[k + sz] = x; } void build() { for(int k = sz - 1; k > 0; k--) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } void update(int k, const Monoid &x) { k += sz; seg[k] = x; while(k >>= 1) { seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); } } Monoid query(int a, int b) { Monoid L = M1, R = M1; for(a += sz, b += sz; a < b; a >>= 1, b >>= 1) { if(a & 1) L = f(L, seg[a++]); if(b & 1) R = f(seg[--b], R); } return f(L, R); } Monoid operator[](const int &k) const { return seg[k + sz]; } }; signed main(int argc, char const *argv[]) { cin.tie(0); ios::sync_with_stdio(false); cout << setprecision(12); int N,Q; cin >> N >> Q; VI C(N); REP(i,N) { cin >> C[i]; C[i]--; } vector<tuple<int,int,int>> RL; REP(i,Q){ int l,r; cin >> l >> r; l--; r--; RL.eb(r,l,i); } sort(RL.begin(),RL.end()); VI memo(N,-1); // その色が最後に出たindex VI ans(Q); auto f = [](int a, int b){return a+b;}; SegmentTree<int> seg(N,f,0); // 一番右側の色 int cur = -1; REP(i,Q){ int l,r,ind; tie(r,l,ind) = RL[i]; while(cur < r){ cur++; if(memo[C[cur]] == -1){ seg.update(cur,1); }else{ seg.update(memo[C[cur]], 0); seg.update(cur,1); } memo[C[cur]] = cur; } ans[ind] = seg.query(l,r+1); } REP(i,Q){ cout << ans[i] << endl; } }
0
//O(n^2) #include<stdio.h> #include<math.h> long long n,rui[10000]; long long max( long long x,long long y ){ if( x > y ) return x; return y; } int main() { long long i,j; long long ans; while( scanf("%lld",&n) != EOF ) { if( n == 0 )break; ans = -10000000000; for( i = 0;i < n;i++ ){ scanf("%lld",rui+i ); if( i )rui[i] += rui[i-1]; } for( i = 0;i < n;i++ ){ for( j = i;j < n;j++ ){ if( i ) ans = max( ans, rui[j]-rui[i-1] ); else ans = max( ans,rui[j] ); } } printf("%lld\n",ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int N, M; vector<int> ItemInfo, PersonInfo; void solve() { int curValue; int total[1000]; memset(total, 0, sizeof(total)); for (int i = 0; i < M; ++i) { curValue = PersonInfo[i]; for (int j = 0; j < N; ++j) { if (ItemInfo[j] <= curValue) { //cout << "=" << j + 1 << endl; ++total[j]; break; } } } cout << max_element(total, total + N) - total + 1 << endl; } int main() { int num; cin >> N >> M; for (int i = 0; i < N; ++i) { cin >> num; ItemInfo.push_back(num); } for (int i = 0; i < M; ++i) { cin >> num; PersonInfo.push_back(num); } solve(); return 0; }
0
#include<bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9+7; int main(){ int x,y; cin>>x>>y; int sum{}; if(x==1&&y==1) sum += 400000; sum += max(0, 400000-x*100000); sum += max(0, 400000-y*100000); cout << sum << endl; }
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main(){ int N, X, T; cin >> N >> X >> T; int S = 0; S += N / X * T; if (N % X >= 1) { S += T; } cout << S << endl; }
0
#include<iostream> const int INF = 1 << 24, MAX_N = 101; int n, cost[MAX_N][MAX_N], d[MAX_N], used[MAX_N]; int dijkstra(int s, int g){ for(int i=1;i<=n;i++){ d[i] = INF; used[i] = false; } d[s] = 0; while(true){ int v = -1; for(int i=1;i<=n;i++){ if(!used[i] && (v == -1 || d[i] < d[v])){ v = i; } } if(v == -1)break; used[v] = 1; for(int i=1;i<=n;i++){ d[i] = std::min(d[i], d[v] + cost[v][i]); } } return d[g]; } int main(){ int k; while(std::cin >> n >> k, n){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ cost[i][j] = INF; } } while(k--){ int f, c, d, e; std::cin >> f; if(f == 0){//注文が入りましたよー std::cin >> c >> d; int res = dijkstra(c, d); if(res == INF){ std::cout << -1 << std::endl; }else{ std::cout << dijkstra(c, d) << std::endl; } }else{//運行情報 std::cin >> c >> d >> e; cost[c][d] = std::min(cost[c][d], e); cost[d][c] = cost[c][d]; } } } }
#include <bits/stdc++.h> using namespace std; #define ll long long #define fr first #define sc second #define ii pair < ll, ll > const int N = (int)5e3 + 7; const ll inf = (ll)1e18 + 7; ii p[N]; ll dp[N]; bool comp (const ii &a, const ii &b) { return a.fr + a.sc < b.fr + b.sc; } main() { int n; scanf ("%d", &n); for (int i = 0; i < n; i++) { scanf ("%lld %lld", &p[i].fr, &p[i].sc); } sort(p, p + n, comp); for (int i = 0; i <= n; i++) { dp[i] = inf; } dp[0] = 0; for (int i = 0; i < n; i++) { for (int j = i; j >= 0; j--) { if (p[i].fr >= dp[j]) { dp[j + 1] = min(dp[j + 1], dp[j] + p[i].sc); } } } int m = n; while (dp[m] == inf) m--; printf ("%d", m); }
0
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i = 0; i < n; i++) #define Rep(i,n) for(ll i = 1; i < n; i++) #define sz(x) int(x.size()) #define all(v) v.begin(), v.end() #define rall(v) v.rbegin(), v.rend() #define YesorNo(a) printf(a ? "Yes\n" : "No\n") #define endl '\n' #define fi first #define se second using ll = long long; using P = pair<int,int>; using Pl = pair<ll,ll>; template<class T> using V = vector<T>; const int dx[] = {0,1,0,-1,1,1,-1,-1}; const int dy[] = {1,0,-1,0,1,-1,-1,1}; const int inf = (1<<30)-1; const ll infll = (1LL<<62)-1; ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;} template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } int main(){ ll n, a, b; cin >> n >> a >> b; V<ll> x(n); rep(i,n) cin >> x[i]; V<ll> dist(n-1); rep(i,n) { dist[i] = x[i+1] - x[i]; dist[i] *= a; } sort(all(dist)); int idx = 0; ll ans = 0; while(dist[idx] < b && idx < n-1) { ans += dist[idx]; idx++; } ans += 1LL * (n-1 - idx) * b; cout << ans << endl; }
#include <iostream> #include <vector> #include <map> #include <set> #include <algorithm> #include <fstream> #include <string> #include <math.h> #include <cstdlib> #include <istream> #include <sstream> #include <cctype> #include <iomanip> //cout << fixed << setprecision(20) << *** << endl; #define rep(i,N) for(int i=0;i<(int)N;++i) typedef long long ll; using namespace std; int main() { ll A,B,M,t; vector<ll>AA,BB,ANS; cin>>A>>B>>M; rep(i,A){ cin>>t; AA.push_back(t); } rep(i,B){ cin>>t; BB.push_back(t); } rep(i,M){ ll x,y,z,zz; cin>>x>>y>>z; zz=AA[x-1]+BB[y-1]-z; ANS.push_back(zz); } sort(AA.begin(),AA.end()); sort(BB.begin(),BB.end()); ANS.push_back(AA[0]+BB[0]); sort(ANS.begin(),ANS.end()); cout<<ANS[0]<<endl; }
0
#include <stdio.h> int main(void) { int n, pm, pe, pj; char c; while (1) { scanf("%d", &n); if (n == 0) { break; } else { for(int i = 0; i < n; ++i) { scanf("%d %d %d", &pm, &pe, &pj); if (pm == 100 || pe == 100 || pj == 100 || (pm + pe) / 2 >= 90 || (pm + pe + pj) / 3 >= 80) { c = 'A'; } else if (((pm + pe + pj) / 3 >= 70) || ((pm + pe + pj) / 3 >= 50 && (pm >= 80 || pe >= 80))) { c = 'B'; } else { c = 'C'; } printf("%c\n", c); } } } return 0; }
#include <cstdio> #include <cstdlib> #include <cmath> #include <climits> #include <cfloat> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <memory> #include <string> #include <vector> #include <algorithm> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <stack> #include <queue> using namespace std; typedef long long LL; typedef pair<int, int> PII; static const double EPS = 1e-5; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) int p[3]; void judge(){ REP(j,3){ if(p[j]==100){ cout<<"A"<<endl; return; } } if(p[0]+p[1]>=180){ cout<<"A"<<endl; return; } int sum=0; REP(j,3)sum+=p[j]; if(sum>=240){ cout<<"A"<<endl; return ; } if(sum>=210){ cout<<"B"<<endl; return ; } if(sum>=150&&(p[0]>=80||p[1]>=80)){ cout<<"B"<<endl; return ; } cout<<"C"<<endl; } int main(void){ int n; while(cin>>n){ if(n==0) break; REP(i,n){ REP(j,3)cin>>p[j]; judge(); } } return 0; }
1
#include <iostream> #include <algorithm> using namespace std; bool Bsearch(int key, int A[], int len) { int l = 0, r = len - 1; while (l <= r) { int mid = l + (r - l) / 2; if (A[mid] > key) r = mid - 1; else if (A[mid] < key) l = mid + 1; else return true; } return false; } int main() { int n, *S; int q, *T; int ans = 0; cin >> n; S = new int[n]; for (int i = 0; i < n; i++) cin >> S[i]; cin >> q; T = new int[q]; for (int i = 0; i < q; i++) cin >> T[i]; sort(S, S + n); for (int i = 0; i < q; i++) for (int j = 0; j < n; j++) if (Bsearch(T[i], S, n) == true) { ans++; break; } cout << ans << endl; delete[] S; delete[] T; return 0; }
#include <iostream> #include <vector> #include <map> #include <set> #include <string> #include <utility> #include <algorithm> #include <cstdio> #include <iomanip> #include <queue> #include <deque> #include <stack> #include <fstream> #include <cmath> #include <random> #include <complex> #include <functional> #include <bitset> #include <sstream> #include <cassert> #include <time.h> #define ll int64_t #define Rep(i, n) for (ll i = 0; i < n; i++) using namespace std; typedef vector<ll> vec; typedef vector<vec> mat; const ll inf = 1LL << 60; template<class T> inline void chmin(T& a, T b) { if (a > b) { a = b; } } template<class T> inline void chmax(T& a, T b) { if (a < b) { a = b; } } void printvec (vec &v) { Rep (i, (ll)v.size()) { cout << v[i] << " \n"[i+1 == (ll)v.size()]; } } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N; cin >> N; vec A(N); Rep (i, N) { cin >> A[i]; } ll ans = 0; Rep (i, 60) { ll cnt = 0; Rep (j, N) { if ((A[j] >> i) & 1) { cnt++; } } if (cnt % 2 == 1) { // cout << "i=" << i << "\n"; ans += (1LL << i); Rep (j, N) { A[j] &= ~(1LL << i); } } } // printvec(A); vec basis; for (ll e : A) { for (ll b : basis) { chmin(e, e ^ b); } if (e) { basis.push_back(e); } } sort(basis.begin(), basis.end()); ll n = basis.size(); Rep (i, n) { Rep (j, n) { if (i == j) continue; chmin(basis[j], basis[j] ^ basis[i]); } } // printvec(basis); ll tmp = 0; for (ll e : basis) { tmp = tmp ^ e; } ans += 2 * tmp; cout << ans << "\n"; // for (ll i = n-1; i >= 0; i--) { // ans += 2 * basis[i]; // for (ll digit = 59; digit >= 0; digit--) { // if (basis[i] >> digit & 1) { // for (ll j = i-1; j >= 0; j--) { // if (basis[j] >> digit & 1) basis[j] = 0; // } // } // } // } // cout << ans << "\n"; }
0
#include <algorithm> #include <cmath> #include <deque> #include <iomanip> #include <iostream> #include <map> #include <queue> #include <set> #include <stack> #include <tuple> #include <vector> using namespace std; typedef long long ll; ll const INF = 1LL << 60; int main() { ll N; cin >> N; string ret; vector<string> seats(N, ""); cout << 0 << endl; cin >> ret; if (ret == "Vacant") return 0; seats[0] = ret; cout << N - 1 << endl; cin >> ret; if (ret == "Vacant") return 0; seats[N - 1] = ret; int l = 0, r = N - 1; int mid; while (true) { mid = (l + r) / 2; cout << mid << endl; cout << flush; cin >> ret; if (ret == "Vacant") { return 0; } else { seats[mid] = ret; if ((((mid - l) % 2 == 1) && seats[l] == seats[mid]) || (((mid - l) % 2 == 0) && seats[l] != seats[mid])) { r = mid; } else { l = mid; } } } return 0; }
#include <iostream> using namespace std; int main(){ int n, a, b; while (true) { int point_a = 0, point_b = 0; cin >> n; if (cin.eof()||n==0) { break; } for(int i =0;i<n;i++){ cin >> a >> b; if(a==b){ point_a += a; point_b += b; } else if (a>b){ point_a += a + b; } else { point_b += a + b; } } cout << point_a << " " << point_b << endl; } }
0
#include<bits/stdc++.h> #define fi first #define se second #define pb push_back #define SZ(x) ((int)x.size()) #define L(i,u) for (register int i=head[u]; i; i=nxt[i]) #define rep(i,a,b) for (register int i=(a); i<=(b); i++) #define per(i,a,b) for (register int i=(a); i>=(b); i--) using namespace std; typedef long long ll; typedef unsigned int ui; typedef pair<int,int> Pii; typedef vector<int> Vi; template<class T> inline void read(T &x){ x=0; char c=getchar(); int f=1; while (!isdigit(c)) {if (c=='-') f=-1; c=getchar();} while (isdigit(c)) {x=x*10+c-'0'; c=getchar();} x*=f; } template<class T> inline void umin(T &x, T y){x=x<y?x:y;} template<class T> inline void umax(T &x, T y){x=x>y?x:y;} inline ui R() { static ui seed=416; return seed^=seed>>5,seed^=seed<<17,seed^=seed>>13; } const int N = 120000; int gcd(int a, int b){return !b?a:gcd(b,a%b);} int n,a[N]; void print(int x){ printf("%s",x==1?"First":"Second");exit(0); } int main() { read(n);rep(i,1,n)read(a[i]); int now=1; while(1){ int cnt=0,flag=1; rep(i,1,n)cnt+=!(a[i]&1),flag&=a[i]==1; if(flag)print(3-now); if(cnt&1)print(now); if(n-cnt!=1)print(3-now); rep(i,1,n)if(a[i]&1){if(a[i]==1)print(3-now);a[i]--;} int g=0;rep(i,1,n)g=gcd(g,a[i]); rep(i,1,n)a[i]/=g;now=3-now; } return 0; }
// C++ 14 #include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) { os << "["; for (int i = 0; i < v.size(); ++i) { os << v[i]; if (i != v.size() - 1) os << ", "; } os << "]"; return os; } template <typename T> void print(T v, string s = "\n") { cout << v << s; } template <typename T> void in(T &v) { cin >> v; } #define ll long long #define loop(__x, __start, __end) for(int __x = __start; __x < __end; __x++) int main() { int n; ll m; in(n),in(m); vector<ll> A(n); loop(i,0,n) in(A[i]); loop(i,0,n) A[i]%=m; ll rem = 0; map<ll, ll> C; C[rem]++; loop(i,0,n) { rem = (rem + A[i]) % m; C[rem]++; } ll ans = 0; for (auto &&c: C) { ans += (c.second * (c.second - 1)) / 2; } print(ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; #define all(a)a.begin(),a.end() using ll=long long; const int INF = 1<<30; const ll INFll =1LL<<62; const int mod= int(1e9)+7; using P = pair<ll,ll>; using ld=long double; int main(){ ll r;cin>>r; if(r<1200)puts("ABC"); else if(r<2800)puts("ARC"); else puts("AGC"); return 0; }
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~= *$* WRITER:kakitamasziru/OxOmisosiru *$* ~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/ #ifdef LOCAL_JUDGE #define _GLIBCXX_DEBUG //FOR THE DEBUG! COMMENT OUT THIS WHEN SUBMITTING! #endif /* I REALLY HOPE MY WISH REACH YOU , ATCODER'S ONLINE JUDGE */ #define WOULD #define YOU #define PLEASE #define ACCEPT #define MY #define SUBMISSION /* I REALLY HOPE MY WISH REACH YOU , ATCODER'S ONLINE JUDGE */ #include <iostream> // cout, endl, cin #include <string> // string, to_string, stoi #include <vector> // vector #include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound #include <utility> // pair, make_pair #include <tuple> // tuple, make_tuple #include <cstdint> // int64_t, int*_t #include <iomanip> #include <limits>//setprecision //#include <cstdio> // printf #include <map> // map #include <queue> // queue, priority_queue #include <set> // set #include <stack> // stack #include <deque> // deque #include <math.h>//pow,,, #include <cmath>//abs,,, #include <bitset> // bitset //It is so troublesome that I include bits/stdc++.h ! using namespace std; template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const long long INF = 100100100100; const long long MOD = 1000000007; typedef pair<int,int> P; //Solve N^M. This, mod_pow use Iterative Square Method. long long mod_pow(long long N, long long M) { if (M == 0) return 1; long long res = mod_pow((N * N) % MOD, M / 2); //最下位ビット(*N)が1の時は単独でNをかける if (M & 1) res = (res * N) % MOD; return res %= MOD; } long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } long long lcm(long long a, long long b) { return a * b / gcd(a, b); } int main() { string S;cin >> S; if(S == "zyxwvutsrqponmlkjihgfedcba"){ cout << -1 << endl; return 0; } char C; int index; vector<bool> kind(26,false); for(int i = 0;i<S.size();i++){ C = S.at(i); index = C-97; kind.at(index) = true; } if(S.size() != 26){ for(int i = 0;i<26;i++){ if(!kind.at(i)){ char plus = i+97; cout << S + plus << endl; return 0; } } } for(int i = S.size()-1;i>=0;i--){ C = S.at(i); index = C-97; for(int j = 0;j<26;j++){ if(!kind.at(j) && j > index){ S.pop_back(); char last = j+97; S += last; cout << S << endl; return 0; } } S.pop_back(); kind.at(index) = false; } }
0
#include <iostream> #include <algorithm> #include <map> #include <vector> #include <string> #include <cstdio> #include <cmath> using namespace std; int main(void) { int l[10]; int a,b; while(~scanf("%d,", &l[0])) { double sum = 0; for(int i=1; i<10; i++) scanf("%d,", &l[i]); scanf("%d,%d",&a,&b); for(int i=0; i<10; i++) sum += (double)l[i]; double c=0, d=0; int i; for(i=0; i<10; i++) { c += l[i]; d += (double)l[i]/a * b; if(c + d >= sum) break; } printf("%d\n", i+1); } return 0; }
#include<cstdio> #include<iostream> using namespace std; int main(){ int a[10]; int v1,v2; while(scanf("%d",a)!=EOF){ for(int i=1;i<10;i++){ scanf(",%d",&a[i]); a[i] += a[i-1]; } scanf(",%d,%d\n",&v1,&v2); int ans; for(int i=9;i>=0;i--){ if(v1*a[9] <= a[i]*(v1+v2))ans=i; } cout<<ans+1<<endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() {int N, K; cin >> N >> K; int L[N]; for (int &l : L) cin >> l; sort(L, L + N); cout << accumulate(L + N - K, L + N, 0);}
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define INF 1e12 #define PB push_back #define PF push_front #define fi first #define se second #define pii pair<int, int> #define pll pair<ll, ll> #define vi vector<int> #define vpi vector<pii> #define vll vector<ll> #define vpl vector<pll> #define vvi vector<vector<int>> #define vvl vector<vector<ll>> #define MX(x) *max_element(all(x)) #define MN(x) *min_element(all(x)) #define ios ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0) #define pr_d(x) cout << fixed << setprecision(15) << x << endl #define ud(c, x) distance(c.begin(), upper_bound(all(c), x)) #define ld(c, x) distance(c.begin(), lower_bound(all(c), x)) #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i, a, b) for (int i = (a); i < (b); ++i) #define rep3(i, n) for (int i = (n - 1); i >= 0; --i) #define rep4(i, a, b) for (int i = (a); i > (b); --i) #define pb push_back #define out(x) cout << x << "\n" bool odd(int i) { return i % 2; } #define all(v) v.begin(), v.end() #define size(x) int(x.size()) int gcd(int a, int b) { return __gcd(a, b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } void Yes_No(bool f) { if (f) printf("Yes\n"); else printf("No\n"); } void YES_NO(bool f) { if (f) printf("YES\n"); else printf("NO\n"); } template <typename T> void deb1(T x) { cout << x << "\n"; } template <typename T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template <typename T> bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; } //-------------------ここから回答する----------------------- void solve(void) { int n, k; cin >> n >> k; vi l(n); rep(i, n) cin >> l[i]; sort(all(l), greater<int>()); int ans = 0; rep(i, k) ans += l[i]; out(ans); } int main(void) { solve(); }
1
#include <bits/stdc++.h> using namespace std; #define repr(i,a,b) for (int i=a; i<b; i++) #define rep(i,n) for (int i=0; i< n; i++) template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } const long long INF = 1LL << 60; #define PI 3.14159265359 template<typename T> void remove(std::vector<T>& vector, unsigned int index) { vector.erase(vector.begin() + index); } int gcd(int a, int b){ if(b == 0) return a; else return gcd(b, a%b); } long long lcm (int a, int b){ return (long long) a*b /gcd(a,b); } int main(){ int A, B; cin >> A >>B; if(A>B)cout << A+(A-1) << endl; else if (A==B)cout << A+B << endl; else cout << B+B-1 << endl; }
#include <iostream> #include <iomanip> #include <sstream> #include <stdio.h> #include <list> #include <vector> #include <stack> #include <queue> #include <map> #include <set> #include <algorithm> #include <math.h> #include <utility> #include <string> #include <ctype.h> #include <cstring> #include <cstdio> #include <sstream> #include <functional> using namespace std; #define FOR(i,k,n) for(int i = (k); i < (n); i++) #define REP(i,n) FOR(i,0,n) #define INF 114514810 #define ll long long #define ALL(a) (a).begin(),(a).end() #define SORT(v) sort(ALL(v)) //#define scanf scanf_s int main() { int n, p; while (cin >> n >> p, n) { int m[55] = {}; int wan = p; int i, j; j = 0; for (i = 1; i < 1000005; i++) { if (wan>0) { wan--; m[j]++; if (m[j] == p) { cout << j << endl; break; } } else { wan += m[j]; m[j] = 0; } j++; if (j >= n) j = 0; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using P = pair<int, int>; ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; } ll LCM(ll a, ll b) { return a/GCD(a, b)*b; } int n, u, v; vector< vector<int> > graphlist(100100, vector<int>()); vector<int> takahashi(100100, -1); vector<int> aoki(100100, -1); struct posdata { int p; int dist; }; int main() { cin >> n >> u >> v; u--; v--; takahashi.at(u) = 0; aoki.at(v) = 0; for(int i = 0; i < n-1; ++i) { int aa, bb; cin >> aa >> bb; aa--; bb--; graphlist.at(aa).emplace_back(bb); graphlist.at(bb).emplace_back(aa); } queue<posdata> tque; posdata tfirst; tfirst.p = u; tfirst.dist = 0; tque.push(tfirst); while(!tque.empty()) { posdata now = tque.front(); tque.pop(); for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) { int np = graphlist.at(now.p).at(i); int ndist = now.dist+1; if(takahashi.at(np) == -1) { takahashi.at(np) = ndist; posdata next; next.p = np; next.dist = ndist; tque.push(next); } } } queue<posdata> aque; posdata afirst; afirst.p = v; afirst.dist = 0; aque.push(afirst); while(!aque.empty()) { posdata now = aque.front(); aque.pop(); for(int i = 0; i < (int)graphlist.at(now.p).size(); ++i) { int np = graphlist.at(now.p).at(i); int ndist = now.dist+1; if(aoki.at(np) == -1) { aoki.at(np) = ndist; posdata next; next.p = np; next.dist = ndist; aque.push(next); } } } int amax = 0; for(int i = 0; i < n; ++i) { if(aoki.at(i) > takahashi.at(i)) { chmax(amax, aoki.at(i)); } } cout << amax-1 << endl; }
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define LL long long #define M 200020 using namespace std; LL read(){ LL nm=0,fh=1; char cw=getchar(); for(;!isdigit(cw);cw=getchar()) if(cw=='-') fh=-fh; for(;isdigit(cw);cw=getchar()) nm=nm*10+(cw-'0'); return nm*fh; } LL ans,sum0,sum1,sig; LL n,m,fs[M],nt[M<<1],to[M<<1],col[M],f[M],sz[M],tmp; LL fd(LL x){return x==f[x]?x:f[x]=fd(f[x]);} void link(LL x,LL y){nt[tmp]=fs[x],fs[x]=tmp,to[tmp++]=y;} void merge(LL x,LL y){x=fd(x),y=fd(y);if(x!=y) f[x]=y,sz[y]+=sz[x];} bool check(LL x,LL now){ if(col[x]) return col[x]==now; col[x]=now; for(LL i=fs[x];i!=-1;i=nt[i]) if(!check(to[i],3-now)) return false; return true; } int main(){ n=read(),m=read(),memset(fs,-1,sizeof(fs)); for(LL i=1;i<=n;i++) f[i]=i,sz[i]=1; for(LL i=1;i<=m;i++){ LL x=read(),y=read(); link(x,y),link(y,x),merge(x,y); } for(LL i=1;i<=n;i++) if(fd(i)==i&&sz[i]==1) sig++; for(LL i=1;i<=n;i++){ if(fd(i)!=i||sz[i]==1) continue; if(check(i,1)) sum0++; else sum1++; } ans+=sig*((n<<1)-1)-sig*(sig-1); ans+=((sum0*sum0)<<1); ans+=sum1*(sum0<<1)+sum1*sum1; printf("%lld\n",ans); return 0; }
0
#include <stdio.h> #include <string.h> int main(){ char tulisanA[11]; char tulisanB[11]; char tulisanC[11]; scanf("%s %s %s", &tulisanA, &tulisanB, &tulisanC); int panjangA = strlen (tulisanA); int panjangB = strlen(tulisanB); if(tulisanA[panjangA-1] == tulisanB[0] && tulisanB[panjangB-1] == tulisanC[0]){ puts("YES"); } else{ puts("NO"); } return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define rev(i, n) for(int i = (int)(n - 1); i >= 0; i--) #define rev1(i, n) for(int i = (int)(n); i > 0; i--) #define pb push_back #define all(v) (v).begin(), (v).end() #define resort(v) sort((v).rbegin(), (v).rend()) #define vi vector<int> #define vvi vector<vector<int>> #define vc vector<char> #define vvc vector<vector<char>> #define vb vector<bool> #define vvb vector<vector<bool>> using ll = long long; using P = pair<int, int>; /* ----------------よく使う数字や配列----------------- */ int dx[] = {1,0,-1,0}; int dy[] = {0,1,0,-1}; constexpr ll mod = 1e9+7; constexpr ll inf = LLONG_MAX; constexpr long double eps = DBL_EPSILON; constexpr long double pi = 3.141592653589793238462643383279; /* ----------------------end----------------------- */ /* --------------------ライブラリ-------------------- */ ll fact(int i) { //階乗 if (i == 0) return 1; return (fact(i - 1)) * i; } ll gcd(ll a, ll b) { //最大公約数 if(b == 0) return a; return gcd(b, a % b); } ll lcm(ll a, ll b) { //最小公倍数 return a * b / gcd(a, b); } int keta(ll n) { //桁数を求める if(n == 0) return 1; int count = 0; while(n != 0) { n /= 10; count++; } return count; } ll ketasum(ll n) { //各桁の和 ll sum = 0; while(n != 0) { sum += n % 10; n /= 10; } return sum; } /* ----------------------end----------------------- */ int main() { int a,b,c;cin >> a >> b >> c; if(a<c&&c<b||b<c&&c<a) puts("Yes"); else puts("No"); return 0; }
0
#include<bits/stdc++.h> using namespace std; set<unsigned int> s; int n,m; int cards[100]; bool used[100]; void solve(int cnt,string a){ string tmp=a; if(cnt==m){ s.insert(atoi(a.c_str())); return; } for(int i=0;i<n;i++){ if(used[i]==true)continue; used[i]=true; stringstream ss; ss << cards[i]; a+=ss.str(); solve(cnt+1,a); a=tmp; used[i]=false; } } int main(){ while(cin>>n,n){ s.clear(); cin>>m; for(int i=0;i<n;i++){ used[i]=false; cin>>cards[i]; } solve(0,""); // for(auto i:s) // cout << i << endl; cout << s.size() << endl; } return 0; }
#include<bits/stdc++.h> using namespace std; vector <string> card,memo; string suzi,m; int n,k,ans,p,but; int main(){ while(1){ cin >> n >> k; if(n==0&&k==0)break; for(int i=0;i<n;i++){ cin >> m; card.push_back(m); } if(k==2){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i!=j){ suzi=card[i]+card[j]; but=0; while(1){ if(p==ans)break; if(memo[p]!=suzi); else { but++; break; } p++; } p=0; if(but==0){ ans++; memo.push_back(suzi); } } but=0; } } } if(k==3){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ for(int a=0;a<n;a++){ if(i!=j&&j!=a&&a!=i){ suzi=card[i]+card[j]+card[a]; but=0; while(1){ if(p==ans)break; if(memo[p]!=suzi); else { but++; break; } p++; } p=0; if(but==0){ ans++; memo.push_back(suzi); } } but=0; } } } } if(k==4){ for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ for(int a=0;a<n;a++){ for(int b=0;b<n;b++){ if(i!=j&&i!=a&&i!=b&&j!=a&&j!=b&&a!=b){ suzi=card[i]+card[j]+card[a]+card[b]; but=0; while(1){ if(p==ans)break; if(memo[p]!=suzi); else { but++; break; } p++; } p=0; if(but==0){ ans++; memo.push_back(suzi); } } but=0; } } } } } cout << ans << endl; memo.clear(); card.clear(); ans=0; } return 0; }
1
#include <iostream> #include <cstdio> using namespace std; int ary[15]; char str[7][12]={"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"}; int main(){ ary[1]=2; for(int i=1;i<12;i++){ if(i==4||i==6||i==9||i==11){ ary[i+1]=(ary[i]+30)%7; } else if(i==2){ ary[i+1]=(ary[i]+29)%7; } else{ ary[i+1]=(ary[i]+31)%7; } } int x,y; while(scanf("%d %d",&x,&y)!=EOF){ if(x==0&&y==0) break; cout<<str[(ary[x]+y)%7]<<endl; } return 0; }
#include <bits/stdc++.h> #define FOR(v, a, b) for(int v = (a); v < (b); ++v) #define FORE(v, a, b) for(int v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for(int v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c,x) ((c).find(x) != (c).end()) #define LLI long long int #define fst first #define snd second #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(x) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;} template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;} template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;} template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;} template <typename T, typename U> T& chmin(T &a, const U &b){return a = (a<=b?a:b);} template <typename T, typename U> T& chmax(T &a, const U &b){return a = (a>=b?a:b);} template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);} const LLI mod = 1e9+7; LLI dp[300][101000]; int main(){ cin.tie(0); ios::sync_with_stdio(false); int N,X; while(cin >> N >> X){ vector<LLI> S(N); cin >> S; sort(ALL(S)); reverse(ALL(S)); fill_array(dp,0); dp[0][X] = 1; REP(i,N){ FORE(j,0,X){ (dp[i+1][j%S[i]] += dp[i][j]) %= mod; (dp[i+1][j] += dp[i][j]*(N-i-1)%mod) %= mod; } } LLI ans = 0; FORE(i,0,X) (ans += i*dp[N][i]) %= mod; cout << ans << endl; } return 0; }
0
#include <algorithm> #include <bitset> #include <climits> #include <cmath> #include <cstdio> #include <iomanip> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <sstream> #include <stack> #include <string> #include <unordered_map> #include <vector> #define MOD 1000000007 #define MOD2 998244353 #define int long long #define double long double #define EPS 1e-9 //#define PI 3.14159265358979 #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; template < typename T > ostream &operator<<(ostream &os, const vector< T > &A) { for (int i = 0; i < A.size(); i++) os << A[i] << " "; os << endl; return os; } template <> ostream &operator<<(ostream &os, const vector< vector< int > > &A) { int N = A.size(); for (int i = 0; i < N; i++) { for (int j = 0; j < A[i].size(); j++) os << A[i][j] << " "; os << endl; } return os; } template < typename T, typename U > ostream &operator<<(ostream &os, const pair< T, U > &p) { os << "(" << p.first << "," << p.second << ")"; return os; } typedef pair< int, int > pii; typedef long long ll; struct edge { int from, to, d, c, i; edge(int _from = 0, int _to = 0, int _d = 0, int _c = 0) { from = _from; to = _to; d = _d; c = _c; } bool operator<(const edge &rhs) const { return (d == rhs.d) ? (c < rhs.c) : (d < rhs.d); } }; struct aabb { int x1, y1, x2, y2; aabb(int x1, int y1, int x2, int y2) : x1(x1), y1(y1), x2(x2), y2(y2) {} }; typedef vector< edge > edges; typedef vector< edges > graph; struct flow { int to, cap, rev, cost; flow(int to = 0, int cap = 0, int rev = 0, int cost = 0) : to(to), cap(cap), rev(rev), cost(cost) {} }; typedef vector< vector< flow > > flows; const int di[4] = {0, -1, 0, 1}; const int dj[4] = {-1, 0, 1, 0}; const int ci[5] = {0, 0, -1, 0, 1}; const int cj[5] = {0, -1, 0, 1, 0}; const ll LINF = LLONG_MAX / 2; const int INF = INT_MAX / 2; const double PI = acos(-1); int pow2(int n) { return 1LL << n; } template < typename T, typename U > bool chmin(T &x, const U &y) { if (x > y) { x = y; return true; } return false; } template < typename T, typename U > bool chmax(T &x, const U &y) { if (x < y) { x = y; return true; } return false; } template < typename A, size_t N, typename T > void Fill(A (&array)[N], const T &val) { fill((T *)array, (T *)(array + N), val); } struct initializer { initializer() { cout << fixed << setprecision(20); } }; initializer _____; int N, M, K, T, Q, H, W; signed main() { cin >> T; while (T--) { cin >> N; vector< int > A(N); rep(i, N) cin >> A[i]; string S; cin >> S; vector< int > es; int ans = 0; for (int i = N - 1; i >= 0; i--) { int v = A[i]; sort(es.rbegin(), es.rend()); for (auto e : es) { chmin(v, v ^ e); } if (S[i] == '0') { if (v != 0) { es.push_back(v); } } else { if (v) { ans = 1; break; } } } cout << ans << endl; } return 0; }
#include<iostream> #include<vector> #include<string> #include<cstdlib> #include<sstream> #include<queue> #include<deque> #include<cmath> #include<algorithm> #include<stack> #include<map> #include<set> #include<iomanip> #define INF 2147483647 #define lli long long int #define MOD 1000000007 using namespace std; typedef vector<int> vi; typedef vector< vector<int> > vvi; typedef pair<int,int> pii; typedef vector<pair<int,int>> vpii; //A???65 a???97 int main() { vector<char> eki(52); eki[0] = 65; eki[26] = 97; for (int i = 1; i < 26; i++){ eki[i] = eki[i-1] + 1; eki[26+i] = eki[26+i-1] + 1; } while(1){ int n; cin >> n; if (n == 0) break; vi key(n); for (int i = 0; i < n; i++){ cin >> key[i]; } string station; cin >> station; string ans = ""; for (int i = 0; i < station.size(); i++){ if (isupper(station[i])){ ans += eki[(station[i]-'A'-key[i%n]+52)%52]; }else{ ans += eki[(station[i]-'a'+26-key[i%n]+52)%52]; } } cout << ans << endl; } return 0; }
0
#include<iostream> #include<algorithm> using namespace std; int main(){ long long int A[5001]={0}; long long int n,MAX = -100000; while(1){ cin >> n; if( n == 0 ) break; for(int i = 1; i <= n; i++){ cin >> A[i]; } MAX = -100000; for(int i = 1; i <= n; i++){ A[i] = max(A[i], A[i] + A[i-1]); MAX = max(A[i], MAX); } cout << MAX << endl; } return 0; }
// 2016-12-05 #include <iostream> #include <cmath> using namespace std; int main() { int e, x, y, z, m; while (cin >> e, e) { m = 1000000; for (z = 0; z * z * z <= e; z++) { y = (int)sqrt(e - z * z * z); x = e - z * z * z - y * y; m = min(m, x + y + z); } cout << m << endl; } return 0; }
0
#include<vector> #include<iostream> #include<algorithm> #include<cstring> using namespace std; const int MAX=10000; int V; vector<int> G[MAX]; vector<int> rG[MAX]; vector<int> vs; bool used[MAX]; int cmp[MAX]; void add_edge(int from,int to) { G[from].push_back(to); rG[to].push_back(from); return; } void dfs(int v) { used[v]=true; for(int i=0;i<G[v].size();i++) { if(!used[G[v][i]])dfs(G[v][i]); } vs.push_back(v); return; } void rdfs(int v,int k) { used[v]=true; cmp[v]=k; for(int i=0;i<rG[v].size();i++) { if(!used[rG[v][i]])rdfs(rG[v][i],k); } return; } int ssc()//Strongly Connected Component { memset(used,0,sizeof(used)); vs.clear(); for(int v=0;v<V;v++) { if(!used[v])dfs(v); } memset(used,0,sizeof(used)); int k=0; for(int i=vs.size()-1;i>=0;i--) { if(!used[vs[i]])rdfs(vs[i],k++); } return k; } int main() { int E; cin>>V>>E; for(int i=0;i<E;i++) { int u,v; cin>>u>>v; add_edge(u,v); } int k=ssc(); int Q;cin>>Q; for(int i=0;i<Q;i++) { int u,v; cin>>u>>v; cout<<(cmp[u]==cmp[v])<<endl; } return 0; }
#include "bits/stdc++.h" using namespace std; vector<long long>Col(100005); struct Edge { int to, id; Edge(int to, long long id) :to(to), id(id) {} }; vector<Edge>g[100005]; void dfs(int v, long long d = 0, int p = -1) { Col[v] = d; for (Edge e : g[v]) { if (p == e.to) { continue; } dfs(e.to, d + e.id, v); } } int main() { int N; cin >> N; for (int n = 0; n < N - 1; ++n) { int a, b; long long c; cin >> a >> b >>c; a--; b--; g[a].emplace_back(b, c); g[b].emplace_back(a, c); } int Q,K; cin >> Q >> K; K--; dfs(K); for (int i = 0;i<Q;++i) { int x, y; cin >> x >> y; x--; y--; cout << Col[x]+Col[y]<<endl; } return 0; }
0
#include <iostream> #include <iomanip> using namespace std; int main() { double n; cin >> n; cout << fixed << setprecision(20) << (2 * (n) * 3.14159265359); return 0; }
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <iomanip> using namespace std; using ll = long long; int main() { int x; cin >> x; int a = x / 500; int b = (x % 500) / 5; cout << a * 1000 + b * 5 << endl; }
0
#include <iostream> using namespace std; #define Q 1000000007 int main(void){ int n; int a[200010]; long long acc[200010], ans; cin >> n; acc[0] = 0; for (int i=1; i<=n; i++){ cin >> a[i]; acc[i] = acc[i-1] + a[i]; acc[i] %= Q; } ans = 0; for (int i=1; i<=n-1; i++){ ans += (a[i]*((acc[n]-acc[i]+Q)%Q)) % Q; ans %= Q; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define IN freopen("perimetric_chapter_1_input.txt","r",stdin) #define OUT freopen("output.txt","w",stdout) #define pb push_back #define mp make_pair #define FOR(i,a,b) for(i=a ; i<=b ; i++) #define DBG printf("Hi\n") #define i64 long long int #define ui64 unsigned long long int #define xx first #define yy second #define ln 17 #define off 2002 #define sq(x) ((x)*(x)) #define FASTIO ios_base::sync_with_stdio(false); cin.tie(NULL) using namespace __gnu_pbds; using namespace std ; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); typedef tree< i64, null_type, less<i64>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; typedef pair<int,int> pii; #define log 20 #define mod 1000000007LL #define INF 1000000000000000000LL #define maxn 300005 const long double eps = 1e-9 ; class numberTheory{ public: numberTheory(){} pii extendedEuclid(i64 a, i64 b) { // returns x, y | ax + by = gcd(a,b) if(b == 0) return pii( a >= 0 ? 1 : -1 , 0LL); else { pii d = extendedEuclid(b, a % b); return pii(d.yy, d.xx - d.yy * (a / b)); } } i64 modularInverse(i64 a, i64 n) { pair<i64,i64> ret = extendedEuclid(a, n); return ((ret.xx % n) + n) % n; } i64 bigMod(i64 a, i64 n , i64 m) { if(n==0) return 1 ; i64 ret = bigMod(a,n/2,m) ; ret = (ret*ret)%m ; if(n%2) ret = (ret*a)%m ; return ret ; } }; const i64 N = 5000000 ; i64 fact[N+5] , invFact[N+5] ; i64 C(i64 n, i64 r) { if( n<0 || r<0 || r>n ) return 0 ; i64 ret = (fact[n]*invFact[r])%mod ; return (ret*invFact[n-r])%mod ; } void preprocess() { numberTheory nt ; fact[0] = 1 ; for(i64 x=1 ; x<=N ; x++) fact[x] = (fact[x-1]*x)%mod ; invFact[N] = nt.modularInverse(fact[N],mod) ; for(i64 x=N-1 ; x>=0 ; x--) invFact[x] = (invFact[x+1]*(x+1) )%mod ; } int dp[2005][2005] ; int main() { preprocess() ; int n , k ; scanf("%d %d",&n,&k) ; k-- ; if(k==0) { printf("1\n") ; return 0 ; } dp[0][0] = 1 ; for(int i=1 ; i<=n ; i++) { dp[i][0] = (1LL*i*((1LL*dp[i-1][0]*C( i*k - 1 , k-1 ))%mod))%mod ; // dp[i][0] = ( dp[i][0] + dp ) for(int j=1 ; j<=i ; j++) { dp[i][j] = (1LL*i*((1LL*dp[i-1][j]*C( i*k + j - 1 , k-1 ))%mod))%mod ; dp[i][j] = ( dp[i][j] + dp[i][j-1] )%mod ; } } printf("%d\n",dp[n][n]) ; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long LL; struct Dimension { int N; vector< LL >basis; Dimension(int N) : N(N), basis(N, 0) { } bool add(LL x) { for (int i = 0; i < N; i++) { if (x&(1LL<<i)) { if (basis[i]) x ^= basis[i]; else { basis[i] = x; return true; } } } return false; } }; char solve() { int n; cin >> n; vector< LL >A(n); for (int i = 0; i < n; i++) cin >> A[i]; string s; cin >> s; Dimension d(60); for (int i = n-1; i >= 0; i--) { if (d.add(A[i])) { if (s[i]=='1') return '1'; } } return '0'; } int main() { int t; cin >> t; while (t--) { cout << solve() << "\n"; } return 0; }
typedef long long ll; typedef long double ld; #include <bits/stdc++.h> using namespace std; void solve(){ ll n; std::cin >> n; vector<ll> a(n); for (int i = 0; i < n; i++) { std::cin >> a[i]; } string s; std::cin >> s; vector<vector<ll>> dp(n+1); dp[n] = {0}; for (int i = n-1; i >= 0; i--) { ll v = a[i]; for (auto e : dp[i+1]) { v = min(v,v^e); } if(v==0){ dp[i] = dp[i+1]; }else{ if(s[i]=='0'){ dp[i+1].push_back(v); sort(dp[i+1].rbegin(),dp[i+1].rend()); for (auto e : dp[i+1]) { dp[i].push_back(e); } }else{ std::cout << 1 << std::endl; return; } } } std::cout << 0 << std::endl; } int main() { int t; std::cin >> t; for (int iii = 0; iii < t; iii++) { solve(); } }
1
#include<iostream> #include<map> #include<string> #include<vector> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) using namespace std; typedef long long ll; typedef int Def; typedef vector<Def> vi; typedef pair<Def,Def> pii; int main(){ int n; while(cin>>n,n){ vi in(n); rep(i,n)cin>>in[i]; string s; cin>>s; rep(i,s.size()){ int t=in[i%n]; if(t>=26){ t-=26; if(islower(s[i]))s[i]=toupper(s[i]); else s[i]=tolower(s[i]); } if(islower(s[i])){ s[i]-=t; if(s[i]<'a')s[i]='Z'-('a'-s[i])+1; }else{ s[i]-=t; if(s[i]<'A')s[i]='z'-('A'-s[i])+1; } } cout<<s<<endl; } }
#include <cstdio> using namespace std; int main() { int n; while (scanf("%d", &n), n!=0) { int k[100]; char s[101]; for (int i=0;i<n;i++) scanf("%d", &k[i]); scanf("%s", s); for (int i=0;s[i]!='\0';i++) { int c = s[i]; for (int j=0;j<k[i%n];j++) { if (c == 'a') c = 'Z'; else if (c == 'A') c = 'z'; else c--; } putchar(c); } putchar('\n'); } return 0; }
1
#include <bits/stdc++.h> #include <stdlib.h> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vec> mat; typedef vector<double> Vec; typedef vector<Vec> Mat; typedef pair<ll,ll> P; typedef pair<double,ll> Pd; typedef pair<double,double> PD; typedef priority_queue<P,vector<P>,greater<P> > P_queue; typedef priority_queue<Pd,vector<Pd>,greater<Pd> > Pd_queue; const ll MOD=998244353; const ll mod=1000000007; const ll INF=1e15; const double DEL=1e-6; #define _GLIBCXX_DEBUG #define REP(i,a,b) for(int i=(int)a;i<(int)b;i++) #define rep(i,n) REP(i,0,n) #define pb push_back #define mp make_pair #define ALL(a) a.begin(),a.end() #define U_ERASE(V) V.erase(unique(ALL(V)), V.end()) void Add(ll &a, ll b){ a=(a+b)%mod; return; } void Pro(ll &a, ll b){ a=(a*b)%mod; return; } ll H,W; vector<P> d; vec ans; void init(){ cin>>H>>W; rep(i,H){ ll x,y; cin>>x>>y; x--; y--; d.pb(mp(x,y)); ans.pb(-1); } } void Solve(){ set<P> x; multiset<ll> y; rep(i,W) { P f=mp(i,i); x.insert(f); y.insert(0); } rep(i,H){ ll Biggest=-INF; for(auto itr=x.lower_bound(mp(d[i].first,-INF));itr!=x.end();){ ll R=(*itr).first, L=(*itr).second; // cout<<i<<':'<<R<<' '<<L<<endl; if(R>d[i].second+1) break; Biggest=max(Biggest,L); auto itr1=y.lower_bound(R-L); y.erase(itr1); itr=x.erase(itr); } if(Biggest!=-INF) if(d[i].second!=W-1) {x.insert(mp(d[i].second+1,Biggest)); y.insert(d[i].second+1-Biggest);} if(y.size()) ans[i]=*(y.begin()); else break; } rep(i,H) if(ans[i]>=0) ans[i]+=(i+1); } int main(){ init(); Solve(); rep(i,H) cout<<ans[i]<<endl; }
#include<bits/stdc++.h> using namespace std; #define Nitroboost ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL) #define ll long long int const int mod=1e9+7; const int N=1e6+5; int main() { Nitroboost; #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif int n,m; cin>>n>>m; map<int,int>mp; for(int i=1;i<=m;i++) mp[i]=i; multiset<int>v; for(int i=1;i<=m;i++) v.insert(0); for(int i=1;i<=n;i++) { int l,r; cin>>l>>r; r++; auto it=mp.lower_bound(l); int t=-1; while(it!=mp.end() && it->first<=r) { t=max(t,it->second); v.erase(v.find(it->first-it->second)); mp.erase(it++); } if(t!=-1 && r<=m) { v.insert(r-t); mp[r]=t; } if(v.size()>0) cout<<i+(*v.begin())<<"\n"; else cout<<"-1\n"; } }
1
#include <bits/stdc++.h> #define SIZE 300005 #define MOD 1000000007LL #define INF 1 << 30 #define LLINF 1LL << 60 #define REP(i,n) for(int i=0;i<n;i++) #define FOR(i,a,b) for(int i=a;i<=b;i++) #define DOWN(i,b,a) for(int i=b;i>=a;i--) #define SET(a,c) memset(a,c,sizeof a) #define FORALL(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i) #define FOREACH(i,c) for(auto (i) : (c)) #define BIT(i,j) ((i)>>(j))&1 #define ALL(o) (o).begin(), (o).end() #define ERASE(o) (o).erase(unique((o).begin(),(o).end()), (o).end()) #define SQ(x) ((x)*(x)) using namespace std; typedef long long ll; typedef valarray<int> Array; typedef pair<ll,ll> Pll; typedef pair<int, int> Pii; typedef pair<double, double> Pdd; template<typename T> inline void priv(vector<T>a){REP(i,a.size()){cout<<a[i]<<((i==a.size()-1)?"\n":" ");}} ll gcd(ll a,ll b){int c=max(a,b);int d=min(a,b);return c==0||d==0?c:gcd(c%d,d);} ll lcm(ll a,ll b){return a==0||b==0?0:a*b/gcd(a,b);} int a[144]; void flip(int x, int y) { if(x>11||y>11||x<0||y<0) return; if(a[x+y*12]) { a[x+y*12] = 0; flip(x+1,y); flip(x-1,y); flip(x,y+1); flip(x,y-1); } } void solve() { int cnt = 0; REP(i,144) if(a[i]) { cnt++; flip(i%12,i/12); } cout << cnt << endl; } int main() { string s; int i = 0; while(cin >> s) { REP(j,12) a[i*12+j] = s[j]-'0'; i=(++i)%12; if(i==0) solve(); } return 0; }
#include<iostream> #include<vector> #include<string> using namespace std; int tisei[12][12]; void shima(int x, int y) { tisei[x][y] = 0; if (x < 11 && tisei[x+1][y]) shima(x + 1, y); if (x > 0 && tisei[x - 1][y]) shima(x -1, y); if (y < 11 && tisei[x][y+1]) shima(x , y+1); if (y > 0 && tisei[x][y-1]) shima(x, y-1); } int main() { vector<int>n; while (1) { for (int a = 0; a < 12; a++) { string c; cin >> c; if (cin.fail())goto end; for (int b = 0; b < 12; b++) { tisei[a][b] = c[b] - 48; } } int s = 0; for (int d = 0; d < 12; d++) { for (int e = 0; e < 12; e++) { if (tisei[d][e]) { s++; shima(d, e); } } } n.push_back(s); } end:; for (int j : n)cout << j << endl; }
1
#include<iostream> #include<tuple> #include<vector> #include<algorithm> #include<iterator> #include<cmath> #include<functional> #include<map> using namespace std; int main(){ int N; cin >> N; map<int, int> o, e; for(int i=0; i<N/2; i++){ int ot, et; cin >> ot; cin >> et; o[ot]++; e[et]++; } int r = N; int ei = 0; int oi = 0; int so = 0; int se = 0; int me = 0; int mo = 0; for (auto it = o.begin(); it != o.end(); it++){ if(mo < it->second){ so = mo; mo = it->second; oi = it->first; }else if(so < it -> second){ so = it->second; } } for (auto it = e.begin(); it != e.end(); it++){ if(me < it->second){ se = me; me = it->second; ei = it->first; }else if(so < it -> second){ se = it->second; } } if(ei == oi){ if (so == 0 && se == 0){ r -= N/2; }else if(so > se){ r -= so + me; }else{ r -= se + mo; } }else{ r -= me + mo; } cout << r << endl; }
#include<bits/stdc++.h> using namespace std; #define int long long int N; int odd[100010], even[100010]; signed main() { cin >> N; for(int i = 0; i < N; i++) { int a; cin >> a; if(i & 1) even[a]++; else odd[a]++; } int odd_index = -1, odd_max = -1; for(int i = 0; i < 100010; i++) { if(odd_max < odd[i]) { odd_max = odd[i]; odd_index = i; } } int even_index = -1, even_max = -1; for(int i = 0; i < 100010; i++) { if(even_max < even[i]) { even_max = even[i]; even_index = i; } } if(even_index != odd_index) { cout << N - even_max - odd_max << endl; return 0; } int odd_max2 = -1; for(int i = 0; i < 100010; i++) { if(odd_max2 < odd[i] && i != odd_index) { odd_max2 = odd[i]; } } int even_max2 = -1; for(int i = 0; i < 100010; i++) { if(even_max2 < even[i] && i != even_index) { even_max2 = even[i]; } } cout << min(N - even_max - odd_max2, N - even_max2 - odd_max) << endl; return 0; }
1
#include <cstdio> const int mod = 1e9 + 7; const int MAXN = 45, MAXS = ( 1 << 17 ) + 5; template<typename _T> void read( _T &x ) { x = 0;char s = getchar();int f = 1; while( s > '9' || s < '0' ){if( s == '-' ) f = -1; s = getchar();} while( s >= '0' && s <= '9' ){x = ( x << 3 ) + ( x << 1 ) + ( s - '0' ), s = getchar();} x *= f; } template<typename _T> void write( _T x ) { if( x < 0 ){ putchar( '-' ); x = ( ~ x ) + 1; } if( 9 < x ){ write( x / 10 ); } putchar( x % 10 + '0' ); } int f[MAXN][MAXS]; int N, X, Y, Z, st, all; int bit( const int i ) { return 1 << i - 1; } bool chk( const int S ) { return ( S & st ) == st; } void add( int &x, const int v ) { x = ( x + v >= mod ? x + v - mod : x + v ); } int insert( const int S, const int v ) { return ( S << v | bit( v ) ) & all; } int main() { read( N ), read( X ), read( Y ), read( Z ); int up = 1 << X + Y + Z; all = ( bit( X + Y + Z ) << 1 ) - 1; st = bit( Z ) | bit( Y + Z ) | bit( X + Y + Z ); f[0][0] = 1; for( int i = 0 ; i < N ; i ++ ) for( int S = 0 ; S < up ; S ++ ) if( f[i][S] && ! chk( S ) ) for( int k = 1, T ; k <= 10 ; k ++ ) if( ! chk( T = insert( S, k ) ) ) add( f[i + 1][T], f[i][S] ); int ans = 1; for( int i = 1 ; i <= N ; i ++ ) ans = 10ll * ans % mod; for( int S = 0 ; S < up ; S ++ ) if( ! chk( S ) ) add( ans, mod - f[N][S] ); write( ans ), putchar( '\n' ); return 0; }
//#pragma GCC target ("avx2") #pragma GCC optimization ("O3") #pragma GCC optimization ("unroll-loops") #include <bits/stdc++.h> #define owo(i,a, b) for(int i=(a);i<(b); ++i) #define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i) #define senpai push_back #define ttgl pair<int, int> #define ayaya cout<<"ayaya~"<<endl using namespace std; /*#include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> using namespace __gnu_pbds; gpu_hash_table<int, int> mp; #define ordered_set tree<ttgl, null_type,less<ttgl>, rb_tree_tag,tree_order_statistics_node_update> */ using ll = long long; using ld = long double; const ll MOD = 924844033; const ll root = 62; int gcd(int a,int b){return b?gcd(b,a%b):a;} ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;} ll modInv(ll a){return binpow(a, MOD-2);} const double PI = acos(-1); const double eps = -1e6; const int INF = 0x3f3f3f3f; const int NINF = 0xc0c0c0c0; const ll INFLL = 0x3f3f3f3f3f3f3f3f; const ll NINFLL = 0xc0c0c0c0c0c0c0c0; const int mxN = 101; int n; int arr[mxN]; int main() { //freopen("file.in", "r", stdin); //freopen("file.out", "w", stdout); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cin.tie(0)->sync_with_stdio(0); cin>>n; int sum = 0; owo(i, 0, n) { cin>>arr[i]; sum+=arr[i]; } int ans = 0; int diff = INF; owo(i, 0, n) { if(abs(arr[i]*n-sum)<diff) { diff = abs(arr[i]*n-sum); ans = i; } } cout<<ans<<"\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int __SIZE = 1 << 18; char ibuf[__SIZE], *iS, *iT; #define ge (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, __SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++) #define ri read_int() #define rl read_ll() #define FILE(s) freopen(s"in", "r", stdin), freopen(s"out", "w", stdout) template<typename T> inline void read(T &x) { char ch, t = 0; x = 0; while(!isdigit(ch = ge)) t |= ch == '-'; while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = ge; x = t ? -x : x; } inline int read_int() { int x; return read(x), x; } inline ll read_ll() { ll x; return read(x), x; } template<typename T> inline void chkmin(T&a, T b) { a = a < b ? a : b; } const int MAXN = 110; int a[MAXN]; int js[MAXN]; int main() { #ifdef LOCAL FILE(""); #endif int cnt = 0; int n = ri; int m = ri; if(m == 1) { if(n == 1) { cout << 1 << endl; cout << 1 << endl; cout << 1 << endl; } else { cout << n << endl; cout << 2 << endl; cout << 1 << ' ' << n - 1 << endl; } } else { for(int i = 1; i <= m; i++) { a[i] = ri; if(a[i] & 1) js[cnt++] = i; } if(cnt > 2) puts("Impossible"); else { if(js[0] && js[0] != 1) swap(a[1], a[js[0]]); if(js[1] && js[1] != m) swap(a[m], a[js[1]]); for(int i = 1; i <= m; i++) printf("%d ", a[i]); puts(""); a[1]++, a[m]--; if(!a[m]) --m; printf("%d\n", m); for(int i = 1; i <= m; i++) printf("%d ", a[i]); puts(""); } } return 0; }
#include <cstdio> #include <iostream> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef long long llint; typedef pair < int, int> pii; typedef pair < llint, llint> pll; const int N = 1e5 + 500; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int n,a[N],nep,ind; llint sum = 0; int main(){ scanf("%d",&n); for(int i = 0;i<n;i++){ scanf("%d",a+i); if(a[i]&1) {nep++;ind=i;} sum += (llint)a[i]; } for(int koji = 1;;koji = !koji){ int allgcd = a[0]; for(int i = 1;i<n;i++) allgcd = __gcd(a[i], allgcd); int nw = 0,ind = 0, jed = 0; llint nsum = -n; for(int i = 0;i<n;i++){ a[i] /= allgcd; nsum += a[i]; if(a[i] == 1) jed++; if(a[i]&1) {nw++;ind = i;} } if(nsum%2 == 1){ if(koji) printf("First\n"); else printf("Second"); break; } else if(nw == 1 && !jed){ a[ind]--; continue; } else{ if(!koji) printf("First\n"); else printf("Second"); break; } } return 0; }
0
#include <bits/stdc++.h> #include <vector> #include <iostream> #include<algorithm> #include<string> #include <map> #include <queue> #include <stack> #include<set> #include<tuple> #define DIV 1000000007 #define TE 2e6+5 using namespace std; using ll = long long; using ldb = long double; int main() { int N; cin >> N; vector<vector<ll>> cnt(64, vector<ll>(30)); for (int i = 0; i < N; i++) { string s; cin >> s; ll temp = 0; int len = s.size(), tens = 0; bool flag = false; for (int j = 0; j < len; j++) { if (s[j] != '.') { temp *= 10; temp += s[j] - '0'; if (flag) tens++; } else flag = true; } for (int i = 0; i < 9 - tens; i++)temp *= 10; //cout << "temp=" << temp << endl; int two = 0, five = 0; while (temp % 2 == 0) { two++, temp /= 2; } while (temp % 5 == 0) { five++, temp /= 5; } cnt[two][five]++; //cout << "two=" << two << " five=" << five << endl; } ll ans = 0; for (int i = 0; i < 64; i++) { for (int j = 0; j < 30; j++) { for (int a = max(0,18 - i); a < 64; a++) { for (int b = max(0,18 - j); b < 30; b++) { ans += cnt[i][j] * (cnt[a][b] + (i == a && j == b ? -1 : 0)); //if (cnt[i][j] > 0 && cnt[a][b]) //cout << "i=" << i << " j=" << j << " a=" << a << " b=" << b << " ans=" << ans << endl; } } } } cout << ans / 2 << endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9+7; // const ll MOD = 998244353; const ll INF = 1ll<<60; #define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() #define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl; int main(int argc, char **argv) { ll N; cin >> N; vector<string> isu(N); ll L = 0; ll R = N; std::cout << L << std::endl; cin >> isu[L]; if (isu[L] == "Vacant") return 0; while (abs(R-L) > -1) { ll mid = (R+L)/2; std::cout << mid << std::endl; cin >> isu[mid]; if (isu[mid] == "Vacant") return 0; if ((mid - L) & 1) { if (isu[mid] == isu[L]) { R = mid; } else { L = mid; } } else { if (isu[mid] == isu[L]) { L = mid; } else { R = mid; } } } std::cout << L << std::endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; using ll=long long; const int mod=1e9+7; const int N = 2e5+5; vector<ll>ans,veca,vecb; vector<int>adj[N]; vector<double>aka; vector<pair<int,double>>dp[N]; vector<bool>vis; void init(int n){ ans.clear();veca.clear();aka.clear();vis.clear();ans.resize(n); vecb.clear(),vecb.resize(n);veca.resize(n);vis.resize(n); for (int i = 0; i <n; i++) { vis[i]=0; } for (int i = 0; i<=n; i++) { adj[i].clear(); } for (int i = 0; i <=n; i++) { dp[i].clear(); } for (int i = 0; i < n; i++) { ans[i]=0; } } int check(int a,int b,int c){ int res=0; if(a+b>c) res=1; return res; } bool valid(int a,int b,int c){ if((a<b)&&(b<c)) return 0; return 1; } void solve(){ int n,flag=0; cin>>n; init(n); for (int i = 0; i <n; i++) { cin>>veca[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if(!valid(i,j,k)){ set<int>st;vector<int>vec; st.insert(veca[i]);st.insert(veca[j]);st.insert(veca[k]); vec.push_back(veca[i]);vec.push_back(veca[j]);vec.push_back(veca[k]); if(st.size()==3){ sort(vec.begin(),vec.end()); flag+=check(vec[0],vec[1],vec[2]); } } } } } cout<<flag<<"\n"; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); int T=1; //cin>>T; while(T--){ solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int N, count; cin >> N; count = 0; vector<int> vec(N); for(int i = 0; i < N; i++){ cin >> vec.at(i); } for(int i = 0; i < N; i++){ for(int j = 0; j < i; j++){ for(int k = 0; k < j; k++){ if((abs(vec.at(i) - vec.at(j)) < vec.at(k) && vec.at(k) < vec.at(i) + vec.at(j)) && vec.at(i) != vec.at(j) && vec.at(j) != vec.at(k)&& vec.at(k) != vec.at(i)) count++; } } } cout << count << endl; }
1
#include <bits/stdc++.h> using namespace std; #define all(v) v.begin(), v.end() #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define intll int long long const int INF = 1e9; const int MOD = 1e9 + 7; int main() { int n ; cin >> n ; string s , t ; cin >> s >> t ; deque<char> S , T ; rep(i,n){ S.push_back(s.at(i)) ; T.push_back(t.at(i)); } if(s == t ) { cout << s.size() <<endl ; return 0 ; } int ans = 2 * n ; rep(i,n){ S.pop_front(); T.pop_back(); if( S == T){ ans = S.size() + 2*(i+1) ; cout << ans <<endl ; return 0 ; } } }
#include <bits/stdc++.h> using namespace std; int main() { int n; string s, t; cin >> n >> s >> t; int cnt = 0; for (int i = 0; i < n; i++) { string subs = s.substr(i, n - i); //後半を抽出 string subt = t.substr(0, n - i); //前半を抽出 if (subs == subt) //抽出した部分がもし重複したら cnt = max(cnt, n - i); //重複部分がながければ長いほど最小になるので、長いものをキープ } cout << 2 * n - cnt << endl; //最大のときは2n。2nから一番長い重複を引く }
1
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 200000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define EQ(a,b) (abs((a)-(b))<EPS) int cost[101][101]; bool used[101]; int d[101]; int n,k; void dijkstra(int s) { fill(d,d+101,INF); fill(used,used+101,false); d[s]=0; for(;;) { int v=-1; for(int u=1;u<=n;u++) { if(!(used[u])&&(v==-1||d[u]<d[v])) { v=u; } } if(v==-1)break; used[v]=true; for(int u=1;u<=n;u++) { d[u]=min(d[u],d[v]+cost[v][u]); } } return ; } int main() { while(1) { for(int i=0;i<101;i++) { for(int j=0;j<101;j++) { cost[i][j]=INF; } } cin >> n >> k; if(n==0&&k==0)break; for(int i=0;i<k;i++) { int type; cin >> type; if(type) { int a,b,co; cin >> a >> b >> co; cost[a][b]=min(cost[a][b],co); cost[b][a]=min(cost[b][a],co); } else { int a,b; cin >> a >> b; dijkstra(a); if(d[b]==INF) { cout << -1 << endl; } else { cout << d[b] << endl; } } } } return 0; }
#include <algorithm> #include <iostream> #include <vector> #include <numeric> using namespace std; #define bit(n, k) ((n) >> (k) & 1) const int MOD = (int)1e9 + 7; template<class T> void add(T &a, T b) { (a += (b % MOD + MOD) % MOD) %= MOD; } int main() { int h, w; cin >> h >> w; int K; cin >> K; K--; vector<vector<int>> num(w, vector<int>(w)); for (int mask = 0; mask < 1 << (w - 1); mask++) { bool ok = true; for (int i = 0; i + 1 < w - 1; i++) { if (bit(mask, i) && bit(mask, i + 1)) ok = false; } if (!ok) continue; vector<int> p(w); iota(p.begin(), p.end(), 0); for (int i = 0; i < w - 1; i++) if (bit(mask, i)) swap(p[i], p[i + 1]); for (int i = 0; i < w; i++) num[i][p[i]]++; } vector<vector<long long>> dp(h + 1, vector<long long>(w)); dp[0][0] = 1; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) for (int k = 0; k < w; k++) { add(dp[i + 1][k], dp[i][j] * num[j][k]); } } cout << dp[h][K] << endl; return 0; }
0
#include <iostream> #include <string> #include <vector> #include <set> #include <map> #include <cmath> #define rep(i, n) for(int i = 0; i < (n); i++) using namespace std; using P = pair<int, int>; long mod = (long) 1e9 + 7; int main(){ int n; long cnt = 0, r = 0, g = 0, b = 0; cin >> n; string s; cin >> s; vector<int> v(n,0); rep(i,n){ if(s[i] == 'R'){ v[i] = 0; r++; } else if(s[i] == 'G'){ v[i] = 1; g++; } else if(s[i] == 'B'){ v[i] = 2; b++; } } for(int i = 1; i < n - 1; i++){ int a = min(i, n - 1 - i); for(int j = 1; j <= a; j++){ if(v[i] != v[i-j] && v[i] != v[i+j] && v[i-j] != v[i+j]) cnt++; } } long ans = r * g * b - cnt; cout << ans << "\n"; return 0; }
#include<iostream> #include<string> using namespace std; int main() { char s[5000]; long long n,i,j,k,total=0,red[5000]={0},green[5000]={0},blue[5000]={0},r=0,g=0,b=0,x,y; cin>>n; for(i=1;i<=n;i++) { cin>>s[i]; } for(i=1;i<=n;i++) { if(s[i]=='R') { r++; red[i]=red[i-1]+1; blue[i]=blue[i-1]; green[i]=green[i-1]; } else if(s[i]=='G') { g++; green[i]=green[i-1]+1; red[i]=red[i-1]; blue[i]=blue[i-1]; } else if(s[i]=='B') { b++; blue[i]=blue[i-1]+1; red[i]=red[i-1]; green[i]=green[i-1]; } } for(i=1;i<=n;i++) { for(j=i+1;j<=n;j++) { if((s[i]=='R'&&s[j]=='G')||(s[i]=='G'&&s[j]=='R')) { x=b-blue[j]; y=(j-i)+j; if(s[y]=='B'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } else if((s[i]=='R'&&s[j]=='B')||(s[i]=='B'&&s[j]=='R')) { x=g-green[j]; y=(j-i)+j; if(s[y]=='G'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } else if((s[i]=='B'&&s[j]=='G')||(s[i]=='G'&&s[j]=='B')) { x=r-red[j]; y=(j-i)+j; if(s[y]=='R'&&x>0&&y<=n) { x--; } if(x>0) { total=total+x; } } } } cout<<total<<endl; return 0; }
1
#include<stdio.h> #include<stdio.h> #include<bits/stdc++.h> #include<vector> #define int long long int #define rep(a,b,c) for(int a=b;a<c;a++) using namespace std; void a1(){ int i, len = 3; char str[] = "abc"; for(i=0;i<len;i++){ printf("%c\n",str[2-i]); } return; } int length(char str[5]){ int cut=0; while(*(str+cut)!='\0'){ cut++; } return cut; } void a2(){ char a[4][5]={"ab","cdef","g","hij"}; int i; for(i=0; i<4; i++){ printf("%d\n",length(a[i])); } return; } void a3(){ char str[4]; strcpy(str,"abc"); printf("%s\n",str); return; } void a4(){ char a[7]="saitou",b[7] = "suzuki"; int i; for(i=0;i<7;i++){ int k = a[i]-b[i]; if(k > 0){ printf("%c",a[i]); }else if(k < 0){ printf("%c",b[i]); }else{ printf("0"); } } printf("\n"); return; } void a5(){ char *a[3]; int n,i,j,cut = 0; for(i=0;i<3;i++){ scanf("%d",&n); a[i]=(char *)malloc((n+1)*sizeof(char)); scanf("%s",a[i]); } for(i=0;i<3;i++){ for(j=0;j<strlen(a[i]);j++){ if(a[i][j]=='P'||a[i][j]=='p'){ cut++; } } } printf(">> %d\n",cut); for(i=0;i<3;i++){ free(a[i]); } } void y(){ int n,m; vector<int> v,v2; while(1){ cin >> n >> m; v.clear();v2.clear(); if(n==0&&m==0)break; v.push_back(0); rep(i,0,n){ int x;cin >> x; v.push_back(x); } rep(i,0,v.size()){ rep(j,0,v.size()){ v2.push_back(v[i]+v[j]); } } v2.erase(unique(v2.begin(),v2.end()),v2.end()); sort(v2.begin(),v2.end()); int ans=0,k=0; //cout << v2.size() << " a " << endl; bool a=false; for(int j=v2.size()-1;j>=0;j--){ rep(i,k,v2.size()){ k=i; if(v2[i]+v2[j]>m){ //if(i==(j-1))a=true; break; } ans=max(ans,v2[i]+v2[j]); // cout << an << " a " << endl; } if(k>j)break; } cout << ans << endl; } return; } signed main(){ y(); return 0; }
#include <iostream> #include <algorithm> using namespace std; int m, n, p[1001], pp[1001*1001]; int main(){ while( true ){ cin >> n >> m; if( m == 0 && n == 0 ) break; for(int i = 0; i < n; i++){ cin >> p[i]; } p[n] = 0; n++; for(int i = 0; i < n*n; i++){ pp[i] = p[i/n] + p[i%n]; } sort(pp, pp + n*n); int cand = 0; int j = n*n-1; for(int i = 0; i <= j; i++){ while( j >= 0 && pp[i] + pp[j] > m ) j--; if(j >= 0){ cand = max(cand, pp[i] + pp[j]); } } cout << cand << endl; } return 0; }
1
#include<bits/stdc++.h> using namespace std; const int MAXN = 40; typedef long long ll; typedef long double ld; typedef unsigned long long ull; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } char s[MAXN], a[MAXN], b[MAXN]; int n, dp[MAXN][MAXN]; ll ans; void work(int pos, int la, int lb) { if (pos == n) { memset(dp, 0, sizeof(dp)), dp[n][la] = 1; for (int i = n; i >= 1; i--) for (int j = max(0, i - lb), k = i - j; j <= la && j <= i; j++, k--) { if (s[i] == a[j]) dp[i - 1][j - 1] += dp[i][j]; if (s[i] == b[k]) dp[i - 1][j] += dp[i][j]; } ans += dp[0][0]; return; } a[la + 1] = s[pos]; work(pos - 1, la + 1, lb); b[lb + 1] = s[pos]; work(pos - 1, la, lb + 1); } int main() { read(n), scanf("%s", s + 1); work(n * 2, 0, 0); writeln(ans); return 0; }
#pragma GCC optimize("O3") #include <iostream> #include <iomanip> #include <cstdio> #include <string> #include <cstring> #include <deque> #include <list> #include <queue> #include <stack> #include <vector> #include <utility> #include <algorithm> #include <map> #include <set> #include <complex> #include <cmath> #include <limits> #include <cfloat> #include <climits> #include <ctime> #include <cassert> #include <numeric> #include <fstream> #include <functional> #include <bitset> using namespace std; using ll = long long; using P = pair<int, int>; using T = tuple<int, int, int>; template <class T> inline T chmax(T &a, const T b) {return a = (a < b) ? b : a;} template <class T> inline T chmin(T &a, const T b) {return a = (a > b) ? b : a;} constexpr int MOD = 1e9 + 7; constexpr int inf = 1e9; constexpr long long INF = 1e18; constexpr double pi = acos(-1); constexpr double EPS = 1e-10; int dx[] = {1, 0, -1, 0}; int dy[] = {0, 1, 0, -1}; int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin>>n; string s; cin>>s; string head = s.substr(0, n); string tail = s.substr(n, n); reverse(tail.begin(), tail.end()); map<pair<string, string>, ll> head_paint, tail_paint; for(int bit=0; bit<(1<<n); bit++){ string ha, hb, ta, tb; for(int i=0; i<n; i++){ if(bit & (1 << i)){ ha += head[i]; ta += tail[i]; } else{ hb += head[i]; tb += tail[i]; } } reverse(hb.begin(), hb.end()); reverse(tb.begin(), tb.end()); head_paint[make_pair(ha, hb)]++; tail_paint[make_pair(ta, tb)]++; } ll ans = 0; for(auto i : head_paint){ ans += i.second * tail_paint[make_pair(i.first.first, i.first.second)]; } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; #ifdef LOCAL #define debug(var) cerr << (#var) << " = " << (var) << endl; #else #define debug(var) #endif void init() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } const int N = 123; int a[N]; void solve() { int k, t; scanf("%d%d", &k, &t); for (int i = 0; i < t; ++i) scanf("%d", a+i); if (t == 1) { printf("%d", a[0]-1); return; } sort(a, a+t); int spare = 1; int prev = 0; for (int i = 0; i < t-1; ++i) { if (i && a[i] == a[i-1]) continue; spare += (a[i]-prev)*(t-i-2); a[t-1] -= (a[i]-prev); prev = a[i]; } printf("%d", max(a[t-1]-spare, 0)); } int main() { init(); int t = 1; //scanf("%d", &t); while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (n); i++) using namespace std; typedef long long ll; int main() { ll n; cin >> n; vector<ll> a(n), d; rep(i, n) cin >> a[i]; rep(i, n) { auto itr = lower_bound(d.rbegin(), d.rend(), a[i]); if (itr == d.rbegin()) d.push_back(a[i]); else *--itr = a[i]; } cout << d.size() << endl; return 0; }
0
#include <iostream> #include <stdio.h> #include <math.h> using namespace std; int main(){ int n; cin >> n; if(n < 3 || n > 10000) return -1; for(int i = 1 ;i <= n ; i ++){ int x = i; if(x % 3 == 0){ cout << " " << i ; continue; }else if(x % 10 == 3){ cout << " " << i ; continue; }else{ while(x > 0){ x = round(x/10); if(x % 10 == 3){ cout << " " << i ; break; } } continue; } } cout << endl; return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < n; ++i) #define all(x) (x).begin(),(x).end() #define endl "\n" using ll = long long; using P = pair<int,int>; const int MOD = 1e9 + 7; const int INF = 1001001001; int main (){ string s; cin >> s; s = s.substr(0, s.size()-8); cout << s << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 #define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(long long i=0;i<x;i++) #define repn(i,x) for(long long i=1;i<=x;i++) #define SORT(x) sort(x.begin(),x.end()) #define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end()) #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) vector<pair<string,P> >vec; #define MAX_N 200001 long long par2[MAX_N]; // 親 long long rankuf2[MAX_N]; // 木の深さ // n要素で初期化 void init2(long long n) { for (long long i = 0; i < n; i++) { par2[i] = i; rankuf2[i] = 0; } } // 木の根を求める long long find2(long long x) { if (par2[x] == x) { return x; } else { return par2[x] = find2(par2[x]); } } // xとyの属する集合を併合 void unite2(long long x, long long y) { x = find2(x); y = find2(y); if (x == y) return; if (rankuf2[x] < rankuf2[y]) { par2[x] = y; } else { par2[y] = x; if (rankuf2[x] == rankuf2[y]) rankuf2[x]++; } } // xとyが同じ集合に属するか否か bool same2(long long x, long long y) { return find2(x) == find2(y); } long long par[MAX_N]; // 親 long long rankuf[MAX_N]; // 木の深さ // n要素で初期化 void init(long long n) { for (long long i = 0; i < n; i++) { par[i] = i; rankuf[i] = 0; } } // 木の根を求める long long find(long long x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } // xとyの属する集合を併合 void unite(long long x, long long y) { x = find(x); y = find(y); if (x == y) return; if (rankuf[x] < rankuf[y]) { par[x] = y; } else { par[y] = x; if (rankuf[x] == rankuf[y]) rankuf[x]++; } } // xとyが同じ集合に属するか否か bool same(long long x, long long y) { return find(x) == find(y); } int main(){ long long K; long long N; long long L; scanf("%lld",&N); scanf("%lld",&K); init(N+1); init2(N+1); vector<long long> q(K-1+1); vector<long long> p(K-1+1); scanf("%lld",&L); vector<long long> r(L-1+1); vector<long long> s(L-1+1); for(int i = 0 ; i <= K-1 ; i++){ scanf("%lld",&p[i]); scanf("%lld",&q[i]); unite(p[i]-1, q[i]-1); } for(int i = 0 ; i <= L-1 ; i++){ scanf("%lld",&r[i]); scanf("%lld",&s[i]); unite2(r[i]-1, s[i]-1); } map<P, ll> m; rep(i, N) { m[P(find(i), find2(i))]++; } ll cnt = 0; rep(i, N-1) { cout << m[P(find(i), find2(i))] << " "; } cout << m[P(find(N-1), find2(N-1))] << endl; return 0; }
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <functional> #include <queue> #include <string> #include <cstring> #include <numeric> #include <cstdlib> #include <cmath> #include <map> using namespace std; typedef long long ll; #define INF 10e17 // 4倍しても(4回足しても)long longを溢れない #define rep(i,n) for(int i=0; i<n; i++) #define rep_r(i,n,m) for(int i=m; i<n; i++) #define END cout << endl #define MOD 1000000007 #define pb push_back #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<int>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) for (auto itr : x) { debug(itr); } template <class T> inline void chmax(T &ans, T t) { if (t > ans) ans = t;} template <class T> inline void chmin(T &ans, T t) { if (t < ans) ans = t;} #include <vector> using namespace std; /* Union-Find-Tree */ /* 必ず要素数をコンストラクタに入れること */ template <class T = long long> class Union_Find { using size_type = std::size_t; using _Tp = T; public: vector<_Tp> par; vector<_Tp> rnk; // 親の根を返す。値の変更は認めない。 const _Tp & operator[] (size_type child) { find(child); return par[child]; } Union_Find (_Tp n) { par.resize(n), rnk.resize(n); for (int i = 0; i < n; ++i) { par[i] = i; rnk[i] = 0; } } // 木の根を求める _Tp find(_Tp x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } // xとyの属する集合を併合 void merge(_Tp x, _Tp y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; } else { par[y] = x; if (rnk[x] == rnk[y]) rnk[x]++; } } // xとyが同じ集合に属するか否か bool same(_Tp x, _Tp y) { return find(x) == find(y); } }; int main() { int n,k,l; cin >> n >> k >> l; Union_Find<long long> uf_road(n+1), uf_train(n+1); int r,s; rep(i,k) { cin >> r >> s; uf_road.merge(r, s); } rep(i,l) { cin >> r >> s; uf_train.merge(r, s); } vector<pair<int,int> > com; map<pair<int,int>, int> mp; for (int i = 1; i <= n; ++i) { int a, b; a = uf_road[i]; b = uf_train[i]; com.push_back(make_pair(a,b)); mp[make_pair(a,b)] += 1; } for (int i = 0; i < n; ++i) { cout << mp[com[i]] << " "; } END; }
1
#include<bits/stdc++.h> using namespace std; static const int64_t INF=100000000000000; int main(){ int N;cin>>N; vector<tuple<int64_t,int64_t,int64_t>>A(N); for(int i=0;i<N;i++){ int64_t h,p;cin>>h>>p; tuple<int64_t,int64_t,int64_t>q(h+p,h,p); A.at(i)=q; }sort(A.begin(),A.end()); vector<pair<int64_t,int64_t>>B(N); for(int i=0;i<N;i++){ int64_t h=get<1>(A.at(i)); int64_t p=get<2>(A.at(i)); pair<int64_t,int64_t>q(h,p); B.at(i)=q; }vector<vector<int64_t>>dp(1+N,vector<int64_t>(1+N,INF)); for(int i=0;i<=N;i++)dp[i][0]=0; for(int i=1;i<=N;i++) for(int j=1;j<=N;j++){ int64_t h=B[i-1].first; int64_t p=B[i-1].second; if(dp[i][j-1]!=INF){ if(dp[i-1][j-1]<=h) dp[i][j]=min(dp[i-1][j-1]+p,dp[i-1][j]); else dp[i][j]=dp[i-1][j]; } }int64_t ans=0; for(int j=N;0<=j;j--) if(dp[N][j]!=INF){ans=j;break;} cout<<ans<<endl; return 0; }
#include<algorithm> #include<iostream> #include<cstdio> typedef long long ll; using namespace std; struct Node{ int h,p; }a[5005]; int n; ll f[5005][5005]; bool cmp(Node a,Node b){ return a.h+a.p<b.h+b.p; } int main(){ ios::sync_with_stdio(false); int i,j; cin>>n; fill(f[0],f[0]+n+1,2e9+1); for(i=1;i<=n;i++) cin>>a[i].h>>a[i].p; sort(a+1,a+n+1,cmp); f[0][0]=0; for(i=1;i<=n;i++) for(j=0;j<=n;j++){ f[i][j]=f[i-1][j]; if(j&&f[i-1][j-1]<=a[i].h) f[i][j]=min(f[i][j],f[i-1][j-1]+a[i].p); } for(i=n;i>=0;i--) if(f[n][i]<=2e9) break; cout<<i; return 0; }
1
#include <iostream> using namespace std; void solve() { int x; cin >> x; cout << x * x * x << endl; } int main() { solve(); return(0); }
#include <stdio.h> long long n, a, b; int main() { scanf("%lld", &n); for (long long i = 1; i * i <= n; i++) { if (n % i == 0) { a = i; b = n / i; } } a = 0; while (b) { b /= 10; a++; } printf("%lld\n", a); }
0
#include <bits/stdc++.h> #include <iostream> #include <string> #include <string.h> #include <ctype.h> #include <algorithm> using namespace std; int main() { int a,b,c,d; string s; cin >> s; a = s[0] - '0'; b = s[1] - '0'; c = s[2] - '0'; d = s[3] - '0'; if(a+b+c+d == 7) cout << a << "+" << b << "+" << c << "+" << d << "=7" << endl; else if(a+b+c-d == 7) cout << a << "+" << b << "+" << c << "-" << d << "=7" << endl; else if(a+b-c+d == 7) cout << a << "+" << b << "-" << c << "+" << d << "=7" << endl; else if(a+b-c-d == 7) cout << a << "+" << b << "-" << c << "-" << d << "=7" << endl; else if(a-b+c+d == 7) cout << a << "-" << b << "+" << c << "+" << d << "=7" << endl; else if(a-b-c+d == 7) cout << a << "-" << b << "-" << c << "+" << d << "=7" << endl; else if(a-b+c-d == 7) cout << a << "-" << b << "+" << c << "-" << d << "=7" << endl; else if(a-b-c-d == 7) cout << a << "-" << b << "-" << c << "-" << d << "=7" << endl; }
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int i, j; for (i = 0; i < (1 << 3); i++) { bitset<3> a(i); int sum = s.at(0) - '0'; for (j = 0; j < 3; j++) { if (a.test(j) == 1) { sum += s.at(j + 1) - '0'; } else { sum -= s.at(j + 1) - '0'; } } if (sum == 7) { break; } } bitset<3> a(i); cout << s.at(0); for (j = 0; j < 3; j++) { if (a.test(j) == 1) { cout << "+"; } else { cout << "-"; } cout << s.at(j + 1); } cout << "=7" << endl; }
1
#include <iostream> #include <algorithm> #include <cstring> #define rep(X, Y) for( int X = 0; X < Y; ++X ) int n, m; int N[1001], M[1001]; int cnt[1001]; int main(){ memset( cnt, 0, sizeof(cnt) ); std::cin >> n >> m; rep( i, n ) std::cin >> N[i]; rep( i, m ) std::cin >> M[i]; rep( i, m ) rep( l, n ) if( N[l] <= M[i] ){ cnt[l]++; break; } std::cout << std::max_element( cnt, cnt + n ) - cnt + 1 << std::endl; return 0; }
#include <bits/stdc++.h> #define maxn 105 using namespace std; int n, m; char s[maxn][maxn]; short f[maxn][maxn][maxn][maxn]; int x, y; int sum1[maxn][maxn], sum2[maxn][maxn]; inline void Max(short &x, short y){ x = max(x, y); } int main(){ scanf("%d%d", &n, &m); for(int i = 1;i <= n;i++) scanf("%s", s[i] + 1); for(int i = 1;i <= n;i++){ for(int j = 1;j <= m;j++){ if(s[i][j] == 'E') x = i, y = j; sum1[i][j] = sum1[i][j - 1] + (s[i][j] == 'o'); sum2[i][j] = sum2[i - 1][j] + (s[i][j] == 'o'); } } int U = x - 1, D = n - x, L = y - 1, R = m - y; for(int i = 0;i <= U;i++){ for(int j = 0;j <= D;j++){ for(int k = 0;k <= L;k++){ for(int o = 0;o <= R;o++){ int l = max(o + 1, y - k), r = min(m - k, y + o); if(i < U) Max(f[i + 1][j][k][o], f[i][j][k][o] + (l <= r && x - i - 1 > j ? sum1[x - i - 1][r] - sum1[x - i - 1][l - 1] : 0)); if(j < D) Max(f[i][j + 1][k][o], f[i][j][k][o] + (l <= r && x + j + 1 < n + 1 - i ? sum1[x + j + 1][r] - sum1[x + j + 1][l - 1] : 0)); int u = max(j + 1, x - i), d = min(n - i, x + j); if(k < L) Max(f[i][j][k + 1][o], f[i][j][k][o] + (u <= d && y - k - 1 > o ? sum2[d][y - k - 1] - sum2[u - 1][y - k - 1] : 0)); if(o < R) Max(f[i][j][k][o + 1], f[i][j][k][o] + (u <= d && y + o + 1 < m + 1 - k ? sum2[d][y + o + 1] - sum2[u - 1][y + o + 1] : 0));//, printf("%d %d %d %d %d %d %d %d %d %d--\n", i, j, k, o, l, r, u, d, y + o + 1, m + 1 - k); //printf("%d %d %d %d %d %d %d %d--\n", i, j, k, o, u, d, y - k - 1, f[i][j][k][o]); } } } } printf("%d", f[U][D][L][R]); }
0
#include<bits/stdc++.h> #define ll long long using namespace std; ll a[100]; int main() { ll n; cin>>n; memset(a,0,sizeof(a)); for(ll i=1;i<=50;i++) { a[i]=(49+n/50); } ll m=n%50; for(ll i=1;i<=50;i++) { a[i]=a[i]-m; } for(ll i=1;i<=m;i++) { a[i]+=50; } cout<<50<<endl; for(ll i=1;i<=49;i++) { cout<<a[i]<<" "; } cout<<a[50]<<endl; }
#include <bits/stdc++.h> #define err(args...) {} #ifdef DEBUG #include "_debug.cpp" #endif using namespace std; using ll = long long; using ld = long double; template <typename T> using lim = numeric_limits<T>; template <typename T> istream& operator>>(istream& is, vector<T>& a) { for(T& x : a) { is >> x; } return is; } const int N = 50; int main() { ios_base::sync_with_stdio(false); cin.tie(0); ll k; cin >> k; cout << N << endl; for(int i = 0; i < N; i++) { if(i < N - 1 - (k - 1) % N) { cout << (k - 1) / N + i << " "; } else if(i == N - 1) { cout << (k - 1) / N + N << " "; } else { cout << (k - 1) / N + N - 1 << " "; } } cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define rg register template <typename _Tp> inline _Tp read(_Tp&x){ char c11=getchar();x=0;while(!isdigit(c11))c11=getchar(); while(isdigit(c11))x=x*10+c11-'0',c11=getchar();return x; } int n,m,tot,a[105]; int main(){ read(n),read(m); for(rg int i=1;i<=m;i++)read(a[i]),tot+=(a[i]&1); if(tot>2){puts("Impossible");return 0;} if(tot){ bool flag=0; for(rg int i=1;i<m;i++) if(a[i]&1) if(flag)swap(a[i],a[m]); else flag=1,swap(a[i],a[1]); } if(m==1){ if(a[1]==1){puts("1\n1\n1");return 0;} printf("%d\n",a[1]);puts("2"); printf("1 %d\n",a[1]-1); return 0; } for(rg int i=1;i<=m;i++)printf("%d ",a[i]); if(a[m]==1){ printf("\n%d\n",m-1); printf("%d\n",a[1]+1); for(rg int i=2;i<m;i++)printf("%d ",a[i]); } else { printf("\n%d\n",m); printf("%d\n",a[1]+1); for(rg int i=2;i<m;i++)printf("%d ",a[i]); printf("%d\n",a[m]-1); }return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ii = pair<ll, ll>; using iii = pair<ii, ll>; #define pb push_back #define pf push_front #define mp make_pair #define fi first #define se second #define __lcm(a, b) a * b / __gcd(a, b) const ll MOD = 1e9 + 7; const ll INF = 2e9; // const ll N = ; ll a, b, temp; ll factorize(ll x) { ll ret = 1; for(ll i = 2; i * i <= x; i++) { if(x % i == 0) { ret++; while(x % i == 0) x /= i; } } if(x > 1) ret++; return ret; } void solve() { cin >> a >> b; temp = __gcd(a, b); cout << factorize(temp) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); solve(); }
0
#include <cstdio> #include <cstring> #include <cmath> #include <utility> #include <iostream> #include <functional> #include <bitset> #include <algorithm> #include <vector> #include <forward_list> #include <set> #include <map> #include <queue> #include <deque> #include <stack> #include <tuple> #include <numeric> #define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i)) using namespace std; using ll = long long; using P = pair<ll, ll>; const ll MOD = 1e9 + 7; const ll INF = (1ll << 60); int main(void) { int n; cin >> n; switch (n) { case 1: cout << "Hello World" << endl; break; default: int a, b; cin >> a >> b; cout << a + b << endl; break; } }
#include <bits/stdc++.h> #define rep(i,n) for(int (i)=0;(i)<(int)(n);(i)++) #define REP(i,n) for(int (i)=0;(i)<=(int)(n);(i)++) #define rep1(i,x,n) for(int (i)=(x);(i)<(int)(n);(i)++) #define REP1(i,x,n) for(int (i)=(x);(i)<=(int)(n);(i)++) #define rrep(i,x) for(int i=((int)(x)-1);i>=0;i--) using namespace std; using ll = long long; using ull = unsigned long long; using Int = long long; //INT_MAX = 2147483647; //LLONG_MAX = 9223372036854775807; //ULLONG_MAX = 18446744073709551615; const int MOD = 1000000007; const ll INF = numeric_limits<ll>::max(); const int inf = 1e8; typedef pair<int,int> P; typedef std::priority_queue<int> IntPrioQueue; typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //少数点表示 //cout << std::fixed << std::setprecision(14) << 値 int main(){ cin.tie( 0 ); ios::sync_with_stdio( false ); string ans=""; ll n,m; cin >> n; m = n; while(m>0){ m--; char s = 'a'+ m%26; ans.push_back(s); m /= 26; } reverse(ans.begin(),ans.end()); cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int f(int x) { return x * x; } int main(void) { int n; while(cin >> n){ int s = 0; for(int i = 1; i * n < 600; i++) s += f(i * n) * n; cout << s << endl; } return 0; }
#include <iostream> #include <cstdio> #include <stack> using namespace std; int main(){ stack<char> str; char c; while(1){ scanf("%c",&c); if(c=='\n'||c==EOF) break; str.push(c); } while(!str.empty()){ printf("%c",str.top()); str.pop(); } printf("\n"); return 0; }
0
#define _USE_MATH_DEFINES #include "bits/stdc++.h" using namespace std; //#define int long long #define DBG 0 #define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;} #define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;} #define rep(i,a,b) for(int i=(a);i<(b);i++) #define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--) #define each(it,c) for(auto it=(c).begin();it!=(c).end();it++) #define all(c) c.begin(),c.end() const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f; const int MOD = (int)(1e9 + 7); const double EPS = 1e-10; signed main() { int n, m; while (cin >> n >> m, n || m) { vector<int>v(n + 1, 0), Q; rep(i, 1, n + 1)cin >> v[i]; rep(i, 0, n + 1) { rep(j, i, n + 1) { if (v[i] + v[j] <= m)Q.push_back(v[i] + v[j]); } } sort(all(Q)); dumpc(Q); int ans(0); rep(i, 0, Q.size()) { int l = 0, r = Q.size(); while (r - l > 1) { int mid = (l + r) / 2; if (Q[mid] > m - Q[i])r = mid; else l = mid; } if (Q[i] + Q[l] <= m)ans = max(ans, Q[i] + Q[l]); dump(ans); } cout << ans << endl; } return 0; }
#include<cstdio> #include<vector> #include<algorithm> using namespace std; int main(){ int n, m; while(1){ scanf("%d%d", &n, &m); if(n == 0 && m == 0)return 0; int res = 0, tmp; vector<int> points, points2; for(int i = 0;i < n;i++){ scanf("%d", &tmp); points.push_back(tmp); } vector<int>::iterator it1,it2; for(it1 = points.begin();it1 != points.end();it1++){ points2.push_back(*it1); for(it2 = points.begin();it2 != points.end();it2++){ if(*it1 + *it2 <= m)points2.push_back(*it1 + *it2); } } sort(points2.begin(),points2.end()); for(it1 = points2.begin();it1 != points2.end();it1++){ it2 = upper_bound(points2.begin(), points2.end(), m - *it1); if(it2 == points2.begin())continue; it2--; res = max(res, *it1 + *it2); } printf("%d\n",res); } }
1
#include <bits/stdc++.h> using namespace std; const int maxn=1e5+5; int n,k,x[maxn]; const int inf=0x3f3f3f3f; long long a,b,now; int topp,topn; long long ans; int main(){ cin>>n>>k; ans=inf; for(int i=1;i<=n;i++){ cin>>x[i]; } for(int i=1;i+k-1<=n;i++){ a=min(abs(x[i]),abs(x[i+k-1])); b=x[i+k-1]-x[i]; ans=min(ans,a+b); } cout<<ans; }
#include <bits/stdc++.h> #define rep(i, n) for(ll i = 0; i < (n); ++i) using namespace std; using ll = long long; const double PI=acos(-1); template<typename T> istream& operator>> (istream& is, vector<T> &vec){ for(T& x: vec) is >> x; return is; } int main(){ ll n, k; cin >> n >> k; vector<ll> x(n); cin >> x; ll inf = 1000000000, ans = inf; rep(i, n-k+1){ ll a = x[i], b = x[i+k-1]; if(a < 0 && b < 0) ans = min(ans, abs(a)); else if(a > 0 && b > 0) ans = min(ans, b); else{ if(abs(a) < abs(b)) ans = min(ans, abs(a)*2 + abs(b)); else ans = min(ans, abs(a) + abs(b)*2); } } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long const int N = 5e5+15; const int mod = 1e9+7; const int M = (1<<22); int A[N]; int add(int x,int y) { return (x+y)%mod; } int mul(int x,int y) { return (ll)x*y%mod; } int dp[M]; int sum[M],X[M]; void solve() { int n; scanf("%d",&n); int g = 0; for(int i=0;i<n;++i) { scanf("%d",&A[i]); g^= A[i]; } int c = 0; int ret = 0; ++dp[0]; if(g>0) { for(int i=0;i<n-1;++i) { c^= A[i]; if(c==0) { ret = add(ret,dp[g]); int u = dp[g]; dp[0] = add(dp[0],u); } else if(c==g) { int u = dp[0]; dp[g] = add(dp[g],u); } } ret = add(ret,1); } else { int k = 0; for(int i=0;i<n-1;++i) { c^= A[i]; if(c==0) { ret= add(ret,dp[0]); dp[0] = add(dp[0],dp[0]); ++k; } else { int x = k - sum[c]; sum[c] = k; int v = mul(x,X[c]); dp[c] = add(dp[c],v); X[c] = add(1,add(X[c],dp[c])); ret = add(1,add(ret,dp[c])); } } ret = add(ret,1); } printf("%d\n", ret); } int main(){ //freopen("input.txt","r",stdin); solve(); }
#include <string> #include <vector> #include <sstream> #include <iostream> #include <algorithm> #include <map> #include <list> #include <set> #include <numeric> #include <queue> #include <stack> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <cstring> #include <climits> #include <cfloat> #include <ctime> #include <complex> #include <cassert> #include <array> #include <bitset> #include <unordered_map> #include <random> using namespace std; typedef long long LL; typedef pair<int,int> P; // a>=0, b>=0, x*a+y*b=gcd>=0, a>0,b>0=>abs(y)<=a,abs(x)<=b LL gcdex(LL a,LL b,LL& x, LL& y){ LL ax=1,ay=0; LL bx=0,by=1; while(b){ LL r=a/b; LL t=a-r*b; a=b; b=t; LL tx=ax-r*bx; ax=bx; bx=tx; LL ty=ay-r*by; ay=by; by=ty; } x=ax; y=ay; return a; } LL modinv(LL a, LL m){ LL x,y; gcdex(a,m,x,y); if(x<0){ x+=m; } else if(x>=m){ x-=m; } return x; } const int M=1e9+7; LL cm[1001][1001]; LL dp[1001][1001]; LL rev[1001]; int main() { for(int i=0;i<1001;i++){ cm[i][0]=1; for(int j=1;j<=i;j++){ cm[i][j]=(cm[i-1][j-1]+cm[i-1][j])%M; } } for(int i=1;i<1001;i++){ rev[i]=modinv(i,M); } int N,A,B,C,D; scanf("%d%d%d%d%d",&N,&A,&B,&C,&D); dp[0][A-1]=1; for(int i=0;i<=N;i++){ for(int j=A;j<=B;j++){ dp[i][j]+=dp[i][j-1]; LL c=1; LL r=i; for(int k=1;k<=D;k++){ c*=cm[r][j]*rev[k]%M; c%=M; r-=j; if(r<0)break; if(C<=k){ dp[i][j]+=c*dp[r][j-1]%M; dp[i][j]%=M; } } } } printf("%lld\n",dp[N][B]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; vector<int64_t> A(N); int minus = 0; for(int i = 0; i < N; i++) { cin >> A.at(i); if(A.at(i) < 0) { minus++; } } if(minus % 2 == 0) { int64_t ans = 0; for(int i = 0; i < N; i++) { ans += abs(A.at(i)); } cout << ans << endl; } else { // 絶対値が一番小さいものをaとする int64_t a = 1000000000; int64_t ans = 0; for(int i = 0; i < N; i++) { if(abs(A.at(i)) < a) { a = abs(A.at(i)); } ans += abs(A.at(i)); } ans -= 2 * a; cout << ans << endl; } }
#include<iostream> #include<vector> using namespace std; typedef long long ll; int main() { cin.tie(0); cin.sync_with_stdio(0); int n; cin >> n; vector<ll> a(n); ll sum = 0; for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); sum += a[i]; } ll ans = sum; for (int i = 0; i < n - 1; i++) { if (a[i] < 0) { a[i] *= -1; a[i+1] *= -1; sum += 2 * a[i]; sum += 2 * a[i+1]; } ans = max(sum, ans); } for (int i = n; i >= 1; i--) { if (a[i] < 0) { a[i] *= -1; a[i-1] *= -1; sum += 2 * a[i]; sum += 2 * a[i-1]; } ans = max(sum, ans); } printf("%lld\n", ans); }
1
#include<stdio.h> #include<algorithm> using namespace std; int partition(int array[], int p, int r) { int x = array[r]; int i = p - 1; for(int j = p; j < r; j++) { if(array[j] <= x) { i++; swap(array[i], array[j]); } } swap(array[i + 1], array[r]); return i + 1; } int main(void) { int n; int array[100000]; scanf("%d", &n); for(int i = 0; i < n; i++) { scanf("%d", &array[i]); } int index = partition(array, 0, n - 1); for(int i = 0; i < n; i++) { if(i > 0) { printf(" "); } if(i == index) { printf("[%d]", array[i]); } else { printf("%d", array[i]); } } printf("\n"); return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=0;i<(n);i++) #define pb push_back #define all(v) (v).begin(),(v).end() typedef vector<int>vint; typedef pair<int,int>pint; signed main(){ int N,p[100]={0},r[100]; cin>>N; rep(i,N*(N-1)/2){ int a,b,c,d; cin>>a>>b>>c>>d; a--;b--; if(c>d)p[a]+=3; else if(c<d)p[b]+=3; else{ p[a]++;p[b]++; } } vector<pint>vec(N); rep(i,N)vec[i]=pint(p[i],i); sort(all(vec));reverse(all(vec)); int tmp=0; rep(i,N){ if(i&&vec[i-1].first!=vec[i].first)tmp=i; r[vec[i].second]=tmp; } rep(i,N)cout<<r[i]+1<<endl; return 0; }
0
#include <bits/stdc++.h> #include <string> #include <algorithm> #include <cstdlib> #include <math.h> using namespace std; int main(void){ int N,X; cin >> N >> X; vector<int> A(N); for(int i=0;i<N;i++){ cin >> A[i]; } sort(A.begin(),A.end()); int ans=0; for(int i=0;i<N;i++){ if(i==N-1){ if(A[i]==X){ ans++; } X-=X; } if(A[i]<=X){ X -= A[i]; ans++; }else{ cout << ans << endl; return 0; } } return 0; }
#include<bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ll long long int main() { int k,t; cin >> k >> t; int ma=0; rep(i,t){ int u; cin >> u; ma=max(ma,u); } if(ma>(k*1)/2){ cout << k-1-(k-ma)*2 << endl; } else cout << 0 << endl; }
0
#include <iostream> #include <algorithm> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <vector> #include <queue> #include <cmath> #include <bitset> // output #define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' '); #define YES cout << "YES" << endl #define Yes cout << "Yes" << endl #define NO cout << "NO" << endl #define No cout << "No" << endl // utility #define ALL(i) (i).begin(), (i).end() #define FOR(i, a, n) for(int i=(a);i<(n);++i) #define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) #define OUT(a, x, b) (x<a || b<=x) template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; } template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; const int MOD = 1000000007; const int INF = 1e18; using namespace std; signed main() { int N; string s; cin >> N >> s; vector<int> vec(N, INF); int x[] = {0, 0, 1, 1}; int y[] = {0, 1, 0, 1}; REP(_, 4){ vec[0] = x[_]; vec[1] = y[_]; FOR(i, 1, N-1){ // hituzi if(vec[i] == 1){ if(s[i] == 'o') vec[i+1] = vec[i-1]; else vec[i+1] = 1-vec[i-1]; }else{ if(s[i] == 'x') vec[i+1] = vec[i-1]; else vec[i+1] = 1-vec[i-1]; } } int ans = 0; if(vec[N-1] == 1){ if(s[N-1] == 'o' && vec[N-2] == vec[0]) ans++; if(s[N-1] == 'x' && vec[N-2] != vec[0]) ans++; }else{ if(s[N-1] == 'x' && vec[N-2] == vec[0]) ans++; if(s[N-1] == 'o' && vec[N-2] != vec[0]) ans++; } if(vec[0] == 1){ if(s[0] == 'o' && vec[N-1] == vec[1]) ans++; if(s[0] == 'x' && vec[N-1] != vec[1]) ans++; }else{ if(s[0] == 'x' && vec[N-1] == vec[1]) ans++; if(s[0] == 'o' && vec[N-1] != vec[1]) ans++; } if(ans == 2){ REP(i, N){ if(vec[i]) cout << "S"; else cout << "W"; } cout << endl; return 0; } } cout << -1 << endl; return 0; }
#include<bits/stdc++.h> using namespace std; using ll = long long; using Graph = vector<vector<int>>; const ll mod=1000000007; const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; int main() { string s; cin>>s; int a=0; int b=0; int n=s.size(); int c[n]={}; char w; int p=0; if(n!=26){ for(int i=0; i<n; i++){ c[i]=s.at(i)-'a'+1;} sort(c,c+n); for(int i=0; i<n; i++){ if(c[i]!=i+1){ a=i; p++; break; }} if(p==0){ a=n;} char q=a+'a'; s+=q; cout<<s<<endl;} else{ for(int i=0; i<n; i++){ c[i]=s.at(i)-'a';} for(int i=0; i<n; i++){ if(c[n-2-i]<c[n-1-i]){ a=n-1-i; b=1; break;}} if(b!=1){ cout<<-1<<endl;} else{ b=10000; for(int k=a; k<n; k++){ if(c[a-1]<c[k]){ b=min(b,c[k]);} } w=b+'a'; for(int i=0; i<a-1; i++){ cout<<s.at(i);} cout<<w<<endl;}}}
0
#include <bits/stdc++.h> using namespace std; typedef unsigned long long ull; #define loop(i,a,b) for(int i=(a);i<ull(b);++i) #define rep(i,n) loop(i,0,n) #define all(a) (a).begin(), (a).end() const double eps = 1e-10; const double pi = acos(-1.0); const double inf = (int)1e8; string field[12]; bool b[12][12]; int f(int x, int y){ int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; for(int i=0; i< 4; i++){ int nx = x+dx[i], ny=y+dy[i]; if(!(0 <= nx && nx < 12 && 0 <= ny && ny < 12)) continue; if(field[nx][ny] == '1' && b[nx][ny]){ b[nx][ny] = false; f(nx, ny); } } } int main(){ while(cin >> field[0]){ int ret = 0; for(int i=0; i <12; i++) for(int j=0; j < 12;j++) b[i][j] = true; for(int i=1; i< 12; i++) cin >> field[i]; for(int i=0; i<12; i++){ for(int j=0; j < 12; j++){ if(field[i][j] == '1' && b[i][j]){ b[i][j] = false; f(i, j); ret++; } } } cout << ret << endl; } }
#include <stdio.h> int par[12 * 12]; // 島の座標の個数 void init(int n) { for (int i = 0; i < n; i++){ par[i] = i; // i 番目の頂点のグループは最初は i } } int find(int x) { if (par[x] == x){ return (x); } return (par[x] = find(par[x])); } void merge(int x, int y) { x = find(x); y = find(y); if (x != y){ par[y] = x; } } int same(int x, int y) { return (find(x) == find(y)); } int main(void) { int i, j; int num; int groups[12 * 12]; char island[13][13]; while (1){ //色々初期化 num = 0; init(12 * 12); for (i = 0; i < 12 * 12; i++){ groups[i] = 0; } for (i = 0; i < 13; i++){ for (j = 0; j < 13; j++){ island[i][j] = 0; } } //入力 for (i = 0; i < 12; i++){ if (scanf("%s", island[i]) == EOF){ return (0); } } for (i = 0; i < 12; i++){ for (j = 0; j < 12; j++){ //001 //001 //...みたいな繋がり方 if (island[i][j] == '1' && island[i + 1][j] == '1'){ merge(i * 12 + j, (i + 1) * 12 + j); } //011 //...みたいな繋がり方 if (island[i][j] == '1' && island[i][j + 1] == '1'){ merge(i * 12 + j, i * 12 + j + 1); } } } for (i = 0; i < 12; i++){ for (j = 0; j < 12; j++){ if (island[i][j] == '1'){ groups[find(i * 12 + j)] = 1; } } } for (i = 0; i < 12 * 12; i++){ num += groups[i]; } printf("%d\n", num); } return (0); }
1
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int main(){ int x,y; cin>>x>>y; int ans=2e9; if(y>=x)ans=min(ans,y-x); x=-x; if(y>=x)ans=min(ans,y-x+1); x=-x,y=-y; if(y>=x)ans=min(ans,y-x+1); x=-x; if(y>=x)ans=min(ans,y-x+2); cout<<ans<<endl; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; #define fi first #define se second #define m_p make_pair #define p_b push_back #define e_b emplace_back #define all(x) (x).begin(),(x).end() #define sz(x) ((int)(x).size()) #define REP(i,m,n) for(int i=(int)(m);i<(int)(n);i++) #define rep(i,n) REP(i,0,n) #ifdef LOCAL//compile with -DLOCAL #define debug(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = "<<(x)<<endl #define debug_vec(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = ";\ rep(i,sz(x)){cerr<<x[i]<<" ";}cerr<<endl #define debug_mat(x) cerr<<"LINE"<<__LINE__<<" : "<<#x<<" = "<<endl;\ rep(i,sz(x)){rep(j,sz(x[i])){cerr<<x[i][j]<<" ";}cerr<<endl;}cerr<<endl #else #define debug(x) void(0) #define debug_vec(x) void(0) #define debug_mat(x) void(0) #endif template<class T> bool chmax(T &a,T b){if(a<b){a=b;return true;}return false;} template<class T> bool chmin(T &a,T b){if(a>b){a=b;return true;}return false;} int main(){ ios_base::sync_with_stdio(false);cin.tie(0); int x,y; cin >> x >> y; int ans=0; if(x==y) ans=0; else if(x==0){ if(y<0) ans++; ans+=abs(y); } else if(y==0){ if(x>0) ans++; ans+=abs(x); } else if(x<y){ if(x<0 && 0<y) ans=1+abs(y-abs(x)); else ans=abs(y-x); } else if(y<x){ if(0<y){ ans++; ans+=1+abs(y-abs(x)); } else if(y<0 && 0<x){ ans++; ans+=abs(abs(y)-x); } else if(x<0){ ans++; x=-x; ans++; ans+=abs(abs(y)-x); } } cout << ans << endl; return 0; }
1
// Created by Vignesh Manoharan #include <iostream> #include <vector> #include <algorithm> #include <cstdio> #include <cmath> #include <map> #include <queue> #include <stack> #include <set> #include <cstring> #include <cassert> using namespace std; typedef long long ll; typedef vector<int> vi; typedef pair<int,int> ii; typedef vector<vi> vvi; const int INF = 1000000000; const ll LINF = 1e17; const double PI =3.141592653589793238; #pragma unused(INF,PI,LINF) #define F(i,a,n) for(int i=(a);i<(n);i++) template<typename T,typename TT> ostream& operator<<(ostream &s,pair<T,TT> t) {return s<<"("<<t.first<<","<<t.second<<")";} template<typename T> ostream& operator<<(ostream &s,vector<T> t){ for(int i=0;i<(t).size();i++)s<<t[i]<<((i<(t).size()-1)?" ":"");return s; } template<typename T> ostream& operator<<(ostream &s,set<T> t){for(T x:t) s<<x<<" ";return s; } template<typename T> istream& operator>>(istream &s,vector<T> &t){ for(int _i=0;_i<t.size();_i++) s>>t[_i];return s; } #define pb push_back #define mp make_pair #define all(v) v.begin(),v.end() vector<vector<ii>> graph; vector<int> dist; void dijkstra(int s){ dist[s]=0; priority_queue<ii,vector<ii>,greater<ii>> pq; pq.push(make_pair(0, s)); while(!pq.empty()){ ii front=pq.top();pq.pop(); int d=front.first,u=front.second; if(d==dist[u]){ for(auto p:graph[u]){ int v=p.first,w=p.second; if(dist[u]+w<dist[v]){ dist[v]=dist[u]+w; pq.push(make_pair(dist[v], v)); } } } } } int main(int argc, const char * argv[]) { #ifdef local_test // input // freopen("input","w",stdout); // cout<<"1 \n 100 10 \n"; freopen("input","r",stdin); freopen("output","w",stdout); #endif int n,m; cin>>n>>m; int r; cin>>r; graph=vector<vector<ii>>(n, vector<ii>()); dist=vi(n,INF); F(i,0,m){ int s,t,d; cin>>s>>t>>d; graph[s].pb(mp(t,d)); // graph[t].pb(mp(s,d)); } dijkstra(r); for(int d:dist) if(d<INF) cout<<d<<"\n"; else cout<<"INF\n"; }
#include <iostream> #include <vector> #include <string.h> #include <stack> #include <queue> #include <algorithm> #include <climits> #include <cmath> #include <map> #include <set> #include <assert.h> #include <sstream> #define REP(i,n) for(ll i=0;i<(n);i++) #define MOD 1000000007 #define int long long #ifdef int const long long INF = LLONG_MAX / 10; #else const int INF = 1010101010; #endif using namespace std; typedef long long ll; typedef vector<vector<ll>> mat; typedef pair<int, int> P; //typedef pair<double, double> P; #ifdef LOCAL #define dump(...) \ do { \ std::ostringstream os; \ os << __LINE__ << ":\t" << #__VA_ARGS__ << " = "; \ print_to(os, ", ", "\n", __VA_ARGS__); \ std::cerr << os.str(); \ } while (0) #define dump_(a) \ do { \ std::ostringstream os; \ os << __LINE__ << ":\t" << #a << " = ["; \ print_to_(os, ", ", "]\n", all(a)); \ std::cerr << os.str(); \ } while (0) #else #define dump(...) #define dump_(...) #endif template <typename T> void print_to(std::ostream &os, const char *, const char *tail, const T &fst) { os << fst << tail; } template <typename Fst, typename... Rst> void print_to(std::ostream &os, const char *del, const char *tail, const Fst &fst, const Rst &... rst) { os << fst << del; print_to(os, del, tail, rst...); } template <typename Iter> void print_to_(std::ostream &os, const char *del, const char *tail, Iter bgn, Iter end) { for (Iter it = bgn; it != end;) { os << *it; if (++it != end) { os << del; } else { os << tail; } } } template <typename Fst, typename... Rst> void println(const Fst &fst, const Rst &... rst) { print_to(std::cout, "\n", "\n", fst, rst...); } template <typename Fst, typename... Rst> void print(const Fst &fst, const Rst &... rst) { print_to(std::cout, " ", "\n", fst, rst...); } template <typename Iter> void println_(Iter bgn, Iter end) { print_to_(std::cout, "\n", "\n", bgn, end); } template <typename Iter> void print_(Iter bgn, Iter end) { print_to_(std::cout, " ", "\n", bgn, end); } // const int MOD = 1000000007; namespace trush { int _ = (std::cout.precision(10), std::cout.setf(std::ios::fixed), std::cin.tie(0), std::ios::sync_with_stdio(0), 0); } int n, m; string S, T; int dp[1010][1010]; signed main() { //cin >> n >> m; cin >> S >> T; n = S.length(); m = T.length(); S = '-' + S; T = '-' + T; REP(i,m+1) dp[0][i] = i; REP(i,n+1) dp[i][0] = i; for (int i=1; i<n+1; i++) { for (int j=1; j<m+1; j++) { if (S[i] == T[j]) { dp[i][j] = min(dp[i-1][j-1], min(dp[i][j-1] + 1, dp[i-1][j] + 1)); } else { dp[i][j] = min(dp[i-1][j-1] + 1, min(dp[i][j-1] + 1, dp[i-1][j] + 1)); } } } cout << dp[n][m] << endl; }
0
#include<bits/stdc++.h> using namespace std; int main(){ string s; cin >> s; int ans = 9999; for(int i = 0; i < s.size()-2; i++){ string a; for(int j = i; j < 3 + i; j++) a.push_back(s.at(j)); //cout << a << endl; ans = min(ans,abs(stoi(a)-753)); a.erase(a.begin(),a.end()); } cout << ans; }
#include<cstdio> #include<iostream> #include<map> #include<set> #include<vector> #include<cstring> #include<cassert> #include<sstream> #include<cmath> #include<algorithm> #include<queue> #include<limits> #include<ctime> #include<stack> #include<bits/stdc++.h> #include<string> #include<stdlib.h> #include<stdio.h> typedef long long ll; using namespace std; int main(){ string s; cin>>s; int d,mins=1000; for(int i=0;i<(s.size()-2);i++){ d=((s[i]-'0')*100)+((s[i+1]-'0')*10)+(s[i+2]-'0'); mins=min(mins,(abs(d-753))); } cout<<mins; }
1
#include <bits/stdc++.h> #define pb push_back #define mp make_pair #define ll long long #define endl '\n' using namespace std; const int N = 1e5 + 5; int n, a[4][N]; int inv[2], flip[2]; int label[N]; void add_col(vector <int> v, int id) { if(abs(v[2] - v[0]) != 2 || abs(v[1] - v[0]) != 1 || v[1] % 3 < 2) { cout << "No" << endl; exit(0); } int parity = !(v[1] & 1); if(v[0] > v[2]) flip[parity]++; label[(v[1] + 1) / 3] = id; if(id & 1 != parity) { cout << "No" << endl; exit(0); } } bool vis[N]; int comp_sz; void DFS(int v) { vis[v] = true; comp_sz++; if(!vis[label[v]]) DFS(label[v]); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n; cin >> n; for(int i = 1; i <= 3; i++) for(int j = 1; j <= n; j++) cin >> a[i][j]; for(int j = 1; j <= n; j++) { vector <int> v = {a[1][j], a[2][j], a[3][j]}; add_col(v, j); } for(int i = 1; i <= n; i++) { if(!vis[i]) { comp_sz = 0; DFS(i); inv[i & 1] += (comp_sz - 1); } } inv[0] &= 1; inv[1] &= 1; flip[0] &= 1; flip[1] &= 1; if(inv[0] != flip[1] || inv[1] != flip[0]) cout << "No" << endl; else cout << "Yes" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rp(i, k, n) for (int i = k; i < n; i++) typedef long long ll; typedef double ld; template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; } template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; } const ll INF = 1ll << 60; const ll MOD = 1e9 + 7ll; const double PI=3.14159265358979323846; int main() { int n; cin >> n; vector<vector<int>> graph(n); rp(i, 0, n) { rp(j, 0, n-1) { int a; scanf("%d", &a); a--; graph.at(i).emplace_back(a); } } vector<int> prog(n); set<int> s; rp(i, 0, n) s.insert(i); int day = 0; while(!s.empty()) { set<int> next_s; for(auto x: s){ int y = graph.at(x).at(prog.at(x)); if(x == graph.at(y).at(prog.at(y))) { if(prog.at(x) < n-2) next_s.insert(x); if(prog.at(y) < n-2) next_s.insert(y); } } for(auto x: next_s) { // printf("%d ", x); prog.at(x) ++; } // printf("\n"); day++; s = next_s; } rp(i, 0, n) { if(prog.at(i) < n-2) { cout << -1 << endl; return 0; } } cout << day << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) typedef long long ll; int main() { string s; cin >> s; if (s == "SAT") cout << 1; else if (s == "MON") cout << 6; else if (s == "TUE") cout << 5; else if (s == "WED") cout << 4; else if (s == "THU") cout << 3; else if (s == "FRI") cout << 2; else if (s == "SUN") cout << 7; }
#define _GLIBCXX_DEBUG #include <bits/stdc++.h> #define FOR(i, a, b) for (int i = (a); i < (b); ++i) #define REP(i, n) FOR(i,0,n) #define BFOR(bit,a,b) for(int bit = (a); bit < (1<<(b)); ++bit) #define BREP(bit,n) BFOR(bit,0,n) using namespace std; using ll = long long; int main() { ll x; cin >> x; ll ans = 0; ll n = x / 11; ans += n*2; x -= n*11; if(x == 0){} else if(x <= 6){ans += 1;} else{ans += 2;} cout << ans << endl; }
0