code_file1
stringlengths
80
4k
code_file2
stringlengths
91
4k
similar_or_different
int64
0
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; #define all(v) v.begin(), v.end() const ll mod=1000000007; ll countBits(ll in){ int res=0; for(;in>0;in>>=1){ if((in&0x01)!=0){ res++; } } return res; } template<typename T> void show2dVector(vector<vector<T>>& v){ for(int i=0;i<v.size();i++){ int m=v[i].size(); cout<<i<<" : "; for(int j=0;i<m;i++){ cout<<v[i][j]<<" "; } cout<<endl; } } void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){ seen[v]=true; cout<<v<<" "; for(auto next: g[v]){ if(seen[next]){ continue; } bfs(g,next,seen); } } bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){ bool res=false; seen[start]=true; if(start==goal){ return true; } for(auto next: g[start]){ if(seen[next]){ continue; } res=dfs(g,next,goal,seen); if(res){ break; } } return res; } ll gcd(ll a,ll b) { return b? gcd(b,a%b): a; } ll lcm(ll a,ll b) { return a*b/gcd(a,b); } bool isLowerCase(char c){ return (c>='a'&&c<='z'); } char toLowerCase(char c){ if(isLowerCase(c)){ return c; }else{ return c+'a'-'A'; } } char toUpperCase(char c){ if(isLowerCase(c)){ return c-('a'-'A'); }else{ return c; } } ll powm(ll a,ll n, ll m){ ll ret=1; while(n>0){ if(n%2==1){ ret=(ret*a)%m; } n>>=1; a=(a*a)%m; } return ret; } const string yesno(bool ans){ return (ans?"Yes":"No"); } int main() { bool ans=false; int a,b,c;cin>>a>>b>>c; if(a<=c&&c<=b){ ans=true; }else if(b<=c&&c<=a){ ans=true; } cout<<yesno(ans)<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin >> a >> b >> c; if(min(a,b)<=c && max(a,b)>=c) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include<bits/stdc++.h> #include<cctype> using namespace std; #define rep(i,n) for (int i=0;i<(n);i++) #define reps(i,n) for (int i=1;i<=(n);i++) #define rrep(i,n) for (int i=(n)-1;i>=0;i--) #define rreps(i,n) for (int i=(n);i>0;i--) #define all(v) (v).begin(),(v).end() #define pi 3.1415926535897932384 #define E9 1000000000 #define eps 1e-4 #define pii pair<int,int> template<class T> inline bool chmin(T &a, T b) {if (a>b) {a = b; return 1;} return 0;}; template<class T> inline bool chmax(T &a, T b) {if (a<b) {a = b; return 1;} return 0;}; typedef long long int ll; const long long INF = 1LL << 60; int main(){ int N; cin >> N; vector<string> rec(N); int iter = 0; int ok = 0, ng = N, mid = 0; while (iter<=20){ cout << mid << endl; string s; cin >> s; cout.flush(); if (s=="Vacant") break; iter++; rec[mid] = s; if ((mid-ok)%2 == (rec[ok]!=rec[mid])) ok = mid; else ng = mid; mid = (ok+ng)/2; } // cout << fixed << setprecision(10); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 1e9; int main() { int i = 0, n = 0; fscanf(stdin, "%d", &n); while (++i <= n) { int x = i; if (x % 3 == 0 || x % 10 == 3) { fprintf(stdout, " %d", i); } else { for (x /= 10; x > 0; x /= 10) { if (x % 10 == 3) { fprintf(stdout, " %d", i); break; } } } } fprintf(stdout, "\n"); return 0; }
0
// C++ 14 #include <bits/stdc++.h> using namespace std; #define ll long long #define loop(__x, __start, __end) for(ll __x = __start; __x < __end; __x++) int main() { ll n, m; cin >> n >> m; vector<ll> D; for (ll i=1; i*i<=m; i++) { if (m%i == 0) { D.push_back(i); if (i*i<=m) D.push_back(m/i); } } sort(D.rbegin(), D.rend()); for (auto &&d: D) { if (n*d <= m) { cout << d << endl; return 0; } } cout << -1 << endl; return 0; }
#pragma GCC optimize("Ofast") #include <bits/stdc++.h> using namespace std; typedef long long ll; #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0) #define rep(pp,nn) for (int i = pp; i < nn; ++i) #define vi vector<int> #define vll vector<ll> #define vs vector<string> #define endl "\n" void __print(int x) {cerr << x;} void __print(long x) {cerr << x;} void __print(long long x) {cerr << x;} void __print(unsigned x) {cerr << x;} void __print(unsigned long x) {cerr << x;} void __print(unsigned long long x) {cerr << x;} void __print(float x) {cerr << x;} void __print(double x) {cerr << x;} void __print(long double x) {cerr << x;} void __print(char x) {cerr << '\'' << x << '\'';} void __print(const char *x) {cerr << '\"' << x << '\"';} void __print(const string &x) {cerr << '\"' << x << '\"';} void __print(bool x) {cerr << (x ? "true" : "false");} string int_to_string(int x) { stringstream ss; ss << x; string ni = ss.str(); return ni; } int string_to_int(string x) { int n; stringstream s(x); s >> n; return n; } int64_t power_mod(ll a, ll b, ll mo) { ll ans = 1; while (b) { if(b & 1) { ans = (ans % mo * a % mo) % mo; } a = (a % mo * a % mo) % mo; b /= 2; } return ans; } int64_t POW(ll a, ll b) { ll ans = 1; while (b) { if(b & 1) { ans = ans * a; } a = a * a; b /= 2; } return ans; } vector<int64_t> INVERSE(int64_t nn, int64_t mo) { vector<int64_t> inv(nn + 1); vector<int64_t> mul(nn + 1); inv[0] = 1; inv[1] = 1; for (ll i = 2; i <= nn; ++i) { inv[i] = (mo - (mo / i) * inv[mo % i] % mo) % mo; } mul[0] = 1; for (int i = 1; i <= nn; ++i) { mul[i] = (mul[i - 1] * inv[i]) % mo; } return mul; } template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';} template<typename T> void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);} #ifndef ONLINE_JUDGE #define debug(x...) cerr << "[" << #x << "] = ["; _print(x) #else #define debug(x...) #endif int mod = 1e9 + 7; void add_self(int &a , int b) { a += b; if (a >= mod) { a -= mod; } } int main() { IOS; //READ THE QUESTION PROPERLY BSDK string s; int d; cin >> s >> d; int n = (int)s.size(); vector<vector<int>> dp(d,vector<int> (2)); //int m = 1e9 + 7; dp[0][0] = 1; for (int i = 0; i < n; ++i) { vector<vector<int>> new_dp(d, vector<int> (2)); for (int j = 0; j < d; ++j) { for (bool sm_already : {false, true}) { for (int digit = 0; digit < 10; ++digit) { if (digit > s[i] - '0' && !sm_already) { break; } //debug(j,dp[j][0], s[i] - '0',digit,i); add_self(new_dp[(j + digit) % d][sm_already || (digit < s[i] - '0')],dp[j][sm_already]); } } } //adebug(new_dp); dp = new_dp; } int answer = (dp[0][0] + dp[0][1]) % mod; answer--; if (answer == -1) { answer = mod - 1; } cout << answer; }
0
#include <iostream> using namespace std; int main(void){ // Your code here! int x; int y; int z; cin >> x; cin >> y; cin >> z; cout << z << " " << x << " " << y << endl; }
#include <bits/stdc++.h> using namespace std; int main(){ int o,u,c; cin >> o >> u >> c; if(o<c&&c<u){ cout << "Yes" << endl; } else if(o>c&&c>u){ cout << "Yes" << endl; } else{ cout << "No" << endl; } }
0
#include <iostream> #include<vector> #include<algorithm> #include<set> using namespace std; int main() { int N, Y; cin >> N >> Y; int set[3] = {-1,-1,-1}; for (int i = 0; i <= N; i++) { for (int j = 0; i + j <= N; j++) { int k = N - i - j; int z = 10000 * i + 5000 * j + 1000 * k; if (z == Y) { set[0] = i; set[1] = j; set[2] = k; goto loopend; } else if(z > Y) { break; } } } loopend: cout << set[0] << ' ' << set[1] << ' ' << set[2]; }
#include<bits/stdc++.h> #include<stdio.h> using namespace std; typedef long long ll; #define i(x) cin>>x #define fr(n,x) for(ll n=0;n<x;n++) #define pf(x) cout<<x const ll mod=1e9+7 ; int main() { ios_base::sync_with_stdio(false),cin.tie(NULL); ll x,y; cin>>x>>y; if (y-x>=2019) { cout<<0; return 0; } ll res=INT_MAX; for(ll n=x;n<=y;n++) { for(ll k=n+1;k<=y;k++) { res=min(res,((n%2019)*(k%2019))%2019); } } cout<<res; }
0
#include <bits/stdc++.h> //#include <tr1/unordered_map> //#include"Bignum/bignum.h" //#define big bignum #define lowbit(x) (x & -x) #define debug(x) (cout << "#x = " << (x) << endl) #define Set(x, i) memset (x, i, sizeof(x)) #define R register #define For(i, j, k) for(R int i = (j); i <= (int) (k); ++i) #define Rep(i, j, k) for(R int i = (j); i >= (int) (k); --i) #define Cross(i, j, k) for(R int i = (j); i; i = (k)) using namespace std; typedef long long ll; const ll N = 21; const ll INF = 5e16; namespace IO { inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return (p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)? EOF: *p1++; } #define dd ch = getchar() inline ll read() { R ll x = 0; R int f = 0; R char dd; for (; !isdigit(ch); dd) f ^= (ch == '-'); for (; isdigit(ch); dd) x = x * 10 + (ch ^ 48); return f? -x: x; } #undef dd inline void write ( ll x ) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 | 48); } inline void wrn ( ll x ) { write(x), putchar(' '); } inline void wln ( ll x ) { write(x), putchar('\n'); } inline void wlnn ( ll x, ll y ) { wrn(x), wln(y); } } using IO::wln; using IO::read; namespace Cesare { /* f[i][S] 表示当前走到 i ,走过的路径 状态为 S 最大可以保留的权值和。 每次转移枚举加入一个点集还是一个点。 这基于一个性质:每次加入的一个点或点集必定 只与一条简单路径上的一个点相连,所以不会影 响简单路径数量。 f[j][S | (1 << j - 1)] = max f[i][S] + w[i][j]) now <- 包含 j 但不包含 S 的所有状态 f[j][S | now] = max f[i][S] + g[now] + d[i][now] */ ll n, m, Max, w[N][N]; ll g[(1 << 16) + 3], d[N][(1 << 16) + 3], f[N][(1 << 16) + 3]; inline void Cmax ( ll &a, ll b ) { (a < b) && (a = b); } int main() { n = read(), m = read(); for (int i = 1, u, v; i <= m; ++i) u = read(), v = read(), w[u][v] = w[v][u] = read(); Set (f, -1); f[1][1] = 0, Max = (1 << n) - 1; For ( S, 0, Max ) For ( i, 1, n ) if (S & (1 << i - 1)) For ( j, 1, i ) if ((i ^ j) && (S & (1 << j - 1))) g[S] += w[i][j]; For ( S, 0, Max ) For ( i, 1, n ) if (!(S & (1 << i - 1))) For ( j, 1, n ) if ((S & (1 << j - 1))) d[i][S] += w[i][j]; For ( S, 0, Max ) For ( i, 1, n ) if (S & (1 << i - 1) && f[i][S] >= 0) { ll nowS = Max ^ S; for (int now = nowS; now; now = nowS & (now - 1)) Cmax(f[i][S | now], f[i][S] + g[now] + d[i][now]); // 转移点集 for (int j = 1; j <= n; ++j) if (!(S & (1 << j - 1)) && w[i][j]) Cmax(f[j][S | (1 << j - 1)], f[i][S] + w[i][j]); // 转移点 } return wln(g[Max] - f[n][Max]), 0; } } int main() { // freopen(".in", "r", stdin); // freopen(".out", "w", stdout); return Cesare :: main(); } /* */
#include <iostream> #include <algorithm> using namespace std; const int inf = 1012345678; int N, M, a[114], b[114], c[114], g[19][19], sum[32777], val[32777], dp[19][32777], dp2[32777]; int main() { cin >> N >> M; int all_cost = 0; for (int i = 0; i < M; i++) { cin >> a[i] >> b[i] >> c[i]; a[i]--, b[i]--; sum[(1 << a[i]) + (1 << b[i])] += c[i]; g[a[i]][b[i]] = g[b[i]][a[i]] = c[i]; all_cost += c[i]; } for (int i = 0; i < M; i++) { for (int j = i + 1; j < M; j++) { if (c[i] < c[j]) { swap(a[i], a[j]); swap(b[i], b[j]); swap(c[i], c[j]); } } } for (int i = 0; i < N; i++) { for (int j = 0; j < 1 << N; j++) { dp[i][j] = -inf; } } dp[0][1] = 0; for (int i = 3; i < 1 << N; i += 2) { for (int j = 0; j < N; j++) { if (i & (1 << j)) { int sb = i - (1 << j); for (int k = 0; k < N; k++) { if ((sb & (1 << k)) && g[j][k] != 0) { dp[j][i] = max(dp[j][i], dp[k][sb] + g[j][k]); } } } } } for (int i = 0; i < N; i++) { for (int j = 0; j < 1 << N; j++) { if (!(j & (1 << i))) { sum[j + (1 << i)] += sum[j]; } } } int ret = 0; for (int i = 0; i < 1 << N; i++) { if ((i & 1) && (i & (1 << (N - 1)))) { int sb = (1 << N) - i - 1; for (int j = sb; j >= 0; j--) { j &= sb; val[j] = 0; for (int k = 0; k < N; k++) { if (i & (1 << k)) { val[j] = max(val[j], sum[j + (1 << k)]); } } } dp2[0] = 0; for (int j = sb; j >= 0; j--) { j &= sb; if (j == sb) continue; int rj = sb - j; dp2[rj] = 0; for (int k = rj; k > 0; k = (k - 1) & rj) { dp2[rj] = max(dp2[rj], dp2[rj - k] + val[k]); } } ret = max(ret, dp2[sb] + dp[N - 1][i]); } } cout << all_cost - ret << endl; return 0; }
1
#include <iostream> int main() { int64_t x; std::cin >> x; // 5と6を交互に出す // どちらから始めてもいいので、最後を6にできる const auto r = x % 11; std::cout << x / 11 * 2 + (r + 5) / 6 << std::endl; return 0; }
#include<stdio.h> #include<iostream> #include <algorithm> #include <functional> #include <array> using namespace std; long long modpow(long long a, long long n, long long mod); long long modinv(long long a, long long mod); //void margesort(int a[], int a_len, int left, int right); //void margesortD(int a[], int b[], int a_len, int left, int right); //int temp[100000]; //int tempb[100000]; //int b[100000]; //int c[100000]; int main() { /* int a, b; cin >> a; cin >> b; if (a <= 5) { cout << 0; } else if (a <= 12) { cout << b / 2; }else{ cout << b; } return 0; */ //B /*long long int r, d, x; cin >> r; cin >> d; cin >> x; int i; for (i = 0; i < 10; i++) { x = r * x - d; cout << x; cout <<'\n'; } return 0;*/ //C /*int n, m; int l, r; cin >> n; cin >> m; int lmax, rmin; lmax = 1; rmin = n; int i; for (i = 0; i < m; i++) { cin >> l; cin >> r; if (l > lmax) { lmax = l; } if (r < rmin) { rmin = r; } } if (lmax <= rmin) { cout << (rmin - lmax + 1); } else { cout << 0; } return 0;*/ //D /*int n, m; cin >> n; cin >> m; int i; int a[100000]; for (i = 0; i < n; i++) { cin >> a[i]; } margesort(a, n, 0, n - 1); for (i = 0; i < m; i++) { cin >> b[i]; cin >> c[i]; } margesortD(c, b, m, 0, m - 1); //sort(b.begin(), b.end()); long long int d; d = 0; int j, k, flag; i = 0; j = m - 1; flag = 0; while (flag == 0) { for (k = i; k < i + b[j]; k++) { if (a[k] < c[j]) { a[k] = c[j]; } else { flag = 1; } if (k == n) { flag = 1; break; } } i = i + b[j]; j--; if (j == -1) { flag = -1; } } for (i = 0; i < n; i++) { d = d + a[i]; } cout << d; return 0; */ //E long long int n, m, k; cin >> n; cin >> m; cin >> k; long long int p; p = 1000000007; long long int c; c = 0; //2駒総コストを求める. int i; int j; c = (((((((n * m) % p) * (n + m)) % p) * (n * m - 1)) % p) * modinv(6, p)) % p; //それを(nm-2)C(k-2)倍する. j = 1; for (i = n*m-2; i > (n * m - 2)-(k-2); i--) { c = (c * i) % p; c = (c * modinv(j, p)) % p; j++; } cout << c; return 0; } /*void margesort(int a[], int a_len, int left, int right) { int i, j, mid, L, R; if (right <= left) return; mid = (left + right) / 2; margesort(a, mid, left, mid); margesort(a, a_len - mid, mid+1, right); for (i = left; i <= mid; i++) temp[i] = a[i]; for (i = mid+1, j = right; i <= right; i++, j--) temp[i] = a[j]; L = left; R = right; for (i = left; i <= right; i++) { if (temp[L] <= temp[R]) { a[i] = temp[L]; L++; } else { a[i] = temp[R]; R--; } } } void margesortD(int a[], int b[], int a_len, int left, int right) { int i, j, mid, L, R; if (right <= left) return; mid = (left + right) / 2; margesortD(a, b, mid, left, mid); margesortD(a, b, a_len - mid, mid+1, right); for (i = left; i <= mid; i++) { temp[i] = a[i]; tempb[i] = b[i]; } for (i = mid+1, j = right; i <= right; i++, j--) { temp[i] = a[j]; tempb[i] = b[j]; } L = left; R = right; for (i = left; i <= right; i++) { if (temp[L] <= temp[R]) { a[i] = temp[L]; b[i] = tempb[L]; L++; } else { a[i] = temp[R]; b[i] = tempb[R]; R--; } } }*/ // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } // a^{-1} mod を計算する long long modinv(long long a, long long mod) { return modpow(a, mod - 2, mod); }
0
#include <iostream> #include <string> using namespace std; enum eKey{ ENCYPTION, CONJUGATED, }; void conv( string& s, int i, eKey e ){ if ( e == CONJUGATED ){ i = 26 - i; } for( unsigned j = 0; j < s.size(); ++j ){ if ( s[ j ] >= 'a' && s[ j ] <= 'z' ){ s[ j ] = ( s[ j ] - 'a' + i ) % 26 + 'a'; } } } int main(){ string s, sThe, sThis, sThat; while ( getline ( cin, s ) ){ for( int i = 0; i < 26; ++i ){ sThe = "the"; sThis = "this"; sThat = "that"; conv( sThe, i , ENCYPTION ); conv( sThis, i , ENCYPTION ); conv( sThat, i , ENCYPTION ); if ( s.find( sThe ) != string::npos || s.find( sThis ) != string::npos || s.find( sThat ) != string::npos ){ conv( s, i, CONJUGATED ); break; } } cout << s << endl; } return 0; }
#include<cstdio> #include<iostream> #include<cstring> using namespace std; int key; int dist(char s1,char s2){ if(s1==s2){ return 0; }else if(s1<s2){ return s2-s1; }else{ return 'z'-s1+s2-'a'+1; } } bool checkThe(char s[]){ if(strlen(s)!=3){ return false; }else{ if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('e',s[2])){ key=dist('t',s[0]); return true; } } return false; } bool checkThis(char s[]){ if(strlen(s)!=4){ return false; }else{ if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('i',s[2]) && dist('i',s[2])==dist('s',s[3])){ key=dist('t',s[0]); return true; } } return false; } bool checkThat(char s[]){ if(strlen(s)!=4){ return false; }else{ if(dist('t',s[0])==dist('h',s[1]) && dist('h',s[1])==dist('a',s[2]) && dist('a',s[2])==dist('t',s[3])){ key=dist('t',s[0]); return true; } } return false; } int main(){ char str[100]; char temp[100]; char *tok; bool f; while(fgets(str,sizeof(str),stdin)!=NULL){ key=0; f=false; for(int i=0;i<strlen(str);i++) temp[i]=str[i]; temp[strlen(str)]='\0'; str[strlen(str)]='\0'; str[strlen(str)-1]='\0'; tok=strtok(str," "); if(strlen(tok)==3){ if(checkThe(tok)==true){ f=true; } }else if(strlen(tok)==4){ if(checkThat(tok)==true){ f=true; }else if(checkThis(tok)==true){ f=true; } } while(f==false && tok!=NULL){ tok=strtok(NULL," "); if(tok!=NULL){ if(strlen(tok)==3){ if(checkThe(tok)==true){ f=true; } }else if(strlen(tok)==4){ if(checkThat(tok)==true){ f=true; }else if(checkThis(tok)==true){ f=true; } } } } for(int i=0;i<strlen(temp);i++){ if('a'<=temp[i] && temp[i]<='z'){ if(temp[i]-key>'z'){ printf("%c",(temp[i]-key)); }else if(temp[i]-key<'a'){ printf("%c",(temp[i]-key)+26); }else{ printf("%c",(temp[i]-key)); } }else{ printf("%c",temp[i]); } } } return 0; }
1
#include<bits/stdc++.h> using namespace std ; #define ll long long #define pb push_back #define mod 998244353 const ll N =30005 ; ll dp[2*N] ; int main () { ll n , t ; cin>> n >> t ; ll i ,j,x,y; dp[0] = 1 ; vector<pair<ll,ll> > v(n) ; for(i = 0 ; i< n ;i++) cin >> v[i].first >> v[i].second ; sort(v.begin(),v.end()) ; for(i = 1 ; i<= n ; i++) { x = v[i-1].first , y = v[i-1].second ; for (j = t-1 ; j>=0 ; j--) if (dp[j]) dp[j+x] = max(dp[j+x],dp[j]+y) ; } ll ans = 0 ; for (i = 0 ; i<2*N ; i++) ans = max(ans,dp[i]) ; cout << ans -1 << endl ; return 0 ; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pll = pair<ll, ll>; using vl = vector<ll>; using vvl = vector<vector<ll>>; /* short */ #define pb push_back #define eb emplace_back #define mp make_pair #define Fi first #define Se second #define ALL(v) begin(v), end(v) #define RALL(v) rbegin(v), rend(v) /* REPmacro */ #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define REP(i, n) for(int i = 0; i < (n); i++) /* exchange */ #define chmin(a, b) (a) = min((ll)(a), (ll)(b)) #define chmax(a, b) (a) = max((ll)(a), (ll)(b)) /* output */ #define I(x) cin >> x; #define D(x) cerr << (x) << " "; #define BR cerr << "\n"; #define P(x) cout << (x) << endl; #define FIX cout << fixed << setprecision(10); /* const */ const int ARRAY = 100005; const int INF = 1001001001; // 10^9 const ll LINF = 1001001001001001001; // 10^18 const int MOD = 1e9 + 7; ll N = 0; ll Q; ll ret = 0; struct Event { ll t, type, X; bool operator<(const Event& a) { return t < a.t; } }; vector<Event> eves; multiset<ll> ms; int main(void){ I(N); I(Q); ll S, T, X; REP(i, N) { I(S); I(T); I(X); Event a = {S-X, 1, X}; Event b = {T-X, -1, X}; eves.pb(a); eves.pb(b); } sort(ALL(eves)); ll d; ll k = 0; REP(i, Q) { I(d); while (k < eves.size() && eves[k].t <= d) { Event e = eves[k]; if (e.type == 1) { ms.insert(e.X); } else { ms.erase(ms.find(e.X)); } k++; } if (ms.empty()) { P(-1); } else { P(*ms.begin()); } } }
0
#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; ll arr[3]; for(i=0;i<3;i++) cin>>arr[i]; sort(arr,arr+3); cout<<arr[0]+arr[1]; }
#include "bits/stdc++.h" using namespace std; int main() { int A,B; cin >> A>>B; int ans = 0; for (int n = 0;n<2; ++n) { if (A > B) { ans += A; --A; } else { ans += B; --B; } } cout << ans<<endl; return 0; }
0
#include <iostream> #include <string> #include <algorithm> #include <cstdio> #include <cmath> #include <iomanip> using namespace std; int main () { double a,b,c,d; cin >> a >> b >> c >> d; cout << setprecision(20) <<sqrt((a-c)*(a-c)+(b-d)*(b-d)); return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<vector> #include<utility> #include<queue> #include<deque> #include<stack> #include<set> #include<map> #include<bitset> #include<string> #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 all(v) v.begin(), v.end() #define allr(v) v.rbegin(), v.rend() #define SZ(x) ((int)(x).size()) #define pb push_back #define mp make_pair #define fs first #define sc second #define lb lower_bound #define ub upper_bound #define LB(a,x) lb(all(a), x) - a.begin() #define UB(a,x) ub(all(a), x) - a.begin() #define MOD 1000000007 #define itn int #define enld endl using namespace std; typedef long long ll; const double pi = 3.141592653589793; 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 A, size_t N, typename T> void Fill(A (&array)[N], const T &val){ std::fill( (T*)array, (T*)(array+N), val ); } int main(){ int N; cin>> N; string A,B,C; cin >> A >> B >> C; int ans =0; rep(i,N){ int cnt = 0; if((A[i] != B[i]) && (B[i] != C[i]) && (C[i] != A[i])) ans += 2; else if((A[i] == B[i]) && (B[i] == C[i]) && (C[i] == A[i])) continue; else ans += 1; } cout << ans << enld; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; const ll mod=1000000007; int main(void){ string s; cin>>s; int n=s.size(); bool ok=false; int L=-1,R=-1; if(n==2&&s[0]==s[1]){ L=1,R=2; } for(int i=0;i<n-2;i++){ if(s[i]==s[i+1]||s[i+1]==s[i+2]||s[i+2]==s[i]){ L=i+1,R=i+3; } } cout<<L<<" "<<R<<endl; }
#include <bits/stdc++.h> using namespace std; /*{{{*/ //template #define rep(i,n) for(int i=0;i<(int)(n);i++) constexpr int INF = numeric_limits<int>::max()/2; constexpr long long LINF = numeric_limits<long long>::max()/3; #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define all(v) (v).begin(),(v).end() #define sz(x) (int)(x).size() #define debug(x) cerr<<#x<<":"<<x<<endl #define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl //struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_; struct Double{ double d; explicit Double(double x) : d(x){} }; ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; } template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; } template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; } template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; } template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; } template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; } typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); } //constexpr double eps = 1e-14; constexpr double eps = 1e-10; constexpr ll mod = 1e9+7; const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1}; /*}}}*/ int main(){ ll K; cin >> K; const ll N = 50; vector<ll> a(N,N-1+K/N); ll rem = K%N; for(int i=0;i<rem;i++){ a[i] += N - (rem-1); } for(int i=rem;i<N;i++){ a[i] -= rem; } cout << N << endl; for(int i=0;i<N;i++){ if(i) cout << " "; cout << a[i]; } cout << endl; }
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 debug(x) cerr << #x << " = " << (x) << endl; 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; } //Write From this Line int main() { ll a, b, k; cin >> a >> b >> k; int count = 1; while(k--){ if(count%2){ // a をそうさ if(a%2) a--; b += a/2; a /= 2; } else{ if(b%2) b--; a += b/2; b /= 2; } count ++ ; } cout << a << " " << b << endl; }
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;n>i;++i) using namespace std; using ll=int64_t; using vi=vector<int>; using VI=vector<ll>; using vis=vector<string>; using vvi=vector<vi>; int main(){ int h,w,n; cin>>h>>w>>n; if(n%max(h,w)==0){ cout<<n/max(h,w)<<endl; return 0; } else{ cout<<n/max(h,w)+1<<endl; return 0; } }
0
#include<bits/stdc++.h> #define mp make_pair #define endl "\n" #define v vector #define b begin() #define e end() using namespace std; typedef long long ll; typedef long l; typedef long long unsigned ull; int main(){ /*int t; while(t--){ }*/ ll n; cin>>n; ll m = n/500; ll q = m*1000; n -= (m*500); ll p = (n/5)*5; cout<<q+p; return 0; }
#include<iostream> using namespace std ; int main() { string S ; cin>>S ; int SIZE=S.size() ; if(SIZE==2) cout<<S<<endl ; else for(int i=SIZE-1;i>=0;i--) cout<<S.at(i) ; return 0 ; }
0
#define _USE_MATH_DEFINES #include <iostream> #include <fstream> #include <cstdio> #include <cmath> #include <cstdlib> #include <cstring> #include <cassert> #include <string> #include <vector> #include <utility> #include <complex> #include <set> #include <map> #include <queue> #include <stack> #include <deque> #include <tuple> #include <bitset> #include <limits> #include <algorithm> #include <array> #include <random> #include <complex> #include <regex> using namespace std; typedef long double ld; typedef long long ll; typedef vector<int> vint; typedef vector<ll> vll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef pair<double, double> pdd; typedef complex<ld> compd; #define quickIO() {cin.tie(0); cout.sync_with_stdio(false);} #define reach(i,a) for(auto i:a) #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 srep(i,a,n) for(int i=a;i<(int)n;i++) #define SREP(i,a,n) for(int i=a;i<=(int)n;i++) #define rrep(i,n) for(int i=n-1;i>=0;i--) #define RREP(i,n) for(int i=n;i>=0;i--) #define all(a) (a).begin(),(a).end() #define mp(a,b) make_pair(a,b) #define mt make_tuple #define pb push_back #define fst first #define scn second int bitcnt(ll x) { x = ((x & 0xAAAAAAAAAAAAAAAA) >> 1) + (x & 0x5555555555555555); x = ((x & 0xCCCCCCCCCCCCCCCC) >> 2) + (x & 0x3333333333333333); x = ((x & 0xF0F0F0F0F0F0F0F0) >> 4) + (x & 0x0F0F0F0F0F0F0F0F); x = ((x & 0xFF00FF00FF00FF00) >> 8) + (x & 0x00FF00FF00FF00FF); x = ((x & 0xFFFF0000FFFF0000) >> 16) + (x & 0x0000FFFF0000FFFF); x = ((x & 0xFFFFFFFF00000000) >> 32) + (x & 0x00000000FFFFFFFF); return x; } int bitcnt(int x) { x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555); x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333); x = ((x & 0xF0F0F0F0) >> 4) + (x & 0x0F0F0F0F); x = ((x & 0xFF00FF00) >> 8) + (x & 0x00FF00FF); x = ((x & 0xFFFF0000) >> 16) + (x & 0x0000FFFF); return x; } ll sqrt(ll x) { ll left = 0, right = x; rep(i, 100) { ll mid = (left + right) >> 1; if (mid*mid <= x) left = mid; else right = mid; } return left; } ll gcd(ll a, ll b) { return a%b == 0 ? b : gcd(b, a%b); } #define debug(x) printf("Case #%d: ", x) #define DEBUG 0 const ll inf = 1e18; const ll mod = 1e9 + 7; const ld eps = 1e-9; const int dx[] = { 1,0,-1,0,0 }; const int dy[] = { 0,1,0,-1,0 }; int main() { ll n, start; cin >> n >> start; vector<ll> x(n); vector<ll> p(n); rep(i, n) cin >> x[i] >> p[i]; vector<ll> pos; int s = 0, t = n - 1; while (x[s]<start && start<x[t]) { if (p[s] < p[t]) { pos.push_back(x[s]); p[t] += p[s]; s++; } else { pos.push_back(x[t]); p[s] += p[t]; t--; } } while (s != t) { if (x[s] < start) pos.push_back(x[s++]); else pos.push_back(x[t--]); } pos.push_back(x[s]); ll cur = start, ret = 0; rrep(i, pos.size()) { ret += labs(pos[i] - cur); cur = pos[i]; } cout << ret << endl; return 0; }
// This amazing code is by Eric Sunli Chen. #include<bits/stdc++.h> using namespace std; template<typename T> bool get_int(T &x) { char t=getchar(); bool neg=false; x=0; for(; (t>'9'||t<'0')&&t!='-'&&t!=EOF; t=getchar()); if(t=='-')neg=true,t=getchar();if(t==EOF)return false; for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0'; if(neg)x=-x;return true; } template<typename T> void print_int(T x) { if(x<0)putchar('-'),x=-x; short a[20]= {},sz=0; while(x>0)a[sz++]=x%10,x/=10; if(sz==0)putchar('0'); for(int i=sz-1; i>=0; i--)putchar('0'+a[i]); } #define ff first #define ss second #define pb push_back #define mp make_pair #define get1(a) get_int(a) #define get2(a,b) (get1(a)&&get1(b)) #define get3(a,b,c) (get1(a)&&get2(b,c)) #define printendl(a) print_int(a),puts("") typedef long long LL; typedef unsigned long long uLL; typedef pair<int,int> pii; const int inf=0x3f3f3f3f; const LL Linf=1ll<<61; const double pi=acos(-1.0); const int maxn=100111; int n,s,x[maxn]; LL p[maxn],ans; int calc(int l,int r) { if(x[r]<s) { ans+=s-x[l]; return l; } if(x[l]>s) { ans+=x[r]-s; return r; } if(p[l]>=p[r]) { p[l]+=p[r]; ans+=x[r]-x[calc(l,r-1)]; return r; } else { p[r]+=p[l]; ans+=x[calc(l+1,r)]-x[l]; return l; } } int main() { get2(n,s); for(int i=1;i<=n;i++)get2(x[i],p[i]); calc(1,n); printendl(ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using st=string; using ch=char; typedef pair<ll,ll> P; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<P> vP; typedef vector<ch> vc; typedef vector<vc> vvc; #define FOR(i,a,b) for(ll i=a;i<b;i++) #define rep(i,n) FOR(i,0,n) #define ROF(i,a,b) for(ll i=a;i>=b;i--) #define per(i,a) ROF(i,a,0) const ll MOD=1000000007; const ll MOD2=998244353; const ld PI=acos(-1); const ll INF=1e18; st abc="abcdefghijklmnopqrstuvwxyz"; st ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ"; struct edge{ll to,cost;}; int main() { st s; cin >> s; st t=""; rep(i,s.size()-8){ t+=s[i]; } cout << t << endl; }
#include <stdio.h> #include <string.h> int main() { int i; char str[21]; scanf("%s",str); for(i=strlen(str)-1; i>=0; i--) printf("%c",str[i]); printf("\n"); return 0; }
0
#include <iostream> #include <queue> #include <string> #include <algorithm> using namespace std; typedef pair<string, int> P; int main() { int n; while(cin >> n && n){ int x[20], y[20]; for(int i = 0; i < n; ++i) cin >> x[i] >> y[i]; int m; cin >> m; queue<P> q; string s; int d; for(int i = 0; i < m; ++i){ cin >> s >> d; q.push(P(s, d)); } int c = n, nowx = 10, nowy = 10; bool ca[20] = {false}; while(!q.empty()){ P p = q.front(); q.pop(); for(int i = 0; i < p.second; ++i){ string str = p.first; if(str == "N") nowy++; else if(str == "E") nowx++; else if(str == "S") nowy--; else if(str == "W") nowx--; for(int j = 0; j < n; ++j){ if(nowx == x[j] && nowy == y[j] && !ca[j]){ ca[j] = true; c--; } } if(c == 0) break; } } if(c == 0) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }
#include<iostream> #include<cstdio> using namespace std; int main(){ int a,b,c; while(cin>>a>>b>>c,a||b||c){ int n,parts[500],input[10000][4],bd; for(int i=0;i<500;i++) parts[i]=2; cin>>n; for(int _i=0;_i<n;_i++){ for(int _j=0;_j<4;_j++){ cin>>input[_i][_j]; } if(input[_i][3]==1){ for(int i=0;i<3;i++){ parts[input[_i][i]]=1; } } for(int i=0;i<=_i;i++){ if(input[i][3]==1) continue; bd=-1; for(int j=0;j<3;j++){ if(parts[input[i][j]]==2 && bd==-1) bd=input[i][j]; else if(parts[input[i][j]]!=1){ bd=-1; break; } } if(bd!=-1) parts[bd]=0; } } for(int i=1;i<=a+b+c;i++){ printf("%d\n",parts[i]); } } return 0; }
0
#include <bits/stdc++.h> #define booga cout << "booga" << endl #define ll long long int /* run this program using the console pauser or add your own getch, system("pause") or input loop */ using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(0); int n,a,b; cin >> n >> a >> b; if(n == 1){ cout << "Hello World "; } else{ cout << a+b; } return 0; }
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { double ax, ay, bx, by; cin >> ax >> ay >> bx >> by; printf("%.6f\n",sqrt(pow(bx-ax,2)+pow(by-ay,2))); return 0; }
0
#include <bits/stdc++.h> using namespace std; #define M 1000000007 long long int power(int a,int b) { if(b==0) return 1; long long int k=power(a,b/2); if(b%2==0) return ((k%M)*(k%M))%M; else return ((k%M)*(k%M)*(a%M))%M; } long long int fact(long long int n) { if(n==1 || n==0) return 1; else return ((n%M)*(fact(n-1)%M))%M; } int div(int n) { int c=0; for(int i=1;i<=sqrt(n);i++) { if(n%i==0) { if(n/i==i) c++; else c+=2; } } return c; } int func(int n) { return (n*(n+1))/2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int n,m; cin>>n; map<string,int> mp; while(n--) { string str; cin>>str; mp[str]++; } cin>>m; while(m--) { string str; cin>>str; mp[str]--; } map<string,int> :: iterator itr; int cmax=0; for(itr=mp.begin();itr!=mp.end();itr++) { if(itr->second > cmax) { cmax= itr-> second; } } cout<<cmax; return 0; }
#include <bits/stdc++.h> using namespace std; #define ll long long int main(){ ios::sync_with_stdio(false); cin.tie(0); int N; cin >> N; vector<int> a(N); bool ok=true; int ans=1e9; //O(N * LOG(MAX(a))) for(int& x : a) { cin >> x; if(x&1) ok=false; int cnt=0; while(x) { if(x&1) { ans=min(ans,cnt); break; } cnt++; x/=2; } } if(!ok) { puts("0");return 0; }else { cout << ans; } return 0; }
0
#include <bits/stdc++.h> using namespace std; #define _GLIBCXX_DEBUG #define rep(i, from, to) for (int i = from; i < (to); ++i) #define mp(x,y) make_pair(x,y) #define all(x) (x).begin(),(x).end() #define pb push_back using ll = long long; using vin=vector<int>; using vll=vector<ll>; using vst=vector<string>; using P = pair<int, int>; const int inf=1e9+7; const ll INF=1e18; template <typename T> void chmin(T &a, T b) { a = min(a, b); } template <typename T> void chmax(T &a, T b) { a = max(a, b); } template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; } template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; } const int dx[4] = { 1, 0, -1, 0 }; const int dy[4] = { 0, 1, 0, -1 }; int main(){cout<<fixed<<setprecision(10); string s[3]; rep(i,0,3)cin>>s[i]; YES(s[0][s[0].size()-1]==s[1][0]&&s[1][s[1].size()-1]==s[2][0]); }
#include<bits/stdc++.h> #include<string.h> using namespace std; int main() { char line[50]; cin.getline(line,50); char a,b,c,d; int count=0; for(int i=0;i<strlen(line);i++) { if(line[i]==' ') { if(count==0) { a=line[i-1]; b=line[i+1]; count=1; } else { c=line[i-1]; d=line[i+1]; } } } if(a==b && c==d) { cout<<"YES"; } else{ cout<<"NO"; } return 0; }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; void solve() { int N, M; while (cin >> N >> M, N || M) { vector<int> point(N); for (int i = 0; i < N; ++i) { cin >> point[i]; } point.push_back(0); vector<int> sum; for (int i = 0; i <= N; ++i) { for (int j = 0; j <= N; ++j) { sum.push_back(point[i] + point[j]); } } int ans = 0; sort(sum.begin(), sum.end()); for (int i = 0; i < sum.size(); ++i) { if (M - sum[i] < 0) { continue; } int left = 0; int right = sum.size(); while (left < right) { int middle = (left + right) / 2; if (sum[i] + sum[middle] > M) { right = middle; } else if (sum[i] + sum[middle] < M) { left = middle + 1; } else { break; } } ans = max(ans, sum[i] + sum[right - 1]); } cout << ans << endl; } } int main() { solve(); return(0); }
/// IN THE NAME OF GUITAR #include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> pll; #define sz(x) (ll) x.size() #define all(x) (x).begin(),(x).end() #define F first #define S second #define lc id << 1 #define rc lc | 1 ll Pow(ll a, ll b, ll md, ll ans = 1) { for (; b; b >>= 1, a = a * a % md) if (b & 1) ans = ans * a % md; return ans % md; } const ll MAXN = 2e3 + 20; const ll INF = 1e18; const ll MOD = 1e9 + 7; ll L[MAXN], R[MAXN], A[MAXN][MAXN], ps[MAXN][MAXN], n, m, ans; stack<ll> st; int main() { scanf("%lld%lld", &n, &m); for (ll i = 1; i <= n; i++) { string s; cin >> s; for (ll j = 1; j <= m; j++) { A[i][j] = (s[j - 1] == '#'); } } for (ll i = 1; i < n; i++) { for (ll j = 1; j < m; j++) { ll cnt = A[i][j] + A[i + 1][j] + A[i][j + 1] + A[i + 1][j + 1]; if (cnt & 1) ps[i][j] = 0; else ps[i][j] = ps[i - 1][j] + 1; } } ans = max(n, m); for (ll i = 1; i < n; i++) { while (sz(st)) st.pop(); st.push(0); for (ll j = 1; j < m; j++) { while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop(); if (sz(st)) L[j] = st.top(); st.push(j); } while (sz(st)) st.pop(); st.push(m); for (ll j = m - 1; j >= 1; j--) { while (sz(st) && ps[i][j] <= ps[i][st.top()]) st.pop(); if (sz(st)) R[j] = st.top(); st.push(j); } for (ll j = 1; j < m; j++) { ans = max(ans, (ps[i][j] + 1) * (R[j] - L[j])); } } printf("%lld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef vector<ll> vl; typedef pair<ll, ll> PP; #define rep(i, n) for(ll i = 0; i < ll(n); i++) #define all(v) v.begin(), v.end() #define inputv(v, n) \ vl v; \ rep(i, n) { \ ll x; \ cin >> x; \ v.push_back(x); \ } bool chmin(ll& a, ll b) { if (b < a) { a = b; return 1; } return 0; } bool chmax(ll& a, ll b) { if (b > a) { a = b; return 1; } return 0; } const ll INF = 999999999999999; const ll MOD = 1000000007; const ll MAX_N = 500010; ll a, b, c, d, e, f, p, t, x, y, z, q, m, n, r, h, k, w, l, ans; struct Gragh { ll N; vector<vl> G; vl visited; Gragh(ll n) { N = n; G.resize(N); resetv(); } void add(ll a, ll b) { G[a].push_back(b); } void resetv(void) { visited = vl(N, 0); } //重さ無し void dfs(ll x) { visited[x] = 1; for (ll i : G[x]) { if (visited[i] == 0) { dfs(i); } } } }; int main() { cin >> n >> m; Gragh G(2 * n); rep(i, m) { cin >> a >> b; a--; b--; G.add(a * 2, b * 2 + 1); G.add(b * 2, a * 2 + 1); G.add(a * 2+1, b * 2); G.add(b * 2+1, a * 2); } G.dfs(0); vl dp(2, 0); rep(i, n * 2) { if (G.visited[i] == 1)dp[i % 2]++; } x=dp[0]; y=dp[1]; k=x+y-n; ans=k*(n-k)+k*(k-1)/2+(x-k)*(y-k)-m; cout << ans << endl; }
#include <cstdio> #include <cstdlib> #include <iostream> #include <string> #include <cmath> #include <algorithm> #include <vector> #include <queue> #include <stack> using namespace std; void solve(int n,int m){ int fr[501][501],a,b,ans=0; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) fr[i][j]=100; for(int i=0;i<m;i++){ scanf("%d%d",&a,&b); fr[a][b]=1; fr[b][a]=1; } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) fr[i][j]=min(fr[i][j],fr[i][k]+fr[k][j]); for(int i=2;i<=n;i++) ans+=(fr[1][i]==2 || fr[1][i]==1); printf("%d\n",ans); return; } int main(){ int n,m; while(1){ scanf("%d%d",&n,&m); if(n+m==0) break; solve(n,m); } return 0; }
0
#include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #include <map> #include <set> #include <cstdio> #include <cmath> #define rep(i,l,n) for(lint i=l;i<n;i++) #define rer(i,l,n) for(lint i=l;i<=n;i++) #define all(a) a.begin(),a.end() #define o(a) cout<<a<<endl #define pb(a) push_back(a) #define mk(a,b) make_pair(a,b) #define fi first #define se second using namespace std; typedef long long lint; typedef vector<int> vi; typedef vector<lint> vli; typedef vector<vi> vvi; typedef pair<int,int> pii; int main(){ int n; cin>>n; vi d(n); int f,l,sf,sl; rep(i,0,n*(n-1)/2){ cin>>f>>l>>sf>>sl; f--; l--; if(sf<sl){ d[l]+=3; }else if(sf>sl){ d[f]+=3; }else{ d[l]++; d[f]++; } } vi e=d; sort(all(e)); rep(i,0,n){ int res=upper_bound(all(e),d[i])-e.begin(); o(n+1-res); } }
#include <bits/stdc++.h> using namespace std; #define int long long typedef pair<int,int> P; int INF = 1e9+7;; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; signed main() { int N; cin >> N; vector<P>ab(N); vector<P>cd(N); for(int i = 0; i < N; i++) { int a,b; cin >> a >> b; ab[i].first = a; ab[i].second = b; } for(int i = 0; i < N; i++) { int c,d; cin >> c >> d; cd[i].first = c; cd[i].second = d; } vector<bool>ok(N,false); sort(ab.begin(),ab.end()); reverse(ab.begin(),ab.end()); int ans = 0; for(int i = 0; i < N; i++) { int X = INF; int Y = INF; for(int j = 0; j < N; j++) { if(ab[i].first < cd[j].first && ab[i].second < cd[j].second) { if((!ok[j]) && X > cd[j].second) { X = cd[j].second; Y = j; } } } if(Y != INF) { ok[Y] = true; } } for(int i = 0; i < N; i++) { if(ok[i]) { ans++; } } cout << ans << endl; }
0
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue> #include <map> #include <set> #include <cmath> #include <vector> typedef long long ll; const int maxn = 1e5 + 5; const ll mod = 1e9 + 7; const double PI = acos(-1.0); ll a[maxn]; int main (int argc, char **argv) { int n; std::cin >> n; int cnt = 0; for (int i=1; i <= n; i++) { std::cin >> a[i]; if (a[i] < 0) { cnt++; } } ll ans = 0; if (cnt&1) { ll min_val = a[1] > 0 ? a[1]:-a[1]; for (int i=2; i <= n; i++) { min_val = std::min(min_val, a[i] > 0 ? a[i]:-a[i]); } ans -= 2*min_val; } for (int i=1; i <= n; i++) { if (a[i] < 0) { ans -= a[i]; } else { ans += a[i]; } } std::cout << ans << std::endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using vl = vector<ll>; template<class T> using vc = vector<T>; template<class T> using vvc = vector<vector<T>>; #define eb emplace_back #define all(x) (x).begin(), (x).end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define repr(i, n) for (ll i = (n)-1; i >= 0; i--) #define repe(i, l, r) for (ll i = (l); i < (r); i++) #define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--) #define repa(i,n) for (auto& i: n) 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;} struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_; #ifdef DEBUG template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; } ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; } template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); } #endif const ll INF = 4e18; const ld EPS = 1e-11; const ld PI = acos(-1.0L); const ll MOD = 1e9 + 7; // const ll MOD = 998244353; //--------------------------------------------------------------------------------// int main() { ll N; cin >> N; vl A(N); rep(i, N) cin >> A[i]; vvc<ll> dp(N + 1, vl(2, -INF)); dp[0][0] = 0; rep(i, N){ dp[i + 1][0] = max(dp[i][0] + A[i], dp[i][1] - A[i]); dp[i + 1][1] = max(dp[i][0] - A[i], dp[i][1] + A[i]); } cout << dp[N][0] << endl; }
1
#include<iostream> #include<string> signed main() { int h,w,a,b; scanf("%d%d%d%d",&h,&w,&a,&b); std::string s(a,'0'); std::string t(w-a,'1'); std::string ans=s+t; std::string S(a,'1'); std::string T(w-a,'0'); std::string Ans=S+T; for(int i=0;i<h;++i){ if(i<b){ printf("%s\n",ans.c_str()); } else{ printf("%s\n",Ans.c_str()); } } }
#include <bits/stdc++.h> #define pb push_back #define pll pair <ll, ll> #define mp make_pair #define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0); #define x first #define y second #pragma GCC optimize("O3") // #pragma GCC optimize("Ofast") // #pragma GCC optimize("unroll-loops") #define plll pair <ll, pair <ll, ll>> #define pllll pair <pair <ll, ll>, pair <ll, ll> > #define psl pair <string, ll> #define all(a) a.begin(), a.end() #define vvl vector <vector <ll> > typedef long long ll; typedef long double ld; using namespace std; const ll maxn = 1e3 + 10; ll n, m, k, t; ll x, y; ll ANS[maxn][maxn]; int main() { pyshnapyshnakaa; ll q, w, e, a, b, c; cin >> n >> m >> x >> y; for (q = 0; q < n; q++) { for (w = 0; w < x; w++) { ANS[q][w] = 0; } for (w = x; w < m; w++) { ANS[q][w] = 1; } } for (q = 0; q < y; q++) { for (w = 0; w < m; w++) { ANS[q][w] ^= 1; } } for (q = 0; q < n; q++) { for (w = 0; w < m; w++) { cout << ANS[q][w]; } cout << endl; } return 0; }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; int gcd(int x, int y){ return y ? gcd(y, x % y) : x; } void divisor(int n, vector<int>& vec){ for(int i=1;i*i<=n;i++){ if(n % i == 0){ vec.push_back(i); if(i != n / i) vec.push_back(n / i); } } } main(){ int n, in[3]; cin >> n; for(int i=0;i<n;i++){ cin >> in[i]; } vector<int> ans; if(n == 2) divisor(gcd(in[0], in[1]), ans); else divisor(gcd(in[0], gcd(in[1], in[2])), ans); sort(ans.begin(), ans.end()); for(int i=0;i<ans.size();i++) cout << ans[i] << endl; }
#include<iostream> #include<map> using namespace std; int main() { int n; cin >> n; map<int,int> ans; for(int i=0;i<n;i++) { int num; cin >> num; for(int j=1;j<=num/2;j++) { if(num%j == 0) ans[j]++; } ans[num]++; } for(map<int,int>::iterator it = ans.begin();it != ans.end();it++) if((*it).second >= n) cout << (*it).first << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; // boost #include <boost/multiprecision/cpp_int.hpp> using boost::multiprecision::cpp_int; using ll = long long; using ld = long double; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; using vld = vector<ld>; using vvld = vector<vld>; typedef pair<ll, ll> P; #define bit(n) (1LL << (n)) //#define int long long #define all(v) v.begin(), v.end() #define rep(i, n) for (ll i = 0; i < n; i++) #define REP(i, n) for (ll i = 1; i < n; i++) #define FOR(i, a, b) for (ll i = (a); i < (b); i++) #define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++) template <class T> inline void chmax(T& a, T b) { a = std::max(a, b); } template <class T> inline void chmin(T& a, T b) { a = std::min(a, b); } #define mod (ll)(1e9 + 7) // #define mod (998244353ll) const long long INF = 1LL << 60; signed main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); ll n, q; string s; cin >> n >> s >> q; vll vq(q); rep(i, q) cin >> vq[i]; vll nc(n + 2); rep(i, n) { if (s[i] == 'C') nc[i]++; } REP(i, n) { nc[i] += nc[i - 1]; } for (const auto k : vq) { ll ans = 0; vll imos(n + 2); vll dc(n + 2); rep(i, n) { if (s[i] == 'D') { dc[i] = nc[min(i + k - 1, n - 1)] - nc[i]; imos[i]++; if (i + k < n) { imos[i + k]--; } } } REP(i, n) { imos[i] += imos[i - 1]; } REP(i, n) { dc[i] += dc[i - 1]; if (s[i] == 'C') { dc[i] -= imos[i]; } else if (s[i] == 'M') { ans += dc[i]; } } cout << ans << endl; } return 0; }
#include<bits/stdc++.h> int main(){ using namespace std; constexpr unsigned long MOD = 998244353; string S; cin >> S; reverse(begin(S), end(S)); unsigned long K; cin >> K; K = min<unsigned long>(count(begin(S), end(S), '1'), K); vector<vector<unsigned long>> dp(K + 1), tmp(K + 1); for(unsigned long i{0}; i <= K; ++i)dp[i] = vector(i + 1, 0UL); for(unsigned long i{0}; i <= K; ++i)tmp[i] = vector(i + 1, 0UL); dp[0][0] = 1; unsigned long cnt{0}; for(const auto& c : S)if(c == '1')++cnt; else{ for(auto&& i : tmp)fill(begin(i), end(i), 0UL); for(unsigned long i{0}; i < K; ++i)for(unsigned long j{0}; j + i < K; ++j){ tmp[j + i + 1][i] = tmp[j + i][i] + dp[j + i][i]; if(j >= cnt)tmp[j + i + 1][i] -= dp[j - cnt + i][i]; } for(auto&& i : dp)partial_sum(begin(i), end(i), begin(i)); for(unsigned long i{0}; i <= K; ++i)for(unsigned long j{0}; j <= i; ++j)(dp[i][j] += tmp[i][j]) %= MOD; cnt = 0; } for(auto&& i : dp)partial_sum(begin(i), end(i), begin(i)); unsigned long ans{0}; for(const auto& i : dp)ans += i.back(); cout << ans % MOD << endl; return 0; }
0
#include<bits/stdc++.h> #define int long long using namespace std; const int mod = 1e9 + 7, maxN = 1e5 + 13; int n, x, dp[maxN], dpw[maxN]; vector<int> vec; int pw (int a, int b) { int ret = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ret = 1ll * ret * a % mod; return ret; } int32_t main () { cin >> n >> x; int cur = 1; for (int i = 0; i < n; i++) { int a; cin >> a; if (a <= x) vec.push_back(a); cur = (cur * (i + 1)) % mod; } sort(vec.begin(), vec.end()); for (int i = 0; i <= n; i++) dpw[i] = pw(i, mod - 2); for (int i = 0; i <= x; i++) { int pos = upper_bound(vec.begin(), vec.end(), i) - vec.begin(); if (pos == 0) dp[i] = i; for (int j = 0; j < pos; j++) { //cout << i << " " << vec[j] << " " << dp[i % vec[j]] << endl; dp[i] = (dp[i] + dpw[pos] * dp[i % vec[j]]) % mod; } //cout << i << " " << pos << " " << dp[i] << endl; } //cout << solve(x, n) << " " << cur << endl; cout << 1ll * dp[x] * cur % mod << endl; }
#include <iostream> #include <string> #include <algorithm> #include <vector> #include <iomanip> #include <queue> #include <stack> #include <cstdlib> #include <map> #include <iomanip> #include <set> #include <stdio.h> #include <ctype.h> #include <random> #include <string.h> #include <cmath> #include <unordered_map> #include <cstdio> using namespace std; #define all(vec) vec.begin(),vec.end() typedef long long ll; ll gcd(ll x, ll y) { if (y == 0)return x; return gcd(y, x%y); } ll lcm(ll x, ll y) { return x / gcd(x, y)*y; } ll kai(ll x, ll y, ll m) { ll res = 1; for (ll i = x - y + 1; i <= x; i++) { res *= i; res %= m; } return res; } ll mod_pow(ll x, ll y, ll m) { ll res = 1; while (y > 0) { if (y & 1) { res = res * x % m; } x = x * x % m; y >>= 1; } return res; } ll comb(ll x, ll y, ll m) { if (y > x)return 0; return kai(x, y, m) * mod_pow(kai(y, y, m), m - 2, m) % m; } const ll mod = 1000000007; int n, x; vector<int> vec[1050000]; ll d[500010]; ll ans; signed main() { cin >> n; for (int i = 1; i <= n; i++) { int a; cin >> a; x ^= a; vec[x].push_back(i); } if (x) { ll cnt1 = 0, cnt2 = 1; for (int j = 0; j < (int)vec[x].size(); j++) { int p = 0; if (j)p = vec[x][j - 1]; int t = lower_bound(all(vec[0]), vec[x][j]) - lower_bound(all(vec[0]), p); cnt2 = (cnt2 + cnt1 * t) % mod; cnt1 = (cnt1 + cnt2) % mod; } cout << cnt2 << endl; return 0; } for (int i = 1; i < (1 << 20); i++) { if ((int)vec[i].size() == 0)continue; ll cnt1 = 0, cnt2 = 1; for (int j = 0; j < (int)vec[i].size(); j++) { int p = 0; if (j)p = vec[i][j - 1]; int t = lower_bound(all(vec[0]), vec[i][j]) - lower_bound(all(vec[0]), p); cnt2 = (cnt2 + cnt1 * t) % mod; cnt1 = (cnt1 + cnt2) % mod; } ans = (ans + cnt1) % mod; } ll cnt = 1; for (int i = 1; i < (int)vec[0].size(); i++)cnt = cnt * 2 % mod; cout << (ans + cnt) % mod << endl; }
0
#include <bits/stdc++.h> #define REP(i, n) for (int i = 0; i < n; ++i) #define REPR(i, n) for (int i = n - 1; i >= 0; --i) #define FOR(i, a, b) for (int i = a; i < b; ++i) #define FORR(i, a, b) for (int i = b - 1; i >= a; --i) #define SORT(v) sort(v.begin(), v.end()) #define SORTR(v) sort(v.rbegin(), v.rend()) #define REV(v) reverse(v.begin(), v.end()) #define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr) #define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin()) #define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin()) #define SZ(v) (int)v.size() using namespace std; using ll = long long; using P = pair<int, int>; int main() { cin.tie(0); ios::sync_with_stdio(false); int k; cin >> k; vector<vector<int>> chess(8, vector<int>(8)); auto ok = [&](const vector<vector<int>> &newChess) { REP(i, 8) { int cnt = 0; REP(j, 8) { cnt += newChess[i][j]; if (cnt > 1) { return false; } } } REP(j, 8) { int cnt = 0; REP(i, 8) { cnt += newChess[i][j]; if (cnt > 1) { return false; } } } REP(i, 8) { int cnt = 0; REP(j, 8) { if (i + j == 8) break; cnt += newChess[i + j][j]; if (cnt > 1) { return false; } } } REP(i, 8) { int cnt = 0; REP(j, 8) { if (i - j < 0) break; cnt += newChess[i - j][j]; if (cnt > 1) { return false; } } } REP(j, 8) { int cnt = 0; REP(i, 8) { if (j + i == 8) break; cnt += newChess[i][j + i]; if (cnt > 1) { return false; } } } REP(j, 8) { int cnt = 0; REP(i, 8) { if (j + i == 8) break; cnt += newChess[7 - i][j + i]; if (cnt > 1) { return false; } } } return true; }; REP(i, k) { int r, c; cin >> r >> c; chess[r][c] = 1; } vector<int> v(8); REP(i, 8) { v[i] = i; } do { vector<vector<int>> newChess = chess; REP(i, 8) { newChess[i][v[i]] = 1; } if (ok(newChess)) { REP(i, 8) { REP(j, 8) { if (newChess[i][j]) { cout << 'Q'; } else { cout << '.'; } } cout << endl; } } } while (next_permutation(v.begin(), v.end())); return 0; }
#include <iostream> #include <cstdio> #include <cstdlib> using namespace std; const char Q = 'Q'; const char _ = '.'; char board[8][9] = { "........", "........", "........", "........", "........", "........", "........", "........", }; int n; bool pre_x[8], pre_y[8]; inline bool check(int x, int y) { int p = x, q = y; while (--p >= 0 && --q >= 0) { // upper left if (board[q][p] == Q) return false; } p = x; q = y; while (++p < 8 && --q >= 0) { // upper right if (board[q][p] == Q) return false; } p = x; q = y; while (++p < 8 && ++q < 8) { // lower right if (board[q][p] == Q) return false; } p = x; q = y; while (--p >= 0 && ++q < 8) { // lower left if (board[q][p] == Q) return false; } return true; } bool solve(int row) { if (row == 8) return true; if (pre_y[row] && solve(row + 1)) { return true; } else { for (int i = 0; i < 8; ++i) { if (!pre_x[i] && check(i, row)) { board[row][i] = Q; pre_x[i] = true; if (solve(row + 1)) { return true; } else { board[row][i] = _; pre_x[i] = false; } } } } return false; } int main(void) { scanf("%d", &n); for (int i = 0, x, y; i < n; ++i) { scanf("%d %d", &y, &x); board[y][x] = Q; pre_y[y] = pre_x[x] = true; } solve(0); for (int i = 0; i < 8; ++i) { printf("%s\n", board[i]); } return 0; }
1
// ALDS1_12_A.cpp // Graph II - Minimum Spanning Tree #include <iostream> using namespace std; const int maxN = 100; const int INF = 2001; int w[maxN][maxN]; int color[maxN]; int n; static const int WHITE = 0; static const int BLACK = 1; int prim(int x) { int res = 0; int cnt = 1; color[x] = BLACK; while (cnt++ != n) { int minCost = INF; int pos = 0; // ??°?????°????????? for (int i = 0; i < n; i++) { if (color[i] != BLACK) continue; for (int j = 0; j < n; j++) { if (color[j] != WHITE) continue; if (w[i][j] < minCost) { pos = j; minCost = w[i][j]; } } } res += minCost; color[pos] = BLACK; } return res; } int main() { cin >> n; int num; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> num; if (num == -1) w[i][j] = INF; else w[i][j] = num; } } cout << prim(0) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using ll = long long; using vi = vector<int>; using vvi = vector<vi>; int main(){ int n; cin >> n; vi p(n); int mn = 100000000, cnt = 0; rep(i,n){ cin >> p[i]; if(mn > p[i]){ mn = p[i]; cnt++; } } cout << cnt << endl; }
0
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define fi first #define se second #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);i>=(b);i--) #define RREP(i, n) RFOR(i, n, 0) #define MFOR(i, m) for(auto i=(m).begin();i!=(m).end();i++) #define ALL(a) (a).begin(), (a).end() #define SZ(x) ((int)(x).size()) typedef long long int ll; typedef pair<int, int> P; typedef pair<ll, ll> Pll; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<ll> vll; typedef vector<vll> vvll; const double eps = 1e-10; const int MOD = 1000000007; const int INF = 1000000000; const ll LINF = 1 << 30; class UF { vector<int> par; // 親 vector<int> rank; // 深さ public: void init(int n) { // n個で初期化 for(int i=0;i<n;i++) { par.push_back(i); rank.push_back(0); } } UF(int n) { for(int i=0;i<n;i++) { par.push_back(i); rank.push_back(0); } } int find(int x) { // 木の中のxの深さを返す if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { // xとyを統合する x = find(x); y = find(y); if (x == y) return; if(rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { // xとyが等しいかどうか判定する return find(x) == find(y); } }; struct Edge { int from, to, cost; }; bool operator<(const Edge& e1, const Edge& e2) { return e1.cost > e2.cost; } struct Point { int pos, x, y; }; bool operator<(const Point& p1, const Point& p2) { return p1.x < p2.x; } bool operator>(const Point& p1, const Point& p2) { return p1.y < p2.y; } template<typename T> void printv(vector<T> const& s) { REP(i, SZ(s)) { cout << s[i] << " "; } cout << endl; } int main () { cin.tie(0); cout << setprecision(10); int n; cin >> n; vector<Point> p(n); REP(i, n) { p[i].pos = i; cin >> p[i].x >> p[i].y; } vector<vector<Edge>> g(n); sort(ALL(p)); REP(i, n-1) { Edge e; int cost = min(abs(p[i].x - p[i+1].x), abs(p[i].y - p[i+1].y)); e.from = p[i].pos; e.to = p[i+1].pos; e.cost = cost; g[e.from].pb(e); swap(e.from, e.to); g[e.from].pb(e); } sort(ALL(p), greater<Point>()); REP(i, n-1) { Edge e; int cost = min(abs(p[i].x - p[i+1].x), abs(p[i].y - p[i+1].y)); e.from = p[i].pos; e.to = p[i+1].pos; e.cost = cost; g[e.from].pb(e); swap(e.from, e.to); g[e.from].pb(e); } UF uf(n); ll ans = 0; int cnt = 0; priority_queue<Edge> edges; REP(i, SZ(g[0])) { edges.push(g[0][i]); } while(cnt < n-1) { Edge next = edges.top(); edges.pop(); if(!uf.same(next.from, next.to)) { uf.unite(next.from, next.to); ans += next.cost; cnt++; REP(i, SZ(g[next.to])) { edges.push(g[next.to][i]); } } } cout << ans << endl; }
#include <iostream> #include <fstream> #include <set> #include <map> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <functional> #include <algorithm> #include <climits> #include <cmath> #include <iomanip> using namespace std; #define ll long long int #define rep(i,n) for( int i = 0; i < n; i++ ) #define rrep(i,n) for( int i = n; i >= 0; i-- ) #define REP(i,s,t) for( int i = s; i <= t; i++ ) #define RREP(i,s,t) for( int i = s; i >= t; i-- ) #define dump(x) cerr << #x << " = " << (x) << endl; #define INF 2000000000 #define mod 1000000007 #define INF2 1000000000000000000 int v, e; const int MAX_N = 100010; // 隣接リスト vector<int> g[MAX_N]; bool used[MAX_N]; // トポロジカルソートされた数列 vector<int> ans; bool head[MAX_N]; void dfs(int u) { if(used[u]) return; used[u] = true; for(auto& i: g[u]) dfs(i); // 帰りがけ順で追加 ans.push_back(u); } void tsort() { for(int i=0; i<v; ++i) dfs(i); reverse(ans.begin(), ans.end()); } signed main(void) { cin.tie(0); ios::sync_with_stdio(false); cin >> v >> e; e += v - 1; rep(i, v) head[i] = true; for(int i=0; i<e; ++i) { int s, t; cin >> s >> t; s--; t--; g[s].push_back(t); } tsort(); //for(int i: ans) cout << i + 1 << endl; vector<pair<int, int>> p; rep(i, v) p.push_back({i, ans[i]}); rep(i, v) ans[i] = -1; sort(p.begin(), p.end()); for(auto& e: p) { int u = e.second; //cerr << "*" << e.first << " " << e.second << endl; for(auto to: g[u]) { ans[to] = u; } } for(int i: ans) cout << i + 1 << endl; return 0; }
0
#include <bits/stdc++.h> #include <vector> using namespace std; long long n, d, hasil; int main(){ while(cin >> d){ hasil = 0; for(int i = d; i <= 600-d; i += d){ hasil += d * pow(i, 2); } cout << hasil << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll,ll> P; #define rep(i,m,n) for(int i=m;i<n;i++) ll mod=1e9+7; int main(){ int n; cin>>n; int a[n+2]; a[0]=0; a[n+1]=0; rep(i,0,n) cin>>a[i+1]; int sum[n+2]; sum[0]=0; rep(i,0,n+1){ sum[i+1]=sum[i]+abs(a[i+1]-a[i]); } int sum2[n+2]; sum2[0]=0; rep(i,0,n+1){ sum2[i+1]=sum2[i]+abs(a[n+1-i]-a[n-i]); } rep(i,1,n+1){ cout<<sum[i-1]+sum2[n-i]+abs(a[i-1]-a[i+1])<<endl; } }
0
#include <bits/stdc++.h> using namespace std; using int64 = long long; #define int int64 #define rep(i, n) for(int i=0; i<n; i++) #define FOR(i, a, b) for(int i=a; i<b; i++) #define SORT(x) sort(x.begin(), x.end()) #define GSORT(x) sort(x.begin(), x.end(), greater<int>()) #define mk make_pair #define fi first #define se second #define pb push_back #define ALL(x) x.begin(), x.end() #define V(T) vector<T> typedef pair<int, int> P; typedef pair<P, P> PP; typedef vector<int> vi; typedef vector<vi> vvi; int max(int a, int b) {if(b>a) return b; else return a;} int min(int a, int b) {if(b<a) return b; else return a;} vvi con(100050); V(bool) visited(100050, false); vi bw(100050, 0); bool exr = false; void dfs(int now, int cost, int past) { bw[now] = cost%2; rep(i, con[now].size()) { if(!visited[con[now][i]]) { visited[con[now][i]] = true; dfs(con[now][i], cost+1, now); } else if(con[now][i] != past) { if(bw[con[now][i]]==bw[now]) exr = true; } } } signed main() { int N, M; cin >> N >> M; rep(i, M) { int a, b; cin >> a >> b; a--; b--; con[a].pb(b); con[b].pb(a); } dfs(0, 0, -1); if(exr) cout << N*(N-1)/2 - M << endl; else { int b=0, w=0; rep(i, N) { if(bw[i]==0) b++; else w++; } cout << b*w-M << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<int>> graph; void DFS(int v, int parent_v, vector<int> &depth, vector<int> &parent){ for(int child_v : graph[v]){ if(child_v == parent_v) continue; depth[child_v] = depth[v] + 1; parent[child_v] = v; DFS(child_v, v, depth, parent); } } int main(){ int n, u, v; cin >> n >> u >> v; u -= 1; v -= 1; graph.resize(n); for(int i=0; i<n-1; i++){ int a, b; cin >> a >> b; a -= 1; b -= 1; graph[a].push_back(b); graph[b].push_back(a); } vector<int> depth(n), parent(n); DFS(v, -1, depth, parent); int init = depth[u]; int start = u; for(int i=0; i<(init-1)/2; i++){ start = parent[start]; } int dist = depth[start] - (init-1)/2; vector<int> depth2(n), parent2(n); DFS(start, parent[start], depth2, parent2); sort(depth2.begin(),depth2.end()); reverse(depth2.begin(),depth2.end()); cout << (init-1)/2 + (depth2[0] + dist - 1) << endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; #define int long long int hmt() {int x=0;int c=getchar(),n=0;for(;!isdigit(c);c=getchar()) n=(c=='-');for(;isdigit(c);c=getchar()) x=x*10+c-'0';if(n) x=-x;return x;} #define in hmt() #define ins ({string x;char c=getchar();for(;c==' '||c=='\n';c=getchar());for(;c!=' '&&c!='\n';c=getchar()) x+=c;x;}) #define forinc(i,a,b) for(int i=a,_b=b;i<=_b;++i) #define fordec(i,a,b) for(int i=a;i>=b;--i) #define forb(i,BS) for(int i=BS._Find_first();i< BS.size();i = BS._Find_next(i)) #define forv(a,b) for(auto &a:b) #define pb push_back #define pii pair<int,int> #define fi first #define se second #define all(a) a.begin(),a.end() #define reset(f,x) memset(f,x,sizeof(f)) #define bit(x,i) ((x>>(i-1))&1) #define onbit(x,i) (x|(1<<(i-1))) #define offbit(x,i) (x&~(1<<(i-1))) const int N=200010; int n,Q,A,B,a[N],oo,s[N],f[N],ans; struct bit { int t[N]; void rs() { reset(t,127);oo=t[0]; } void upd(int i,int x) { for(;i<=n;i+=i&-i) t[i]=min(t[i],x); } int get(int i) { int g=oo; for(;i;i-=i&-i) g=min(g,t[i]); return g; } }T[2]; void add(int i,int x) { T[0].upd(i,x-i); T[1].upd(n-i+1,i+x); } main() { //freopen("C.inp","r",stdin); n=in,Q=in,A=in,B=in; forinc(i,1,Q) a[i]=in; forinc(i,2,Q) s[i]=s[i-1]+abs(a[i]-a[i-1]); T[0].rs();T[1].rs(); add(B,abs(A-a[1])); add(A,abs(B-a[1])); ans=s[Q]+min(abs(A-a[1]),abs(B-a[1])); forinc(i,2,Q) { f[i]=min(T[0].get(a[i])+a[i],T[1].get(n-a[i]+1)-a[i])+s[i-1]; add(a[i-1],f[i]-s[i]); ans=min(ans,f[i]+s[Q]-s[i]); } cout<<ans<<"\n"; }
#include<bits/stdc++.h> #define maxn 200005 #define LL long long using namespace std; const int mod = 1e9+7; int n,Q,pre,now; struct Seg{ LL mn[maxn<<2],tag[maxn<<2]; int flg; Seg(){memset(mn,0x3f,sizeof mn);} inline void add(int i,LL v){mn[i]+=v,tag[i]+=v;} inline void down(int i){if(tag[i]) add(i<<1,tag[i]),add(i<<1|1,tag[i]),tag[i]=0;} void ins(int i,int l,int r,int x,LL v){ if(l==r) {mn[i]=min(mn[i],v+l*flg);return;} int mid=(l+r)>>1; down(i); x<=mid?ins(i<<1,l,mid,x,v):ins(i<<1|1,mid+1,r,x,v); mn[i]=min(mn[i<<1],mn[i<<1|1]); } LL qry(int i,int l,int r,int x,int y){ if(x<=l&&r<=y) return mn[i]; int mid=(l+r)>>1; LL ret=mn[0]; down(i); if(x<=mid) ret=min(ret,qry(i<<1,l,mid,x,y)); if(y>mid) ret=min(ret,qry(i<<1|1,mid+1,r,x,y)); return ret; } LL whole(int i,int l,int r){ if(l==r) return mn[i]+l; int mid=(l+r)>>1; down(i); return min(whole(i<<1,l,mid),whole(i<<1|1,mid+1,r)); } }T1,T2; int main() { scanf("%d%d%d%d",&n,&Q,&pre,&now),T1.flg=-1,T2.flg=1; T1.ins(1,1,n,now,0),T2.ins(1,1,n,now,0); for(int i=1;i<=Q;i++,pre=now){ scanf("%d",&now); if(now==pre) continue; LL tmp=min(T1.qry(1,1,n,1,now)+now,T2.qry(1,1,n,now,n)-now); T1.add(1,abs(now-pre)),T2.add(1,abs(now-pre)); T1.ins(1,1,n,pre,tmp),T2.ins(1,1,n,pre,tmp); } printf("%lld\n",T1.whole(1,1,n)); }
1
#include <cstdio> using namespace std; int main() { int n; while (scanf("%d", &n), n != 0){ int ice[10] = {0}; int ice_n; for (int i = 0; i < n; i++){ scanf("%d", &ice_n); ice[ice_n]++; } for (int i = 0; i < 10; i++){ if (ice[i] == 0){ puts("-"); } else { while (ice[i]-- != 0){ printf("*"); } puts(""); } } } }
#include<iostream> #include<stdio.h> #include<math.h> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ int l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, v1, v2, sum, i; double ter; vector<int> s; while (scanf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d", &l1, &l2, &l3, &l4, &l5, &l6, &l7, &l8, &l9, &l10, &v1, &v2) != EOF) { s.clear(); s.push_back(l1); s.push_back(l2); s.push_back(l3); s.push_back(l4); s.push_back(l5); s.push_back(l6); s.push_back(l7); s.push_back(l8); s.push_back(l9); s.push_back(l10); sum = l1 + l2 + l3 + l4 + l5 + l6 + l7 + l8 + l9 + l10; ter = (double(sum*v1)) / (v1 + v2); sum = 0; for (i = 0; i < 10; i++) { sum += s[i]; if (ter <= (double)sum) { cout << i + 1 << endl; break; } } } return 0; }
0
#include <stdio.h> #include <limits.h> #include <algorithm> using namespace std; long long int sum = 0; void merge(int A[],int left,int mid, int right){ int a1 = mid - left; int a2 = right - mid; int l[a1+1],r[a2+1]; for(int i = 0; i < a1; i++) l[i] = A[left + i]; for(int i = 0; i < a2; i++) r[i] = A[mid + i]; l[a1] = r[a2] = INT_MAX; int i = 0, j = 0; for(int k = left;k < right; k++){ if(l[i] < r[j]){ A[k] = l[i++]; }else{ A[k] = r[j++]; sum += a1 - i; } } } void mergeSort(int A[],int left,int right){ if(left+1 < right){ int mid = (left+right)/2; mergeSort(A,left,mid); mergeSort(A,mid,right); merge(A,left,mid,right); } } int main(){ int n; scanf("%d",&n); int A[n]; for(int i = 0; i < n; i++) scanf("%d",&A[i]); mergeSort(A,0,n); printf("%lld\n",sum); }
#include <bits/stdc++.h> typedef long long LL; typedef unsigned long long ULL; #define int long long #define debug printf("fuck %d\n", __LINE__); inline LL read() { LL res = 0, bo = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') bo = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { res = (res << 1) + (res << 3) + ch - '0'; ch = getchar(); } return bo * res; } template <typename T> void read(T &x) { x = read(); } template <typename T, typename... Argv> void read(T &a, Argv &... argv) { read(a); read(argv...); } const int N = 1e5 + 10; int x[N], p[N], s, n; LL Calc(int l, int r, int t) { if (s < x[l]) return x[r] - s; if (s > x[r]) return s - x[l]; if (p[l] >= p[r]) return p[l] += p[r], Calc(l, r - 1, l) + (t == r ? x[r] - x[l] : 0); else return p[r] += p[l], Calc(l + 1, r, r) + (t == l ? x[r] - x[l] : 0); } signed main() { read(n, s); for (int i = 1; i <= n; ++ i) read(x[i], p[i]); std::cout << Calc(1, n, p[n] > p[1] ? 1 : n) << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; template< typename T > struct edge { int src, to; T cost; edge(int to, T cost) : src(-1), to(to), cost(cost) {} edge(int src, int to, T cost) : src(src), to(to), cost(cost) {} edge &operator=(const int &x) { to = x; return *this; } operator int() const { return to; } }; template< typename T > using Edges = vector< edge< T > >; template< typename T > using WeightedGraph = vector< Edges< T > >; using UnWeightedGraph = vector< vector< int > >; template< typename T > using Matrix = vector< vector< T > >; template< typename G > struct LowLink { const G &g; vector< int > used, ord, low; vector< int > articulation; vector< pair< int, int > > bridge; LowLink(const G &g) : g(g), used(g.size()), ord(g.size()), low(g.size()) {} int build(int idx = 0, int k = 0, int par = -1) { used[idx] = true; ord[idx] = k++; low[idx] = ord[idx]; bool is_articulation = false; int cnt = 0, ordmax = k; for(auto &to : g[idx]) { if(!used[to]) { ++cnt; ordmax = build(to, ordmax, idx); low[idx] = min(low[idx], low[to]); is_articulation |= ~par && low[to] >= ord[idx]; if(ord[idx] < low[to]) bridge.emplace_back(idx, to); } else if(to != par) { low[idx] = min(low[idx], ord[to]); } } is_articulation |= par == -1 && cnt > 1; if(is_articulation) articulation.push_back(idx); return ordmax; } }; int main() { int V, E; scanf("%d %d", &V, &E); UnWeightedGraph g(V); for(int i = 0; i < E; i++) { int x, y; scanf("%d %d", &x, &y); g[x].push_back(y); g[y].push_back(x); } LowLink< UnWeightedGraph > lowlink(g); lowlink.build(); sort(lowlink.articulation.begin(), lowlink.articulation.end()); for(auto &v : lowlink.articulation) printf("%d\n", v); }
#include <iostream> #include <vector> using namespace std; int dfs(auto& nodes, int current_index, int parent_index, int& time) { int children = 0; auto& current_node = nodes[current_index]; current_node.time = time; current_node.low = time; for (auto adjacent_index : current_node.adjacent) { if (adjacent_index == parent_index) { continue; } auto& adjacent_node = nodes[adjacent_index]; if (adjacent_node.time) { current_node.low = min(adjacent_node.time, current_node.low); } else { ++children; dfs(nodes, adjacent_index, current_index, ++time); if (current_node.time <= adjacent_node.low) { current_node.is_articulation_point = true; } current_node.low = min(adjacent_node.low, current_node.low); } } return children; } int main() { int n, m; cin >> n >> m; if (n == 0 || m == 0) { return 0; } struct node { int time{0}; int low{0}; bool is_articulation_point{false}; vector<int> adjacent; }; vector<node> nodes(n); for (int i = 0; i < m; ++i) { int from, to; cin >> from >> to; nodes[from].adjacent.push_back(to); nodes[to].adjacent.push_back(from); } int time = 0; nodes[0].is_articulation_point = dfs(nodes, 0, -1, ++time) > 1; for (int i = 0; i < nodes.size(); ++i) { if (nodes[i].is_articulation_point) { cout << i << endl; } } return 0; }
1
#include <iostream> using namespace std; int F[102][102]; void setblock(int c, int d, int x, int y){ if(d==0){ for(int i=0;i<4;i++){ F[y][x+i]=c; F[y+1][x+i]=c; } }else{ for(int i=0;i<4;i++){ F[y+i][x]=c; F[y+i][x+1]=c; } } } void DFS(int Y, int X, int c){ if(F[Y][X]!=c) return; F[Y][X] = 6; DFS(Y-1, X ,c); DFS(Y , X+1,c); DFS(Y+1, X ,c); DFS(Y , X-1,c); } int main(){ int w,h,xs,ys,xg,yg,n; while(true){ cin >> w >> h; for(int i=0;i<h;i++){ for(int k=0;k<w;k++){ F[i][k]=-1; } } if(w==0&&h==0){ break; } cin >> xs >> ys; cin >> xg >> yg; cin >> n; for(int i=0; i<n; i++){ int c,d,x,y; cin >> c >> d >> x >> y; setblock(c,d,x,y); } DFS(ys,xs,F[ys][xs]); if(F[yg][xg]==6){ cout << "OK" << endl; }else{ cout << "NG" << endl; } } return 0; }
#include<iostream> #include<algorithm> #include<cstdio> #include<cmath> #include<math.h> #include<string> #include<string.h> #include<stack> #include<queue> #include<vector> #include<utility> #include<set> #include<map> #include<stdlib.h> #include<iomanip> using namespace std; #define ll long long #define ld long double #define EPS 0.0000000001 #define INF 1e9 #define MOD 1000000007 #define rep(i,n) for(i=0;i<n;i++) #define loop(i,a,n) for(i=a;i<n;i++) #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) typedef vector<int> vi; typedef pair<int,int> pii; int main(void) { int i,j; int n,q; while(1){ cin>>n>>q; if(n==0 && q==0)break; vi v(105,0); rep(i,n){ int m,a; cin>>m; rep(j,m){ cin>>a; v[a]++; } } int ans=0; for(i=1;i<=100;i++) if(v[i]>v[ans] && v[i]>=q)ans=i; cout<<ans<<endl; } }
0
#include <bits/stdc++.h> using namespace std; #define ll long long int void printVector(vector<int> v) { for (auto x : v) { cout << x << ' '; } cout << '\n'; } void printArray(ll A[], int size) { for (int i = 0; i <= size - 1; i++) cout << A[i] << ' '; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(); int n, q; cin >> n >> q; string s; cin >> s; vector<int> v; int tot = 0; for (int i = 0; i <= n - 1; i++) { if (i + 1 <= n - 1 && s[i] == 'A' && s[i + 1] == 'C') tot++; v.push_back(tot); } for (int i = n - 1; i >= 1; i--) { //cout << 'h' << '\n'; v[i] = v[i - 1]; } v[0] = 0; //printVector(v); while (q--) { int l, r; cin >> l >> r; l--; r--; cout << v[r] - v[l] << '\n'; } return 0; }
#include<bits/stdc++.h> using namespace std; #define ll long long #define endl "\n" #define si(a) scanf("%d",&a) #define si2(a,b) scanf("%d%d",&a,&b) #define sl(a) scanf("%lld",&a) #define sl2(a,b) scanf("%lld%lld",&a,&b) #define pb push_back #define mk make_pair #define loop(n) for(int i=0; i<n; i++) #define FOR(a,b) for(int i=a; i<=b; i++) #define sz size() #define ff first #define ss second #define mem(a,val) memset(a, val, sizeof(a)) #define md 1000000007 #define pi acos(-1.0) ll a[500010], bit[500010], n, m[500010], ans[500010]; pair< pair<ll,ll>, ll>p[500010]; void update(ll idx, ll val) { while(idx<=n) { bit[idx]+=val; idx+= idx & (- idx); } } ll sum(ll idx) { ll tot=0; while(idx>0) { tot+=bit[idx]; idx-= idx & (- idx); } return tot; } int main() { ll t,i,j,k,l,r,mn=0, mx=0; sl2(n,k); for( i=1; i<=n; i++) { sl(a[i]); } for(i=0; i<k; i++) { sl2(l,r); p[i].ff.ff=r; p[i].ff.ss=l; p[i].ss=i; } sort(p, p+k); /*for(i=0; i<k; i++) { cout<<p[i].ff.ss<<" "<<p[i].ff.ff<<" "<<p[i].ss<<endl; }*/ memset(m, -1, sizeof(m)); ll cnt=0; for( i=1; i<=n; i++) { if(m[a[i]]!=-1) { //cout<<i<<endl; update(m[a[i]], -1LL); } m[a[i]]=i; update(i, 1); // cout<<cnt<<" "<<p[i].ff.ff<<endl; while(cnt<k && p[cnt].ff.ff==i) { //cout<<p[i].ss<<" "<<sum(p[i].ff.ff)<<" "<<sum(p[i].ff.ss-1)<<endl; ans[p[cnt].ss] = sum(p[cnt].ff.ff) - sum(p[cnt].ff.ss-1); cnt++; } } for(i=0; i<k; i++) { printf("%lld\n", ans[i]); } }
0
#include <vector> #include <iostream> using namespace std; int main() { int n; cin >> n; int m = 1e9; for (int i = 0; i < n; i++) { int a; cin >> a; m = min(__builtin_ctz(a), m); } cout << m << endl; }
#include <bits/stdc++.h> using namespace std; #define rep(i,n) for (int i=0;i<n;i++) using ll = long long; using pint = pair<int,int>; using vpil = vector<pint>; using vppil = vector<vpil>; int main(){ int n; cin >> n; vppil l(n); rep(i,n){ int a; cin>>a; rep(j,a){ pint xy; cin>>xy.first>>xy.second; xy.first--; l.at(i).push_back(xy); } } //end input //binary search int ans=0; rep(bit,1<<n){ bool flag=true; rep(i,n){ if(bit&(1<<i)){//is bit true //get items from l; for(pint xy:l.at(i)){ if((bit&(1<<xy.first))>>xy.first!=xy.second){ flag=false; i+=n; break;//=>gotonext } } } } //gotonext if(flag){ans=max(__builtin_popcount(bit),ans);} } cout<<ans<<endl; }
0
#include<bits/stdc++.h> using namespace std; #define rep(i,x,y) for(int i=x;i<y;i++) #define print(A,x,n) rep(i,0,n){cout<<(i ? " ":"")<<A[i]x;}cout<<endl; #define pprint(A,y,m,n) rep(j,0,m){print(A[j],y,n);} const long mod=1e9+7; const int size=1e5; const int inf=1e9; int main(){ int H,W;cin>>H>>W; int A[H][W] = {}; int a; rep(i,0,H)rep(j,0,W){ cin>>a; A[i][j] += a; } rep(i,0,H)rep(j,0,W){ cin>>a; A[i][j] -= a; } bool dp[H+1][W+1][12801] = {}; dp[0][1][0] = 1; rep(i,1,H+1)rep(j,1,W+1){ int t = abs(A[i-1][j-1]); rep(k,0,12801){ if(k + t <= 12800) dp[i][j][k+t] |= dp[i-1][j][k] | dp[i][j-1][k]; dp[i][j][abs(k - t)] |= dp[i-1][j][k] | dp[i][j-1][k]; } } rep(i,0,12801){ if(dp[H][W][i]){ cout<<i<<endl; break; } } }
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i=0; i<n; i++) #define pb push_back #define int long long const int ofs = 12800; int H, W; int A[82][82], B[82][82]; bool dp[82][82][25610]; signed main() { cin.tie(0); ios::sync_with_stdio(false); cin >> H >> W; rep(i, H) rep(j, W) cin >> A[i][j]; rep(i, H) rep(j, W) cin >> B[i][j]; dp[0][0][A[0][0]-B[0][0]+ofs] = true; dp[0][0][B[0][0]-A[0][0]+ofs] = true; rep(i, H) rep(j, W) { if (i>0) rep(k, 25610) { if (!dp[i-1][j][k]) continue; int nk = k+A[i][j]-B[i][j]; if (0<=nk && nk<25610) dp[i][j][nk] = true; nk = k+B[i][j]-A[i][j]; if (0<=nk && nk<25610) dp[i][j][nk] = true; } if (j>0) rep(k, 25610) { if (!dp[i][j-1][k]) continue; int nk = k+A[i][j]-B[i][j]; if (0<=nk && nk<25610) dp[i][j][nk] = true; nk = k+B[i][j]-A[i][j]; if (0<=nk && nk<25610) dp[i][j][nk] = true; } } int ans = 1000000; rep(i, 25610) if (dp[H-1][W-1][i]) ans = min(ans, abs(i-ofs)); cout << ans << endl; }
1
#include <iostream> #include <string> #include <cstdlib> #include <cmath> #include <vector> #include <map> #include <set> #include <algorithm> #include <queue> #include <stack> #include <functional> #include <bitset> #include <assert.h> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<char> vc; typedef vector<string> vs; typedef vector<bool> vb; typedef vector<double> vd; typedef pair<ll,ll> P; typedef vector<P> vpl; typedef tuple<ll,ll,ll> tapu; #define rep(i,n) for(ll i=0; i<(n); i++) #define REP(i,a,b) for(int i=(a); i<(b); i++) #define all(x) (x).begin(), (x).end() #define rall(x) (x).rbegin(), (x).rend() const int inf = 1<<30; const ll linf = 1LL<<62; const int MAX = 510000; ll dy[8] = {1,-1,0,0,1,-1,1,-1}; ll dx[8] = {0,0,1,-1,1,-1,-1,1}; const double pi = acos(-1); const double eps = 1e-7; template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){ if(a>b){ a = b; return true; } else return false; } template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){ if(a<b){ a = b; return true; } else return false; } template<typename T> inline void print(T &a){ rep(i,a.size()) cout << a[i] << " "; cout << "\n"; } template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";} template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){ cout << a << " " << b << " " << c << "\n"; } const int mod = 1e9 + 7; //const int mod = 998244353; void solve(){ ll n; cin >> n; vl a(n); rep(i,n) cin >> a[i]; vl basis; string s; cin >> s; ll ans = 0; for(ll i=n-1; i>=0; i--){ for(auto b : basis){ chmin(a[i], a[i]^b); } if(s[i]=='0'){ if(a[i]) basis.push_back(a[i]); }else{ if(a[i]) ans = 1; } } cout << ans << "\n"; } int main(){ ll t; cin >> t; while(t--){ solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef pair<ll, ll> ii; typedef tuple<ll, ll, ll> iii; typedef vector<ll> vi; typedef vector<ii> vii; typedef vector<iii> viii; typedef vector<vi> vvi; typedef vector<vii> vvii; #define REP(i,n) for (ll i = 0; i < n; ++i) #define REPR(i,n) for (ll i = n-1; i >= 0; --i) #define FOR(i,m,n) for (ll i = m; i < n; ++i) #define FORR(i,m,n) for (ll i = n-1; i >= m; --i) #define FORE(x,xs) for (const auto& x : xs) #define FORI(i,v) for (auto i = v.begin(); i != v.end(); i++) #define ALL(v) v.begin(), v.end() #define CHMIN(x,y) x = min(x, y) #define CHMAX(x,y) x = max(x, y) #define YES(b) cout << ((b) ? "YES" : "NO") << endl #define Yes(b) cout << ((b) ? "Yes" : "No") << endl const int MAX = 2e5+10; const int INF = 1e9+10; int N; vii U, D, R, L; vii holRev(vii v) { vii ret; FORE (xy, v) { int x, y; tie(x, y) = xy; ret.push_back(ii(MAX-x, y)); } return ret; } vii verRev(vii v) { vii ret; FORE (xy, v) { int x, y; tie(x, y) = xy; ret.push_back(ii(x, MAX-y)); } return ret; } int sub1(vii r, vii l, bool hol) { int ret = INF; vector<set<int>> u(MAX); FORE (xy, l) { int x, y; tie(x, y) = xy; if (!hol) swap(x, y); u[y].insert(x); } FORE (xy, r) { int x, y; tie(x, y) = xy; if (!hol) swap(x, y); auto it = u[y].upper_bound(x); if (it != u[y].end()) { CHMIN(ret, (*it - x) * 5); } } return ret; } int sub2(vii uu, vii r) { int ret = INF; vector<set<int>> u(2*MAX+10); FORE (xy, uu) { int x, y; tie(x, y) = xy; u[x+y].insert(x); } FORE (xy, r) { int x, y; tie(x, y) = xy; auto it = u[x+y].upper_bound(x); if (it != u[x+y].end()) { CHMIN(ret, (*it - x) * 10); } } return ret; } int solve() { int ret = INF; // 正面衝突 CHMIN(ret, sub1(R, L, true)); CHMIN(ret, sub1(U, D, false)); // 直行衝突 CHMIN(ret, sub2(U, R)); CHMIN(ret, sub2(holRev(U), holRev(L))); CHMIN(ret, sub2(verRev(D), verRev(R))); CHMIN(ret, sub2(holRev(verRev(D)), holRev(verRev(L)))); return ret; } int main() { cout << fixed << setprecision(15); cin >> N; REP (i, N) { int x, y, c; char u; cin >> x >> y >> u; if (u == 'U') U.push_back(ii(x, y)); else if (u == 'R') R.push_back(ii(x, y)); else if (u == 'D') D.push_back(ii(x, y)); else L.push_back(ii(x, y)); } int ans = solve(); if (ans == INF) cout << "SAFE" << endl; else cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main(){ int l,r; cin >> l >> r; const int num = 2019; int comp = INT_MAX; long long ans = 0; for(long long i=l; i<=r; i++){ for(long long j=l; j<i; j++){ ans = ((i%num) * (j%num))%num; if(ans < comp){ comp = ans; } else if(ans == 0){ cout << ans << endl; return 0; } } } cout << comp << endl; }
#include <iostream> #include <iomanip> #include <string> #include <sstream> #include <stdio.h> #include <string.h> #include <vector> #include <algorithm> #include <utility> #include <tuple> #include <cstdint> #include <cstdio> #include <cmath> #include <map> #include <queue> #include <set> #include <stack> #include <deque> #include <unordered_map> #include <unordered_set> #include <bitset> #include <cctype> using namespace std; #define MOD 2019 int main(){ long long int L = 0,R = 0; cin >> L >> R; if(L + MOD <= R){ cout << 0 << endl; }else{ L = L % MOD; R = R % MOD; int answer = 100000000; for(int i = L;i < R;i++){ for(int j = i + 1;j <= R;j++){ answer = min(answer,((i*j)%2019)); } } cout << answer << endl; } }
1
#include<stdio.h> int main() { char S[101]; char T[101]; scanf("%s %s", &S, &T); printf("%s%s",T,S); return 0; }
#include <bits/stdc++.h> #define ll long long using namespace std; #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = 1; i <= n; i++) ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; } ll lcm(ll a, ll b) { return (a * b) / gcd(a,b); } using P = pair <int, int>; 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; } ll MOD = 1000000007; ll INF =1<<30; int main(){ int n,h,w; cin >> n >> h >> w; cout << (n-h+1)*(n-w+1) <<endl; }
0
#define _USE_MATH_DEFINES #include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <set> #include <map> #include <queue> #include <stack> #include <cmath> //#include <atcoder/all> using namespace std; //using namespace atcoder; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef tuple<ll, ll, ll> tl3; //typedef modint998244353 mint; const int BIG_NUM = 1e9; const ll INF = 1000000000000000000; const ll MOD = 1e9 + 7; //const ll MOD = 998244353; const ll MAX = 1e9 + 5; int main() { int m; cin >> m; vector<int> d(m); vector<ll> c(m); for (int i = 0; i < m; i++) { cin >> d[i] >> c[i]; } vector<vector<pll>> dl(10); for (int i = 0; i < 10; i++) { dl[i].emplace_back(make_pair(i, 0)); } for (int i = 0; i < 10; i++) { for (int j = 0; j < 60; j++) { //cout << j << endl; ll num = dl[i][j].first * 2; ll cnt = dl[i][j].second * 2 + 1; if (num >= 10) { cnt++; num = num % 10 + 1; } dl[i].emplace_back(make_pair(num, cnt)); } //cout << i << endl; } /* for (int i = 0; i < 10; i++) { for (int j = 0; j < 60; j++) { cout << dl[i][j].first << " ";// << dl[i][j].second << endl; } cout << endl; } */ ll cnt = 0; vector<int> a(m); for (int i = 0; i < m; i++) { int num = 0; cnt--; for (int b = 0; b < 60; b++) { if (((c[i] >> b) & 1) == 1) { num += dl[d[i]][b].first; cnt += dl[d[i]][b].second; cnt++; //cout << i << " " << dl[d[i]][b].second << " " << dl[d[i]][b].first << endl; } if (num >= 10) { num -= 9; cnt++; } } a[i] = num; } //cout << cnt << endl; int num = a[0]; for (int i = 1; i < m; i++) { //cout << a[i] << endl; num += a[i]; cnt++; if (num >= 10) { num -= 9; cnt++; } } cout << cnt << endl; }
#include <bits/stdc++.h> #define rep(i, n) for (int i = 0; i < n; i++) #define rrep(i, n) for (int i = n - 1; i >= 0; i--) using namespace std; #define INF ((1<<30)-1) #define LINF (1LL<<60) #define EPS (1e-10) typedef long long ll; typedef pair<ll, ll> P; const int MOD = 1000000007; const int MOD2 = 998244353; ll c[200010], d[200010]; int main(){ int m; cin >> m; rep(i, m) cin >> d[i] >> c[i]; ll tot = 0, digit = 0; rep(i, m) { tot += d[i] * c[i]; digit += c[i]; } cout << digit - 1 + (tot - 1) / 9 << endl; return 0; }
1
#include<stdio.h> int main() { int a[11][11],n,i,j; while(scanf("%d",&n),n) { for(i=0;i<n;++i) { a[i][n] = 0; for(j=0;j<n;++j) { scanf("%d", &a[i][j]); a[i][n] += a[i][j]; } } for(j = 0; j <= n; ++j) { a[n][j] = 0; for(i = 0; i < n; ++i) a[n][j] += a[i][j]; } for(i = 0; i <= n; ++i) { for(j = 0; j <= n; ++j) { printf("%5d",a[i][j]); } putchar('\n'); } } return 0; }
#include <stdio.h> #include <string.h> #include <string> #include <iostream> #include <vector> #include <sstream> #include <memory> #include <iomanip> int main(int argc, char* argv[]) { while (true) { std::string line; std::stringstream buffer; getline(std::cin, line); buffer << line; size_t length = 0; buffer >> length; if (length == 0) break; int *total = new int[length + 1]; size_t width = length + 1; memset(total, 0, sizeof(int) * width); for (size_t i = 0; i < length; ++i) { getline(std::cin, line); std::istringstream s(line); std::string element; int sum = 0; for (size_t n = 0; n < length; ++n) { getline(s, element, ' '); std::stringstream tmp; int val = 0; tmp << element; tmp >> val; sum += val; total[n] += val; std::cout << std::setw(5) << val; } std::cout << std::setw(5) << sum << std::endl; total[length] += sum; } for (size_t i = 0; i < width; ++i) { std::cout << std::setw(5) << total[i]; } std::cout << std::endl; delete[] total; } return 0; }
1
#include <bits/stdc++.h> #define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout) #define Grt ch = getchar() #define DeBug(x) std::cout << #x << '=' << x << std::endl typedef long long ll; const int MaxN = 45, MaxS = 1 << 18, mod = 1e9 + 7; namespace IO { char buf[1<<15], *fs, *ft; inline char getc() { return ft == fs && (ft = (fs = buf) + fread(buf, 1, 1<<15, stdin), ft == fs) ? 0 : *fs++; } template <typename T> inline void read(T &x) { x = 0; T f = 1, Grt; while (!isdigit(ch) && ch ^ '-') Grt; if (ch == '-') f = -1, Grt; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), Grt; x *= f; } template <typename T, typename... Args> inline void read(T &x, Args &...args) { read(x); read(args...); } char Out[1<<24], *fe = Out; inline void flush() { fwrite(Out, 1, fe - Out, stdout); fe = Out; } template <typename T> inline void write(T x, char str) { if (!x) *fe++ = 48; if (x < 0) *fe++ = '-', x = -x; T num = 0, ch[20]; while (x) ch[++ num] = x % 10 + 48, x /= 10; while (num) *fe++ = ch[num --]; *fe++ = str; } } using IO::read; using IO::write; template <typename T> inline bool chkMin(T &a, const T &b) { return a > b ? (a = b, true) : false; } template <typename T> inline bool chkMax(T &a, const T &b) { return a < b ? (a = b, true) : false; } template <typename T> inline T min(T a, T b) { return a < b ? a : b; } template <typename T> inline T max(T a, T b) { return a > b ? a : b; } ll f[MaxN][MaxS], ans = 1; int main() { int n, x, y, z; read(n, x, y, z); int state = (1 << (x + y + z - 1)) | (1 << (y + z - 1)) | (1 << (z - 1)); int sum = (1 << (x + y + z)) - 1; f[0][0] = 1; for (int i = 1; i <= n; ++ i) { (ans *= 10) %= mod; for (int s = 0; s <= sum; ++ s) if (f[i - 1][s]) for (int c = 1, now; c <= 10; ++ c) { now = (s << c) | (1 << (c - 1)), now &= sum; if ((now & state) != state) f[i][now] = (f[i][now] + f[i - 1][s]) % mod; } } for (int s = 0; s <= sum; ++ s) ans = (ans - f[n][s] + mod) % mod; write(ans, '\n'); IO::flush(); return 0; }
#include <cstdio> #include <vector> using namespace std; #define MAX_H 20 #define MAX_W 20 #define INF (1<<28) int w, h; int F[MAX_H][MAX_W]; int G[MAX_H][MAX_W]; int gy, gx; int y, x; int py, px; void debug(); const int dy[] = {1, 0, -1, 0}; const int dx[] = {0, 1, 0, -1}; void move(int d) { //printf(" %d\n", d); while (F[y][x] == 0) { x += dx[d]; y += dy[d]; } if (F[y][x] == 3) return; F[y][x] = 0; y -= dy[d]; x -= dx[d]; } bool can_move(int d) { if (F[y+dy[d]][x+dx[d]] == 1) return false; int _y = y; int _x = x; while (F[_y][_x] == 0) { _y += dy[d]; _x += dx[d]; if (_y < 0 || h <= _y || _x < 0 || w <= _x) return false; } return true; } void debug() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { printf("%d ", F[i][j]); } printf("\n"); } } int solve(int cur) { if (y == gy && x == gx) return 0; if (cur >= 10) return INF; int min_cost = INF; for (int i = 0; i < 4; i++) { if (can_move(i)) { int backup[h][w]; int py = y, px = x; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) backup[j][k] = F[j][k]; move(i); min_cost = min(min_cost, solve(cur+1)+1); y = py, x = px; for (int j = 0; j < h; j++) for (int k = 0; k < w; k++) F[j][k] = backup[j][k]; } } return min_cost; } int main() { while (scanf("%d %d", &w, &h), w || h) { int sy, sx; for (int i = 0; i < h; i++) for (int j = 0; j < w; j++) { scanf("%d", &F[i][j]); if (F[i][j] == 2) { y = i, x = j; F[i][j] = 0; } if (F[i][j] == 3) gy = i, gx = j; } int ans = solve(0); printf("%d\n", (ans > 10 ? -1 : ans)); } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; int compat[n][n]; for (int q = 0; q < n; q++) { for (int w = 0; w < n; w++) { cin >> compat[q][w]; } } ll dp[1<<n]; for (int q = 0; q < (1<<n); q++) { dp[q] = 0; for (int w = 0; w < n; w++) { if ((q & (1<<w)) == 0) { continue; } for (int e = w+1; e < n; e++) { if ((q & (1<<e)) != 0) { dp[q] += compat[w][e]; } } } } for (int q = 1; q < (1<<n); q++) { for (int w = q; w > 0; w = (w-1) & q) { dp[q] = max(dp[q], dp[w] + dp[w ^ q]); } } cout << dp[(1<<n)-1] << "\n"; return 0; }
#include <iostream> #include <sstream> #include <cstdio> #include <stdlib.h> #include <string> #include <vector> #include <algorithm> #define N 8 using namespace std; int row[N]; bool col[N],dpos[2*N-1],dneg[2*N-1]; void printBoard(){ for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ if(j==row[i])printf("Q"); else printf("."); } printf("\n"); } } void putqueen(int i){ if(i == N){ printBoard(); return; } if (row[i]!=-1){ putqueen(i+1); } else { for(int j=0;j<N;j++){ if(col[j]||dpos[i+j]||dneg[i-j+N-1])continue; row[i]=j; col[j]=dpos[i+j]=dneg[i-j+N-1]= true; putqueen(i+1); row[i]=-1; col[j]=dpos[i+j]=dneg[i-j+N-1]= false; } } } int main(){ int n,x,y; cin>>n; for(int i=0;i<N;i++)row[i]=-1; for(int i=0;i<n;i++){ cin>>x>>y; row[x]=y; col[y]=dpos[x+y]=dneg[x-y+N-1]=true; } putqueen(0); return 0; }
0
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld double #define all(x) x.begin(),x.end() #define sz(x) (int)x.size() #define pb emplace_back #define X first #define Y second const int N = 2e5 + 5; typedef pair<int,int> ii; vector<int> g[N]; int par[N]; int deg[N]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int m; cin >> m; for(int i = 1 ; i < n + m ; ++i) { int x; cin >> x; int y; cin >> y; g[x].pb(y); deg[y]++; } queue<int> qu; vector<int> tp; for(int i = 1 ; i <= n ; ++i) if(!deg[i]) qu.push(i); while (qu.size()) { int u = qu.front(); qu.pop(); tp.pb(u); for(int v : g[u]) { deg[v]--; if(!deg[v]) qu.push(v); } } for(int x : tp) for(int v : g[x]) par[v] = x; for(int i = 1 ; i <= n ; ++i) cout << par[i] << "\n"; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) const int MOD = 1000000007; int main() { int h, w; cin >> h >> w; int a[h][w]; rep(i, h) { rep(j, w) cin >> a[i][j]; } int cnt = 0; vector<tuple<int, int, int, int>> ans; //現在位置を示す値 int i = 0, j = 0; while (1) { if (i % 2 == 0) { if (j == w - 1) { if (i == h - 1) goto end; if (a[i][j] % 2 == 1) { cnt++; ans.emplace_back(i + 1, j + 1, i + 2, j + 1); a[i + 1][j]++; } i++; } else { if (a[i][j] % 2 == 1) { cnt++; ans.emplace_back(i + 1, j + 1, i + 1, j + 2); a[i][j + 1]++; } j++; } } if (i % 2 == 1) { if (j == 0) { if (i == h - 1) goto end; if (a[i][j] % 2 == 1) { cnt++; ans.emplace_back(i + 1, j + 1, i + 2, j + 1); a[i + 1][j]++; } i++; } else { if (a[i][j] % 2 == 1) { cnt++; ans.emplace_back(i + 1, j + 1, i + 1, j); a[i][j - 1]++; } j--; } } } end: cout << cnt << endl; rep(i, cnt) { cout << get<0>(ans[i]) << " " << get<1>(ans[i]) << " " << get<2>(ans[i]) << " " << get<3>(ans[i]) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { string s,d[10]; cin>>s; int i,j,a,b; a=s.size(); int c[a-2]; for(i=0;i<a-2;i++) { for(j=i;j<i+3;j++) { d[i]=d[i]+s[j]; } c[i]=abs(753-stoi(d[i])); } sort(c,c+(a-2)); cout<<c[0]<<endl; }
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int main(void) { char s[10]; int i,a,x=0,y=0,z=0,b=0,c=0,d=1000; scanf("%s",s); a=strlen(s); for(i=0;i<a-2;i++){ x=s[i]-'0'; y=s[i+1]-'0'; z=s[i+2]-'0'; b=(x*100)+(y*10)+z; c=753-b; if(c<0) c*=-1; if(d>c) d=c; } printf("%d\n",d); return 0; }
1
// http://algorithms.blog55.fc2.com/blog-entry-66.html #include <stdio.h> #include <string.h> #define M 100 int N,R[M+1],C[M][M],B[M][M]; void compute(){ int i=0,j,k,cost; memset(C,99,sizeof(C)); for(;i<N;i++)C[i][i]=0; for(j=1;j<N;j++) for(i=0;i<N - j; i++ ) for (k = i; k < i + j; k++ ){ cost = C[i][k] + C[k+1][i+j] + R[i]*R[k+1]*R[i+j+1]; if(cost<C[i][i+j])C[i][i+j]=cost,B[i][i+j]=k; } } void order( int i, int j ){ if(i==j)printf("M%d",i); else{ printf("("); order( i, B[i][j]-1 ); order(B[i][j], j); printf(")"); } } void input(){ scanf("%d",&N); int z,i=0; for(;i<N;i++)scanf("%d%d",R+i,&z); R[i]=z; } void output(){ printf("%d\n",C[0][N-1]); //order(0, N); //puts(""); } main(){ input(); compute(); output(); }
#include <stdio.h> int main(){ int n; scanf("%d",&n); while(n){ int a=0,b=0,aa,bb; for(int i=n;i>0;i--){ scanf("%d %d",&aa,&bb); if(aa>bb){ a+=aa+bb; }else if(aa<bb){ b+=aa+bb; }else{ a+=aa; b+=bb; } } printf("%d %d\n",a,b); scanf("%d",&n); } }
0
#include <bits/stdc++.h> #define rep(a,n) for (ll a = 0; a < (n); ++a) using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef vector<vector<ll> > Graph; 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; } const ll INF = 1e18; int main(){ ll n; cin >> n; vector<ll>a(n); rep(i,n)cin>>a[i]; set<ll>st; map<ll,ll>mp; rep(i,n){ mp[a[i]]++; st.insert(a[i]); } ll ans = 0; for(ll i=40;i>=0;i--){ ll now = pow(2,i); for(auto itr=mp.begin();itr!=mp.end();itr++){ if(itr->second<=0)continue; ll f = itr->first; ll s = itr->second; if(f*2==now){ while(s>1){//同一の数字のペア ans++; s-=2; itr->second-=2; } continue; } ll x = now - f; if(!x>0)continue; if(st.find(x)==st.end())continue; if(mp[x]>0){ ll c = min(s,mp[x]); ans += c; mp[x]-=c; itr->second-=c; } } } cout << ans << endl; return 0; }
#include<bits/stdc++.h> #define int long long using namespace std; const int maxm=2e5+5; const int mod=1e9+7; int a[maxm]; int b[maxm]; int n; signed main(){ cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } vector<int>ans; for(int i=n;i>=1;i--){ int cnt=0; for(int j=i;j<=n;j+=i){ cnt+=b[j]; } if(cnt%2==a[i]){ continue; }else{ b[i]=1; ans.push_back(i); } } cout<<ans.size()<<endl; for(auto i:ans){ cout<<i<<' '; } return 0; } /* */
0
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int i = 0; i < n; i++) typedef long long ll; // Welcome to my source code! struct Node { int key; Node *parent, *left, *right; }; Node *root, *NIL; void insertKey(int key) { Node *y = NIL; Node *x = root; Node *z; z = new Node; z->key = key; z->left = NIL; z->right = NIL; while (x != NIL) { y = x; if (z->key < x->key) { x = x->left; } else { x = x->right; } } z->parent = y; if (y == NIL) { root = z; } else { if (z->key < y->key) { y->left = z; } else { y->right = z; } } } Node* findKey(Node *u, int key) { while (u != NIL && u->key != key) { if (key < u->key) { u = u->left; } else { u = u->right; } } return u; } Node* getMinimum(Node *x) { while (x->left != NIL) x = x->left; return x; } Node* getSuccessor(Node *z) { if (z->right != NIL) return getMinimum(z->right); Node *y = z->parent; while (y != NIL && z == y->right) { z = y; y = y->parent; } return y; } Node* deleteNode(Node *z) { Node *y; Node *x; if (z->left == NIL || z->right == NIL) y = z; else y = getSuccessor(z); if (y->left != NIL) x = y->left; else x = y->right; if (x != NIL) x->parent = y->parent; if (y->parent == NIL) root = x; else if (y == y->parent->left) y->parent->left = x; else y->parent->right = x; if (y != z) z->key = y->key; } void inorder(Node *u) { if (u == NIL) return; inorder(u->left); printf(" %d", u->key); inorder(u->right); } void preorder(Node *u) { if (u == NIL) return; printf(" %d", u->key); preorder(u->left); preorder(u->right); } int main() { int m, k; string com; cin >> m; REP(i,m) { cin >> com; if (com == "print") { inorder(root); cout << '\n'; preorder(root); cout << endl; } else { cin >> k; if (com == "insert") insertKey(k); else if (com == "find") { if (findKey(root, k) != NIL) cout << "yes" << endl; else cout << "no" << endl; } else { deleteNode(findKey(root, k)); } } } }
#include <stdio.h> struct binary_tree{ int n; int l; int r; }; binary_tree bt[1000000]; int now=0,empty[1000000]; void push(int n,int t){ if(now==0){ bt[0].n=n; now++; return; } if(bt[t].n<n){ if(bt[t].r==0){ bt[t].r=empty[now]; bt[bt[t].r].n=n; now++; return; } else push(n,bt[t].r); } if(bt[t].n>n){ if(bt[t].l==0){ bt[t].l=empty[now]; bt[bt[t].l].n=n; now++; return; } else push(n,bt[t].l); } } void dfs(int n){ printf(" %d",bt[n].n); if(bt[n].l!=0){ dfs(bt[n].l); } if(bt[n].r!=0){ dfs(bt[n].r); } } void print_sorted(int n){ if(bt[n].l!=0){ print_sorted(bt[n].l); } printf(" %d",bt[n].n); if(bt[n].r!=0){ print_sorted(bt[n].r); } } bool find(int n,int t){ if(bt[t].n==n)return true; if(bt[t].n<n){ if(bt[t].r!=0)return find(n,bt[t].r); return false; } if(bt[t].l!=0)return find(n,bt[t].l); return false; } void delete_item(int n,int t,int p){ if(bt[t].n==n){ if(bt[t].r!=0&&bt[t].l!=0){ int big=bt[t].r,bp=t; while(bt[big].l!=0){ bp=big; big=bt[big].l; } bt[t].n=bt[big].n; if(bp!=t)bt[bp].l=bt[big].r; else bt[bp].r=bt[big].r; now--; empty[now]=big; bt[big].n=bt[big].l=bt[big].r=0; return; } if(bt[t].r==0){ if(p==-1){ now--; empty[now]=bt[t].l; bt[t]=bt[bt[t].l]; bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0; return; } if(bt[p].n>n)bt[p].l=bt[t].l; else bt[p].r=bt[t].l; now--; empty[now]=t; bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0; return; } if(bt[t].l==0){ if(p==-1){ now--; empty[now]=bt[t].r; bt[t]=bt[bt[t].r]; bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0; return; } if(bt[p].n>n)bt[p].l=bt[t].r; else bt[p].r=bt[t].r; now--; empty[now]=t; bt[empty[now]].n=bt[empty[now]].l=bt[empty[now]].r=0; return; } } else if(bt[t].n>n){ delete_item(n,bt[t].l,t); return; } else delete_item(n,bt[t].r,t); } int main(){ int n,temp; char a[1000]; scanf("%d",&n); for(int i=0;i<n;i++){ empty[i]=i; bt[i].n=bt[i].l=bt[i].r=0; scanf("%s",a); if(a[0]=='i'){ scanf("%d",&temp); push(temp,0); } if(a[0]=='p'){ print_sorted(0); printf("\n"); dfs(0); printf("\n"); } if(a[0]=='f'){ scanf("%d",&temp); if(find(temp,0))printf("yes\n"); else printf("no\n"); } if(a[0]=='d'){ scanf("%d",&temp); delete_item(temp,0,-1); } } }
1
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (n); ++i) using namespace std; using ll = long long; const int mod = 998244353; int main() { int n, d0; cin >> n >> d0; if(d0 != 0) { cout << 0 << endl; return 0; } vector<int> d(100005); d.at(0) = 1; int max_d = 0; for(int i = 1; i < n; ++i) { int di; cin >> di; max_d = max(max_d, di); d.at(di)++; } if(d.at(0) != 1) { cout << 0 << endl; return 0; } ll ans = 1; int i = 1; while(i < n) { if(d.at(i) == 0) break; rep(j, d.at(i)) { ans *= d.at(i-1); ans %= mod; } ++i; } if(i < max_d) cout << 0 << endl; else cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define INF 1LL<<62 #define inf 998244353 ll ch[100010]; // a^n mod を計算する long long modpow(long long a, long long n, long long mod) { long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } int main() { ll n; cin>>n; for(ll i=0;i<n;i++){ ll now; cin>>now; ch[now]++; if(i==0&&now!=0){ cout << 0; return 0; } } if(ch[0]!=1){ cout << 0; return 0; } ll cnt=ch[0]; ll ans=1; for(ll i=1;;i++){ if(ch[i]==0){ cout << 0; return 0; } ans*=modpow(ch[i-1], ch[i], 998244353); ans%=inf; cnt+=ch[i]; if(cnt==n){ break; } } cout <<ans; // your code goes here return 0; }
1
#include<iostream> #include<cstdio> #include<string> using namespace std; string S; int X[1000005][3]; int z=0; int y=0; int main() { cin >> S; if(S[0]=='J'){ X[z][0]++; y=0; } if(S[0]=='O'){ X[z][1]++; y=1; } if(S[0]=='I'){ X[z][2]++; y=2; } for(int i=1;i<S.length();i++){ if(S[i-1]==S[i])X[z][y]++; else{ z++; if(S[i]=='J')y=0; if(S[i]=='O')y=1; if(S[i]=='I')y=2; X[z][y]++; } } int ans=0; for(int i=0;i<=z;i++){ if(X[i][0]<X[i+1][1])continue; if(X[i+1][1]>X[i+2][2])continue; ans=max(ans,X[i+1][1]); } printf("%d\n",ans); return 0; }
#include <bits/stdc++.h> using namespace std; #define mp make_pair #define pb push_back typedef long long ll; ll gcd(ll f , ll s){if(s==0)return f;else return gcd(s,f%s);} int const N = 1000006; ll const M = 998244353; long double const ep = .000000000000000001; int p[N]; queue<int>q; ll Farray[1032][1032] , Sarray[1032][1023] , arr[1002][1003]; int main() { int n;cin >> n; if(n==2){ cout << 4 << " " << 7 << endl << 23 << " " << 10 ; return 0; } for(int i = 2 ; i <= 1000 ; i++){ if(!p[i]){ for(int x = i + i; x <= 100000 ; x+=i)p[x] = 1; } } for(int i = 2 ; i <= 100000 ; i++)if(!p[i])q.push(i); pair<int,int>start = mp(0,0); for(pair<int,int> i = start ; i.first < n ; i.first += 2){ int currP = q.front(); q.pop(); for(pair<int,int> x = i ; x.first < n && x.second < n ; x.first++ , x.second++){ Farray[x.first][x.second] = currP; } } for(pair<int,int> i = mp(0,2) ; i.second < n ; i.second += 2){ int currP = q.front(); q.pop(); for(pair<int,int> x = i ; x.first < n && x.second < n ; x.first++ , x.second++){ Farray[x.first][x.second] = currP; } } for(pair<int,int> i = start ; i.second < n ; i.second += 2){ int currP = q.front(); q.pop(); for(pair<int,int> x = i ; x.first < n && x.second >= 0 ; x.first++ , x.second--){ Sarray[x.first][x.second] = currP; } } for(pair<int,int> i = mp(1+(n%2),n-1) ; i.first < n ; i.first += 2){ int currP = q.front(); q.pop(); for(pair<int,int> x = i ; x.first < n && x.second >= 0 ; x.first++ , x.second--){ Sarray[x.first][x.second] = currP; } } for(int i = 0 ; i < n ; i++) for(int x = 0 ; x < n ; x++) arr[i][x] = Farray[i][x] * Sarray[i][x]; for(int i=0 ; i< n ; i++){ for(int j=0 ; j< n ; j++){ if((i + j)%2== 1){ set<ll>s; if(i != 0){ s.insert(Farray[i-1][j]); s.insert(Sarray[i-1][j]); } if(i != n-1){ s.insert(Farray[i+1][j]); s.insert(Sarray[i+1][j]); } if(j != 0){ s.insert(Farray[i][j-1]); s.insert(Sarray[i][j-1]); } if(j != n-1){ s.insert(Farray[i][j+1]); s.insert(Sarray[i][j+1]); } arr[i][j] = 1; set<ll>::iterator it = s.begin(); for(;it!=s.end() ; it++)arr[i][j]*= *(it); arr[i][j]++; } } } for(int i=0 ; i< n ; i++){ for(int j=0 ; j< n ; j++){ cout << arr[i][j] << " "; } cout << endl; } return 0; }
0
#include<iostream> #include<math.h> #include<vector> #include<array> #include<algorithm> #include<numeric> #include<map> #include<stack> #include<queue> #include<deque> #include<set> #include<cstdio> #include<cstring> #include<string> using namespace std; typedef long long ll; typedef vector<ll> vec; typedef vector<vector<ll>> mat; typedef pair<int,int> P; ll mod=pow(10,9)+7; ll INF=pow(10,18); int main(){ int N,M;scanf("%d %d",&N,&M); vec A(M); for(int i=0;i<M;i++) scanf("%lld",&A[i]); sort(A.begin(),A.end(),greater<ll>()); vec n={0,2,5,5,4,5,6,3,7,6}; mat dp(N+1,vec(M+1,0)); //漸化式 for(int i=0;i<N-1;i++){ if(i>0 && dp[i][0]==0) continue; vec v; for(int j=0;j<M;j++){ if(i+n[A[j]]>N) continue; v=dp[i];v[0]++;v[j+1]++; int t=dp[i+n[A[j]]][0]; if(v[0]>t){ dp[i+n[A[j]]]=v; } else if(v[0]==t){ for(int k=1;k<=M;k++){ if(v[k]>dp[i+n[A[j]]][k]){ dp[i+n[A[j]]]=v; break; } else if(v[k]<dp[i+n[A[j]]][k]){ break; } } } } } string ans; for(int i=0;i<M;i++){ char c=A[i]+'0'; for(int j=0;j<dp[N][i+1];j++) ans+=c; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> #define rep(i,n) for (int i = 0; i < (int)(n); i++) #define REP(i,n) for (int i = 1; i < (int)(n); i++) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define debug(var) do{cout << #var << " : "; view(var);}while(0) template<class T> bool chmin(T &a, T b) {if(a>b) {a=b;return 1;}return 0;} template<class T> bool chmax(T &a, T b) {if(a<b) {a=b;return 1;}return 0;} using namespace std; template<class T> void view(T e) {cout << e << endl;} template<class T> void view(const vector<T> &v) {for(const auto &e : v){cout << e << " ";} cout << endl;} template<class T> void view(const vector<vector<T>> &vv) {for(const auto &v : vv){view(v);}} using vint = vector<int>; using vvint = vector<vector<int>>; using ll = long long; using vll = vector<ll>; using vvll = vector<vector<ll>>; using P = pair<int,int>; const int inf = 1e9; const ll inf_l = 1e18; const int MAX = 1e5; int main() { int n, m; cin >> n >> m; vint change = {0,2,5,5,4,5,6,3,7,6}; vint a(m); rep(i,m) cin >> a[i]; set<int> st; rep(i,m) st.insert(change[a[i]]); vint dp(n+1,-1); dp[0] = 0; REP(i,n+1) { for (auto x : st) { if (i-x >= 0 && dp[i-x] != -1) { chmax(dp[i],dp[i-x]+1); } } } int n_split = dp[n]; sort(rall(a)); string ans; while (n_split--) { rep(i,m) { if (n-change[a[i]] < 0) continue; if (dp[n-change[a[i]]] == dp[n] - 1) { ans += to_string(a[i]); n -= change[a[i]]; break; } } } cout << ans << endl; }
1
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main(){ int a,b; cin>>a>>b; int ans=0; for(int i=a;i<=b;i++){ int s[5]; int x=i; for(int j=0;j<5;j++){ s[j]=x%10; x/=10; } if(s[0]==s[4]&&s[1]==s[3])ans++; } cout<<ans<<endl; return 0; }
#include <stdio.h> int main (){ int G, N; scanf("%d %d", &G, &N); int z = 1; for (int i = 0; i < G; i++) { if (2 * z <= z + N) { z *= 2; } else { z += N; } } printf("%d", z); return 0; }
0
#include<bits/stdc++.h> using namespace std ; #define ll long long #define pb push_back #define mod 998244353 const ll N =30005 ; ll dp[2*N] ; int main () { ll n , t ; cin>> n >> t ; ll i ,j,x,y; dp[0] = 1 ; vector<pair<ll,ll> > v(n) ; for(i = 0 ; i< n ;i++) cin >> v[i].first >> v[i].second ; sort(v.begin(),v.end()) ; for(i = 1 ; i<= n ; i++) { x = v[i-1].first , y = v[i-1].second ; for (j = t-1 ; j>=0 ; j--) if (dp[j]) dp[j+x] = max(dp[j+x],dp[j]+y) ; } ll ans = 0 ; for (i = 0 ; i<2*N ; i++) ans = max(ans,dp[i]) ; cout << ans -1 << endl ; return 0 ; }
#include<bits/stdc++.h> #define reg register const int maxn = 1000005; int N; char S[maxn]; char T[maxn]; int main(){ scanf("%d", &N); scanf("%s%s", S+1, T+1); int flag = 1; for(reg int i = 1; i <= N; i ++) if(S[i] != T[i]){ flag = 0; break ; } if(flag){ printf("0\n"); return 0; } int Min_l = N, Ans = 1; std::queue <int> Q; for(reg int i = N; i >= 1; i --){ Min_l = std::min(i, Min_l); if(T[i] == T[i-1]) continue ; while(Min_l && S[Min_l] != T[i]) Min_l --; if(!Min_l){ printf("-1\n"); return 0; } while(!Q.empty()){ if(Q.front()-Q.size()+1 > i) Q.pop(); else break ; } Q.push(Min_l); if(Min_l != i) Ans = std::max(Ans, (int)Q.size() + 1); } printf("%d\n", Ans); return 0; }
0
#include<bits/stdc++.h> using namespace std; const int N=200005; long long a[N],l[N],r[N]; int main() { ios::sync_with_stdio(false); int n; long long d,ret=0; cin>>n>>d; for(int i=1;i<=n;i++) { cin>>a[i]; ret+=a[i]; } ret+=(n-1)*d; r[1]=a[1]; for(int i=2;i<=n;i++) r[i]=min(a[i],r[i-1]+d); l[n]=a[n]; for(int i=n-1;i>=1;i--) l[i]=min(a[i],l[i+1]+d); for(int i=2;i<n;i++) ret+=min(l[i],r[i]); cout<<ret<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> pii; typedef long long ll; typedef vector<int> vi; #define pb push_back #define eb emplace_back #define mp make_pair #define fi first #define se second #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define ALL(c) (c).begin(),(c).end() #define dump(x) cout << #x << " = " << (x) << endl class unionfind { vector<int> par, rank; public: void init(int n) { par.resize(n); rank.resize(n); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } int find(int x) { if (par[x] == x) return x; else return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return ; if (rank[x] < rank[y]) par[x] = y; else { par[y] = x; if (rank[x] == rank[y]) ++rank[x]; } } bool same(int x, int y) { return (find(x) == find(y)); } } uf; const int MN = 200010; using P = pair<ll, int>; vector<pii> g[MN]; int col[MN]; ll X[MN]; vector<P> dp[MN]; void mergevec(vector<P>& a, vector<P> b) { for (auto p : b) { a.eb(p); } sort(ALL(a)); set<int> st; vector<P> res; for (P p : a) { if (res.size() == 2) { break; } if (!st.count(p.se)) { st.insert(p.se); res.eb(p); } } a = res; } void dfs_up(int v, int p) { dp[v].eb(X[v], col[v]); for (pii e : g[v]) { if (e.fi != p) { dfs_up(e.fi, v); vector<P> nx; for (auto t : dp[e.fi]) { nx.eb(t.fi + e.se, t.se); } mergevec(dp[v], nx); } } } void dfs_down(int v, int p, int pw) { if (p != -1) { vector<P> nx; for (auto t : dp[p]) { nx.eb(t.fi + pw, t.se); } mergevec(dp[v], nx); } for (pii e : g[v]) { if (e.fi != p) { dfs_down(e.fi, v, e.se); } } } int N, D; int main() { cin >> N >> D; rep(i, N) cin >> X[i]; rep(i, N-1) { int a = i; int b = i + 1; int c = D; g[a].eb(b, c); g[b].eb(a, c); } uf.init(N); rep(i, N) { col[i] = i; } ll ans = 0; while (true) { bool one = true; for (int i = 1; i < N; ++i) { if (col[i] != col[0]) { one = false; } } if (one) { break; } rep(i, N) { dp[i].clear(); } vector<P> mt(N, mp(LLONG_MAX, -1)); dfs_up(0, -1); dfs_down(0, -1, -1); rep(i, N) { for (P p : dp[i]) { if (p.se != col[i]) { mt[col[i]] = min(mt[col[i]], mp(p.fi + X[i], p.se)); break; } } } rep(i, N) if (mt[i] != mp(LLONG_MAX, -1)) { int u = i, v = mt[i].se; ll cost = mt[i].fi; if (!uf.same(u, v)) { uf.unite(u, v); ans += cost; } } rep(i, N) { col[i] = uf.find(col[i]); } } cout << ans << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; int main(void){ int n,u,v; cin>>n>>u>>v; vector<vector<int>> t(n+1,vector<int>(0)); for(int i=0; i<n; ++i){ int a,b; cin>>a>>b; t[a].push_back(b),t[b].push_back(a); } vector<int> ud(n+1,0),vd(n+1,0); vector<bool> d(n+1,true); queue<pair<int,int>> q; q.push({v,0}); d[v]=false; while(!q.empty()){ int x=q.front().first,y=q.front().second; q.pop(); vd[x]=y; for(auto& i:t[x]) if(d[i]==true) q.push({i,y+1}),d[i]=false; } int res=0; d=vector<bool>(n+1,true); q.push({u,0}); d[u]=false; while(!q.empty()){ int x=q.front().first,y=q.front().second; q.pop(); ud[x]=y; if(vd[x]>ud[x]) res=max(vd[x]-1,res); for(auto& i:t[x]) if(d[i]==true) q.push({i,y+1}),d[i]=false; } cout<<res; return 0; }
#include<bits/stdc++.h> using namespace std; vector<int>adj[100003],leaf; int par[100003],depth[100005],sparse[100005][20],root=0,lz=0; int lca(int p,int q) { int a,b,c,d,lp,i,j; if(depth[p]<depth[q]) { c=p; p=q; q=c; } for(j=19;j>=0;j--) { if((depth[p]-(1 << j))>=depth[q]) { p=sparse[p][j]; if(depth[p]==depth[q])break; } } if(p==q){ return p; } else { for(j=19;j>=0;j--) { if(sparse[p][j]!=0 && sparse[p][j]!=sparse[q][j]) { p=sparse[p][j]; q=sparse[q][j]; if(par[p]==par[q])break; } } return par[p]; } } void dfs(int src) { int sz=adj[src].size(); if(sz==1 && src!=root){ leaf.push_back(src); lz++; } for(int lp=0;lp<sz;lp++) { int u=adj[src][lp]; if(u!=root && par[u]==0) { par[u]=src; sparse[u][0]=src; depth[u]=depth[src]+1; dfs(u); } } } int main() { int n,m,a,b,i,j,u,v,ans,z,x,y,fr,sc,tr,w; scanf("%d %d %d",&n,&u,&v); for(i=1;i<=n-1;i++) { scanf("%d %d",&a,&b); adj[a].push_back(b); if(root==0){ if(adj[a].size()==2)root=a; } adj[b].push_back(a); if(root==0){ if(adj[b].size()==2)root=b; } } if(n==2)printf("0\n"); else { dfs(root); for(j=1;j<=19;j++) { for(i=1;i<=n;i++) { if(sparse[i][j-1]!=0) { sparse[i][j]=sparse[sparse[i][j-1]][j-1]; } } } m=lca(u,v); ans=(depth[v]-depth[m])+(depth[u]-depth[m]); ans=ans-1; for(i=0;i<lz;i++) { y=leaf[i]; m=lca(u,y); w=lca(v,y); fr=(depth[u]-depth[m])+(depth[y]-depth[m]); sc=(depth[v]-depth[w])+(depth[y]-depth[w]); if(fr<sc) { if(sc-1>ans)ans=sc-1; } } printf("%d\n",ans); } return 0; }
1
#include <iostream> using namespace std; string s; int main() { cin >> s; if (s.length() % 2 != 0) { cout << "No\n"; return 0; } bool works = true; for (int i=0; i<s.length(); i+=2) { if (s[i] != 'h' || s[i + 1] != 'i') works = false; } if (works) cout << "Yes\n"; else cout << "No\n"; }
#include <bits/stdc++.h> #define ll long long int #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define vi vector<int> #define vll vector<long long int> #define all(v) v.begin(),v.end() const ll MOD = 1e9 + 7; const ll INF = 10000000000000000; const ll MAX_N = 1e5 + 7; using namespace std; //vector<bool> h(10000001,1); int main(){ string s; cin >> s; int n = (int)s.size(); n/=2; string b=""; for(int i =0;i<n;i++) b+="hi"; //cout << b << '\n'; if(b==s)cout << "Yes"; else cout << "No"; }
1
#include <iostream> #include <limits.h> using namespace std; int dp[100][100]; int p[101]; int n; void solve() { for (int m = n; m > 0; m--) { for (int t = 0, i = 0, j = n - m + 1; t < n && j < n; t++) { dp[i][j] = INT_MAX; for (int k = i; k < j; k++) { int temp = dp[i][k] + dp[k + 1][j] + p[i] * p[k + 1] * p[j + 1]; if (temp < dp[i][j]) { dp[i][j] = temp; } } i++; j++; } } } int main() { int a, b; cin >> n >> a; p[0] = a; for (int i = 1; i < n; i++) { cin >> a >> b; p[i] = a; } cin >> a; p[n] = a; solve(); cout << dp[0][n - 1] << endl; return 0; }
#include <stdio.h> #include <limits.h> int main(){ int n,p[101],i,j,k,l,x,m[101][101]; scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d %d",&p[i-1],&p[i]); for(i=1;i<=n;i++)m[i][i]=0; for(l=2;l<=n;l++){ for(i=1;i<=n-l+1;i++){ j=i+l-1; m[i][j]=INT_MAX; for(k=i;k<j;k++){ x=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]; if(m[i][j]>x)m[i][j]=x; } } } printf("%d\n",m[1][n]); return 0; }
1
#include <bits/stdc++.h> using namespace std ; #define pb(n) push_back(n) #define fi first #define se second #define all(r) (r).begin(),(r).end() #define gsort(st,en) sort((st),(en),greater<int>()) #define vmax(ary) *max_element(all(ary)) #define vmin(ary) *min_element(all(ary)) #define debug(x) cout<<#x<<": "<<x<<endl #define fcout(n) cout<<fixed<<setprecision((n)) #define scout(n) cout<<setw(n) #define vary(type,name,size,init) vector< type> name(size,init) #define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init)); #define rep(i,n) for(int i = 0; i < (int)(n);++i) #define REP(i,a,b) for(int i = (a);i < (int)(b);++i) #define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it) #define repa(n,array) for(auto &n :(array)) using ll = long long; using vi = vector<int>; using vl = vector<ll>; using dict = map<string,int>; using pii = pair<int,int> ; using pll = pair<ll,ll> ; const int mod = 1000000007; constexpr int imax = ((1<<30)-1)*2+1 ; constexpr int inf = 100000000; constexpr double PI = acos(-1.0) ; double eps = 1e-10 ; const int dy[] = {-1,0,1,0,1,-1,1,-1}; const int dx[] = {0,-1,0,1,1,-1,-1,1}; inline bool value(int x,int y,int w,int h){ return (x >= 0 && x < w && y >= 0 && y < h); } struct data{ ll i,j,k; vector<ll> v; ll ooo(set<ll> ok){ ll cnt = 0,part,flag = 0; v.resize(3); v[0] = i; v[1] = j; v[2] = k; for(auto n:ok){ rep(i,3){ if(v[i] == n){ flag |= (1 << i); ++cnt; } } } if(cnt == 2){ rep(i,3){ if(!(flag & (1 << i))){ return v[i]; } } } return -1; } }; int main(){ cin.tie(0); ios::sync_with_stdio(false); ll a,b,c; while(cin >> a >> b >> c && a && b && c){ ll n; set<ll> ok; set<ll> wrong; vector<data> qes; cin >> n; ll x,y,z,w; rep(i,n){ cin >> x >> y >> z >> w; --x,--y,--z; if(w == 1){ ok.insert(x); ok.insert(z); ok.insert(y); } else{ qes.push_back({x,y,z}); } } rep(i,qes.size()){ wrong.insert(qes[i].ooo(ok)); } rep(i,a+b+c){ if(find(all(ok),i) != ok.end()){ cout << 1<<endl; } else if(find(all(wrong),i) != wrong.end()){ cout << 0 <<endl; } else cout << 2 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; #define loop(i,n) for(int i=1;i<=n;i++) struct test { int i, j, k; }; vector<test> t; int p[302]; int main(){ int a, b, c, n, i, j, k, r; while (cin >> a >> b >> c){ cin >> n; t.clear(); loop(i, a + b + c)p[i] = 2; loop(h, n){ cin >> i >> j >> k >> r; if (r == 1){ p[i] = p[j] = p[k] = 1; } else{ t.push_back({ i, j, k }); } } for (int h = 0; h < t.size(); h++){ i = t[h].i; j= t[h].j; k = t[h].k; if (p[i] == 1 && p[j] == 1){ p[k] = 0; } else if (p[j] == 1 && p[k] == 1){ p[i] = 0; } else if (p[k] == 1 && p[i] == 1){ p[j] = 0; } } loop(i, a + b + c)cout << p[i] << endl; } return 0; }
1
#include <iostream> using namespace std; const int INF = 1 << 30; const int dx[] = { 0, 0, 1, -1 }; const int dy[] = { 1, -1, 0, 0 }; int W, H; int B[32][32]; int dfs(int x, int y, int times) { if(times == 10) return INF; int best = INF; for(int i = 0; i < 4; i++) { int j = 1; for(; ; j++) { int ny = y + dy[i] * j, nx = x + dx[i] * j; if(nx < 0 || W <= nx || ny < 0 || H <= ny) { break; } if(B[ny][nx] == 1) { if(j == 1) { break; } B[ny][nx] = 0; best = min(best, dfs(nx - dx[i], ny - dy[i], times + 1)); B[ny][nx] = 1; break; } if(B[ny][nx] == 3) { return times + 1; } } } return best; } int main() { for(; cin >> W >> H && W; ) { int sx = -1, sy = -1; for(int y = 0; y < H; y++) { for(int x = 0; x < W; x++) { cin >> B[y][x]; if(B[y][x] == 2) { sx = x; sy = y; } } } int res = dfs(sx, sy, 0); cout << (res == INF ? -1 : res) << endl; } }
#include<bits/stdc++.h> #define int long long using namespace std; const int MAXN = 1e5 + 10; int N; int a[1001][1001], vis[MAXN], prime[MAXN], tot; void GetPhi() { vis[1] = 1; for(int i = 2; i; i++) { if(!vis[i]) prime[++tot] = i; if(tot == 1000) break; for(int j = 1; j <= tot && (i * prime[j] <= 10000); j++) { vis[i * prime[j]] = 1; if(!(i % prime[j])) break; } } } int lcm(int x, int y) { if(x == 0 || y == 0) return x + y; return x / __gcd(x, y) * y; } main() { GetPhi(); cin >> N; if(N == 2) { printf("4 7\n23 10"); return 0; } for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(!((i + j) & 1)) a[i][j] = prime[(i + j) / 2] * prime[N + (i - j) / 2 + (N + 1) / 2]; for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(!a[i][j]) a[i][j] = lcm(lcm(a[i - 1][j], a[i][j - 1]), lcm(a[i][j + 1], a[i + 1][j])) + 1; for(int i = 1; i <= N; i++, puts("")) for(int j = 1; j <= N; j++) cout << a[i][j] << " "; return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for (int i = 0; i < (int)(n); i++) int main() { int a, b, c; cin >> a >> b >>c; if (a < b) { if (a <= c && c <= b) puts("Yes"); else puts("No"); } else { if (b <= c && c <= a) puts("Yes"); else puts("No"); } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define rep(k,i,n) for(ll i=k;i<n;++i) int main(void){ ll a,b,c; cin>>a>>b>>c; if(a>b)swap(a,b); rep(a,i,b+1){ if(i==c){ cout<<"Yes"; return 0; } } cout<<"No"; }
1
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math") #include <bits/stdc++.h> using namespace std; template<class t> inline t read(t &x){ char c=getchar();bool f=0;x=0; while(!isdigit(c)) f|=c=='-',c=getchar(); while(isdigit(c)) x=(x<<1)+(x<<3)+(c^48),c=getchar(); if(f) x=-x;return x; } template<class t> inline void write(t x){ if(x<0) putchar('-'),write(-x); else{if(x>9) write(x/10);putchar('0'+x%10);} } #define int long long const int N=205; int basis[65],n,a[N],b[N]; void insert(int x){ for(int i=59;~i;i--) if(x>>i&1){ if(basis[i]) x^=basis[i]; else{ basis[i]=x; return ; } } } bool exists(int x){ for(int i=59;~i;i--) if(x>>i&1){ if(basis[i]) x^=basis[i]; else return 0; } return 1; } bool doit(){ read(n); for(int i=0;i<60;i++) basis[i]=0; for(int i=1;i<=n;i++) read(a[i]); for(int i=1;i<=n;i++) scanf("%1d",&b[i]); for(int i=n;i;i--){ if(!b[i]) insert(a[i]); else if(!exists(a[i])) return 1; } return 0; } signed main(){ int t; read(t); while(t--) write(doit()),puts(""); }
#include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; int main(void){ int T, N; long long Abuf; vector<long long> A; string S; cin >> T; for(int itestcase=0; itestcase<T; itestcase++){ cin >> N; A.clear(); for(int i=0; i<N; i++){ cin >> Abuf; A.push_back(Abuf); } cin >> S; vector<long long> DP; DP.push_back(0); for(int i=N-1; i>=0; i--){ Abuf = A[i]; for(long long v: DP)Abuf = min(Abuf, Abuf^v); if(S[i]=='0'){ DP.push_back(Abuf); }else if(Abuf != 0LL){ DP.clear(); break; } } if(DP.empty()){ cout << 1 << endl; }else{ cout << 0 << endl; DP.clear(); } } return 0; }
1
#include <algorithm> #include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define all(x) (x).begin(), (x).end() #define ll long long #define INF 1000000000000000000 typedef pair<ll, ll> pll; int main() { cin.tie(0); ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<ll> x; rep(i, N) { ll a; cin >> a; if (a == 0) { K--; N--; } else x.push_back(a); } if (x.empty()) { cout << 0 << endl; return 0; } int a = lower_bound(all(x), 0) - x.begin(); ll ans = INF; int s = a - K, t = a - 1; int cnt = 0; while (cnt <= K) { if (s < 0 || t >= N) { cnt++; s++; t++; continue; } else if (x[s] * x[t] < 0) { ans = min(ans, abs(x[s] * 2) + abs(x[t])); ans = min(ans, abs(x[s]) + abs(x[t] * 2)); } else ans = min(ans, max(abs(x[s]), abs(x[t]))); cnt++; s++; t++; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> #include <iostream> #include <string> #include <string.h> #include <ctype.h> #include <algorithm> using namespace std; int main() { int n,ans,W[310000] = {},E[310000] = {}; string s; cin >> n >> s; for(int i = 1; i <= n-1; i++) { if(s[i-1] == 'W') W[i] = W[i-1] + 1; else W[i] = W[i-1]; } for(int i = n-2; i >= 0; i--) { if(s[i+1] == 'E') E[i] = E[i+1] + 1; else E[i] = E[i+1]; } int m = n; for(int i = 0; i < n; i++) m = min(m,W[i]+E[i]); cout << m << endl; }
0
#include <iostream> using namespace std; #define loop(i,a,b) for(int i=(a); i<(int)(b); i++) #define rep(i,b) loop(i,0,b) int main() { int n,m; cin>>n>>m; int A[1000], B[1000]; rep(i,n) cin>>A[i]; int c[1000]={}; rep(i,m) cin>>B[i]; rep(j,m)rep(i,n){ if(B[j]>=A[i]){ c[i]++; break; } } int mx=0, num=0; rep(i,n){ if(mx<c[i]){ num=i; mx=c[i]; } } cout<<num+1<<endl; }
#include <bits/stdc++.h> using namespace std; #define BE(x) x.begin(), x.end() int main() { long long l, r; cin >> l >> r; long long ans = 2019; int icnt = 0; for (long long i = l; i < r; i++) { int jcnt = 0; for (long long j = i+1; j <= r; j++) { ans = min(ans, i*j%2019); jcnt++; if (jcnt == 2020) break; } icnt++; if (icnt == 2020) break; } cout << ans << endl; }
0
/* HARD WORK FOREVER PAYS */ #include<bits/stdc++.h> #define pb push_back #define mp make_pair #define f first #define s second #define turbo(){ \ ios_base::sync_with_stdio(false); \ cin.tie(NULL); \ } using namespace std; typedef long long ll; typedef pair<int,int> PII; typedef unsigned long long int ull; vector<ull> allPrimes; void sieve(int n) { vector<bool> prime(n+1, true); for (int p=2; p*p<=n; p++) { if (prime[p] == true) { for (int i=p*2; i<=n; i += p) prime[i] = false; } } for (int p=2; p<=n; p++) if (prime[p]) allPrimes.push_back(p); } ull factorialDivisors(ull n) { sieve(n); ull result = 1; for (int i=0; i < allPrimes.size(); i++) { ull p = allPrimes[i]; ull exp = 0; while (p <= n) { exp = exp + (n/p); p = p*allPrimes[i]; } result = result*(exp+1)%1000000007; } return result; } int main() { int n; cin>>n; cout << factorialDivisors(n)<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main(){ long long n; long long ans = 1; cin >> n; vector<int> fact(10000); //factor container for(int j = 2; j<=n;j++){ long long num = j; for (int i = 2; i <= num; i++) { while (num%i == 0) { // 素数で割り切れなくなるまで割っていく fact.at(i)++; //割った個数を配列に足す num /= i; } } } for(int i = 0; i < 1003; i++){ ans *= (fact.at(i) + 1); ans %= 1000000007; } cout << ans << endl; }
1
#include <stdio.h> #include <queue> using namespace std; int main() { const int INF = 10000000; int n, k, map[100][100], a, b, c; priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q; while(true) { scanf("%d%d", &n, &k); if(n == 0 && k == 0) break; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) map[i][j] = INF; } for(int g = 0; g < k; ++g) { scanf("%d", &a); if(a == 0) { //ŒvŽZ int ans = -1; bool flag[100] = {}; scanf("%d%d", &a, &b); q.push(pair<int, int>(0, a - 1)); while(!q.empty()) { pair<int, int> p = q.top(); q.pop(); int &cost = p.first, &num = p.second; if(flag[num]) continue; flag[num] = true; if(num == b - 1) { ans = cost; for(int i = q.size(); i > 0; --i) q.pop(); break; } for(int i = 0; i < n; ++i) { if(map[num][i] == INF || flag[i]) continue; q.push(pair<int, int>(cost + map[num][i], i)); } } printf("%d\n", ans); } else { //’ljÁ scanf("%d%d%d", &a, &b, &c); if(map[a - 1][b - 1] > c) map[a - 1][b - 1] = map[b - 1][a - 1] = c; } } } return 0; }
#include <iostream> #include <algorithm> using namespace std; #define INF 100000000 int map[100][100]; int n,k,cou,tmin; int dd[101],col[100]; int main(){ int t,a,b,c,d,e; cin >> n >> k; while(n!=0&&k!=0){ for (int i=0;i<10000;i++)map[i/100][i%100]=0; for (int i=0;i<k;i++){ cin >> t; // for (int y=0;y<k;y++){ //for (int x=0;x<k;x++) // cout << map[y][x] << " "; //cout << endl; //} if (t == 0){ cin >> a >> b; for (int j=0;j<101;j++)dd[j]=INF; for (int j=0;j<101;j++)col[j]=0; dd[a-1]=0; cou = 1; col[a-1]=1; for (int m=0;m<n;m++) if(map[a-1][m]>0) dd[m]=min(dd[m],dd[a-1]+map[a-1][m]); while (cou < n){ tmin=100; for (int m=0;m<n;m++) if (col[m]==0) if (dd[tmin]>dd[m]) tmin = m; // for (int m=0;m<n;m++)cout << dd[m] << " ";//Dbg // cout << endl;//Dbg col[tmin]=1; cou ++; for (int m=0;m<n;m++) if(map[tmin][m]>0) dd[m]=min(dd[m],dd[tmin]+map[tmin][m]); } if (dd[b-1]==INF)cout << -1 << endl; else cout << dd[b-1] <<endl; }else{ cin >> c >> d >> e; c--;d--; if (map[c][d]==0){ map[c][d]=e; map[d][c]=e; } map[c][d]=min(map[c][d],e); map[d][c]=min(map[d][c],e); } } cin >> n >> k; } return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string) s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); } #ifdef LOCAL #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) 42 #endif int main() { ios:: sync_with_stdio(false); cin.tie(); long long n; cin >> n; long long mi; cin >> mi; for (int i = 0; i < 4; i++) { long long x; cin >> x; mi = min(mi, x); } long long ans = 5 - 1 + (n + (mi - 1)) / mi; cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve(ll N, ll A, ll B, ll C, ll D, ll E) { ll bn = min({A, B, C, D, E}); // bottle neck cout << ((N - 1) / bn + 5) << endl; } int main(){ cin.tie(0); ios::sync_with_stdio(false); ll N, A, B, C, D, E; cin >> N >> A >> B >> C >> D >> E; solve(N, A, B, C, D, E); return 0; }
1
#include<bits/stdc++.h> using namespace std; using lli = long long; #define rep(i,n) for(int i=0;i<n;i++) #define N 100100 lli n; int main(void){ cin >> n; vector<lli> v(n); vector<lli> e(N), o(N); rep(i, n) cin >> v[i]; rep(i, n){ if(i%2 == 0){ e[v[i]]++; }else{ o[v[i]]++; } } auto p1 = max_element(e.begin(), e.end())-e.begin(); auto p2 = max_element(o.begin(), o.end())-o.begin(); if(p1 != p2){ cout << n-e[p1]-o[p2] << endl; }else{ if(e[p1] < o[p2]){ e[p1] = 0; lli ne = *max_element(e.begin(), e.end()); cout << n-ne-o[p2] << endl; }else if(e[p1] == o[p2]){ lli tmp = e[p1]; e[p1] = o[p2] = 0; lli noe = max(*max_element(e.begin(), e.end()), *max_element(o.begin(), o.end())); cout << n-tmp-noe << endl; }else{ o[p2] = 0; lli no = *max_element(o.begin(), o.end()); cout << n-e[p1]-no << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; //vector<vector<int>> a(5, vector<int>(5, 0)) 5*5-0 int main(){ long long int n; cin >> n; vector<int> a(n); for(int i = 0; i < n; i++){ cin >> a.at(i); } long long int step = 0; for(int i = 1; i < n; i++){ if(a.at(i - 1) > a.at(i)){ step += a.at(i - 1) - a.at(i); a.at(i) = a.at(i - 1); } } cout << step << endl; }
0
#include<stdio.h> int V,E,R,S[500010],T[500010],D[500010]; int C[100000]; const int INF=10000*100000+100; int i,t,j; int main() { for(i=0;i<100000;i++) { C[i]=INF; } scanf("%d",&V); scanf("%d",&E); scanf("%d",&R); C[R]=0; for(i=0;i<E;i++) { scanf("%d",&S[i]); scanf("%d",&T[i]); scanf("%d",&D[i]); } for(t=0;t<V;t++) { int update=-1; for(i=0;i<E;i++) { int s=S[i],t=T[i],d=D[i]; if(C[s]<INF && C[t]>C[s]+d) { C[t]=C[s]+d; update=1; } } if(update==-1) break; } for(j=0;j<V;j++) { if(C[j]<INF) printf("%d\n",C[j]); else printf("INF\n"); } return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> typedef struct{ int n, cost; }Data; typedef struct{ Data data; int prior; }node; typedef struct{ node* nodes; int arr_sz; //??????????????????????????? int que_sz; //?¬????????´?????????´???????????? }priority_queue; node* my_realloc(node* old_ptr, int old_sz, int new_sz){ node* new_ptr = (node*)malloc(sizeof(node) * new_sz); if(old_sz){ memcpy(new_ptr, old_ptr, sizeof(node) * old_sz); free(old_ptr); } return new_ptr; } void push(priority_queue *q, Data d, int prior){ if(q->que_sz >= q->arr_sz){ int new_sz = (q->arr_sz ? q->arr_sz * 2 : 8); q->nodes = my_realloc(q->nodes, q->arr_sz, new_sz); q->arr_sz = new_sz; } int k = q->que_sz; while(true){ if(k == 0)break; int next = (k - 1) / 2; if(q->nodes[next].prior > prior){ q->nodes[k] = q->nodes[next]; k = next; } else break; } q->nodes[k].data = d; q->nodes[k].prior = prior; ++q->que_sz; } Data pop(priority_queue *q){ Data ret = q->nodes[0].data; q->nodes[0] = q->nodes[--q->que_sz]; int k = 0; while(true){ int l = k * 2 + 1, r = k * 2 + 2; int next = -1; if(l < q->que_sz)next = l; if(r < q->que_sz)next = (q->nodes[r].prior < q->nodes[l].prior ? r : l); if(next == -1)break; if(q->nodes[next].prior < q->nodes[k].prior){ q->nodes[k] = q->nodes[next]; k = next; } else break; } q->nodes[k] = q->nodes[q->que_sz]; return ret; } typedef struct{ int to, cost; }Edge; #define maxN 100005 #define maxM 500001 #define inf 2000000000 int cost[maxN]; Edge *g[maxN]; int g_sz[maxN], cnt[maxN]; int n, m, r; int s[maxM], t[maxM], d[maxM]; void dijkstra(int k){ priority_queue que = {0}; for (int i = 0; i < n; ++i)cost[i] = inf; cost[k] = 0; Data d; d.n = k, d.cost = 0; push(&que, d, d.cost); while (que.que_sz){ d = pop(&que); int v = d.n; if (cost[v] < d.cost)continue; for (int i = 0; i < g_sz[v]; ++i){ Edge e = g[v][i]; if (cost[e.to] > cost[v] + e.cost){ cost[e.to] = cost[v] + e.cost; Data x; x.n = e.to, x.cost = cost[e.to]; push(&que, x, x.cost); } } } } int main(void){ scanf("%d%d%d", &n, &m, &r); for(int i = 0; i < m; ++i){ scanf("%d%d%d", &s[i], &t[i], &d[i]); ++cnt[s[i]]; } for(int i = 0; i < n; ++i){ g[i] = (Edge*)malloc(sizeof(Edge) * cnt[i]); } for(int i = 0; i < m; ++i){ Edge e; e.to = t[i], e.cost = d[i]; g[s[i]][g_sz[s[i]]++] = e; } dijkstra(r); for(int i = 0; i < n; ++i){ if(cost[i] == inf)printf("INF\n"); else printf("%d\n", cost[i]); } return 0; }
1
#include<iostream> #include<string> #include<algorithm> using namespace std; int main(){ int x,y,z; int ans=0; x=y=z=0; char c='\0'; string s; cin>>s; for(int i=0;i<s.size();i++){ bool f=true; if(s[i]=='J'){ if(c=='J')x++; else { x=1; y=z=0; } }else if(s[i]=='O'){ if(c=='J' || c=='O')y++; else f=false; }else{ if(c=='O' || c=='I'){ z++; if(y<=x && y<=z)ans=max(ans,y); } else f=false; } if(f)c=s[i]; else c='\0'; } cout<<ans<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N, M = 0, h = 0; cin >> N; M = N; for(int i = 0; i < 10; i++) { h += M % 10; M = M / 10; if(M == 0) { break; } } if(N % h == 0) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
0
#include <bits/stdc++.h> #include <regex> using namespace std; #define _GLIBCXX_DEBUG //vector<int> A(M), B(M); //vector<vector<char>> answer(N, vector<char>(N, '-')); int main() { string s; int ans = 0; cin >> s; if(s[0] == 'R'){ ans++; if(s[1] == 'R'){ ans++; if(s[2] == 'R') ans++; } } else{ if(s[1] == 'R'){ ans++; if(s[2] == 'R') ans++; } else{ if(s[2] == 'R') ans++; } } cout << ans << endl; return 0; }
#include<iostream> #include<string> #include<algorithm> #include<vector> #include<queue> #include<set> #include<iomanip> #define pi 3.14159265358979323846 #define ll long long #define rep(i, a, n) for(int i = a; i < n; i++) using namespace std; int main(){ string s; cin >> s; if(s == "RRR"){ cout << "3\n"; return 0; } if(s == "RRS" || s == "SRR"){ cout << "2\n"; return 0; } if(s == "SSS"){ cout << "0\n"; return 0; } cout << "1\n"; return 0; }
1
#include <iostream> using namespace std; typedef long long int LLI; int main() { LLI n, t, tmp, next, ans; cin >> n >> t >> tmp; ans = t; for(int i = 1; i < n; i++) { cin >> next; if( next - tmp >= t ) ans += t; else ans += next - tmp; tmp = next; } cout << ans; return 0; }
#include<bits/stdc++.h> using namespace std; ///Welcome to Nasif's Code #define bug printf("bug\n"); #define bug2(var) cout<<#var<<" "<<var<<endl; #define co(q) cout<<q<<endl; #define all(q) (q).begin(),(q).end() typedef long long int ll; typedef unsigned long long int ull; const int MOD = (int)1e9+7; const int MAX = 1e6; #define pi acos(-1) #define inf 1000000000000000LL #define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int main() { FastRead //freopen("output.txt", "w", stdout); ll n,t,sum=0,en=-1,last; cin>>n>>t; while(n--) { ll a; cin>>a; if(a>en) { sum+=t; en=a+t; last=a; } else{ sum+=(a-last); en=a+t; last=a; } } cout<<sum<<endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; #define ll long long #define REP(i,m,n) for(int i=(int)(m); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define RREP(i,m,n) for(int i=(int)(m); i>=(int)(n); i--) #define rrep(i,n) RREP(i,n-1,0) #define all(v) v.begin(), v.end() const int inf = 1e9+7; const ll longinf = 1LL<<60; const ll mod = 1e9+7; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; string s, t; cin >> N >> s >> t; for(int l=N; l<=2*N; l++) { string S(l, ' '); rep(i, N) S[i] = s[i]; bool ok = true; rep(i, N) if(S[l-N+i] != ' ' && S[l-N+i] != t[i]) ok = false; if(ok) { cout << l << "\n"; return 0; } } return 0; }
#include<stdio.h> int main(void) { int a,s,d,f,g,h[10001],i,j,z[10001]; scanf("%d",&a); for(i=1;i<=a;i++){ z[i]=1; //printf("%d\n",z[4]); } // printf("%d\n",z[4]); for(i=1;i<=a*(a-1)/2;i++){ h[i]=0; } for(i=1;i<=a*(a-1)/2;i++){ scanf("%d %d %d %d",&s,&d,&f,&g); if(f<g){ h[d]=h[d]+3; } else if(f>g){ h[s]=h[s]+3; } else if(f==g){ h[d]++; h[s]++; } } for(i=1;i<=a;i++){ for(j=1;j<=a;j++){ if(h[i]<h[j]){ z[i]++; // printf("z[%d]=%d\n",i,z[i]); } } } for(i=1;i<=a;i++){ printf("%d\n",z[i]); } return 0; }
0
#include<bits/stdc++.h> #define N 100005 using namespace std; int n; int val[3][N],p[N],inv[N],cir[2]; void fail(){puts("No");exit(0);} int main(){ scanf("%d",&n); for(int t=0;t<3;t++) for(int i=1;i<=n;i++) scanf("%d",&val[t][i]); for(int i=1;i<=n;i++){ p[i]=(val[1][i]+1)/3; int temp[3]={val[0][i],val[1][i],val[2][i]}; sort(temp,temp+3); for(int j=0;j<3;j++) if(temp[j]!=p[i]*3+j-2) fail(); if(val[1][i]%3!=2) fail(); inv[i]=val[0][i]>val[1][i] ? 1:0; } for(int i=1;i<=n;i++) if((p[i]-i)%2!=0) fail(); for(int i=2;i<=n;i++) inv[i%2]+=inv[i]; for(int i=1;i<=n;i++) if(p[i]!=0){ cir[i%2]++; for(int j=i,last;j!=0;last=j,j=p[j],p[last]=0); } for(int i=0;i<=1;i++) if(((n/2+(n&i))-cir[i])%2!=inv[i^1]%2) fail(); return puts("Yes"),0; }
#ifdef DEBUG #define _GLIBCXX_DEBUG #endif #include <bits/stdc++.h> using namespace std; mt19937 mrand(random_device{} ()); int rnd(int x) { return mrand() % x; } typedef long double ld; typedef long long ll; #ifdef DEBUG #define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr) #else #define eprintf(...) ; #endif #define pb push_back #define mp make_pair #define sz(x) ((int) (x).size()) #define TASK "text" const int inf = (int) 1.01e9; const ld eps = 1e-9; const ld pi = acos((ld) -1.0); const int mod = (int) 1e9 + 7; void add(int &x, int y) { if ((x += y) >= mod) { x -= mod; } } int mult(int x, int y) { return (long long) x * y % mod; } int myPower(int x, int pw) { int res = 1; for (; pw; pw >>= 1) { if (pw & 1) { res = mult(res, x); } x = mult(x, x); } return res; } void precalc() { } const int maxn = (int) 1e5 + 10; int table[maxn][3]; int n; int read() { if (scanf("%d", &n) < 1) { return 0; } for (int i = 0; i < 3; ++i) { for (int j = 0; j < n; ++j) { scanf("%d", table[j] + i); } } return 1; } int a[maxn]; int xr[maxn]; int used[maxn]; void solve() { for (int i = 0; i < n; ++i) { if (abs(table[i][1] - table[i][0]) != 1 || abs(table[i][2] - table[i][1]) != 1) { printf("No\n"); return; } a[i] = (table[i][0] - 1) / 3; xr[i] = (table[i][0] > table[i][2]); } for (int i = 0; i < n; ++i) { used[i] = 0; } int whole[2] = {(((n + 1) / 2) & 1), ((n / 2) & 1)}; for (int i = 0; i < n; ++i) { if ((a[i] & 1) != (i & 1)) { printf("No\n"); return; } whole[!(i & 1)] ^= xr[i]; if (!used[i]) { whole[i & 1] ^= 1; for (int x = i; !used[x]; x = a[x]) { used[x] = 1; } } } if (whole[0] || whole[1]) { printf("No\n"); } else { printf("Yes\n"); //printf("No\n"); } } int main() { precalc(); #ifdef LOCAL freopen(TASK ".out", "w", stdout); assert(freopen(TASK ".in", "r", stdin)); #endif while (1) { if (!read()) { break; } solve(); #ifdef DEBUG eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC); #endif } return 0; }
1
/** * created: 01.07.2020 15:47:43 **/ #define _GLIBCXX_DEBUG #include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> // using bint = boost::multiprecision::cpp_int; using namespace std; #define int long long #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i, s, n) for (int i = (s); i < (int)(n); i++) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() #define pcnt(bit) __builtin_popcountll(bit) 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;} const int mod = 1000000007; // const int MOD = 998244353; const long double pi = acos(-1.0); const int inf = 1LL << 60; signed main() { cin.tie(0); ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> a(n); int sum = 0; rep(i,n) { cin >> a[i]; if (i != 0) sum += abs(a[i] - a[i-1]); } sum += abs(a[0]); sum += abs(a[n-1]); rep(i,n) { if (i == 0) cout << sum - (abs(a[0]) + abs(a[1] - a[0])) + abs(a[1]) << endl; else if (i == n-1) cout << sum - (abs(a[n-1] - a[n-2]) + abs(a[n-1])) + abs(a[n-2]) << endl; else cout << sum - (abs(a[i]-a[i-1]) + abs(a[i+1]-a[i])) + abs(a[i+1]-a[i-1]) << endl;; } return 0; }
#include<bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; vector<int> a(n+1,0); for(int i=1;i<=n;i++) cin>>a.at(i); vector<int> d(n+1); int s=0; for(int i=0;i<n;i++){ d.at(i)=abs(a.at(i+1)-a.at(i)); s+=d.at(i); } d.at(n)=abs(a.at(n)); s+=d.at(n); for(int i=1;i<=n;i++){ if(min({a.at(i-1),a.at(i),a.at((i+1)%(n+1))})==a.at(i)||max({a.at(i-1),a.at(i),a.at((i+1)%(n+1))})==a.at(i)) cout<<s-2*min(d.at(i-1),d.at(i))<<endl; else cout<<s<<endl; } }
1
#define _USE_MATH_DEFINES #include "bits/stdc++.h" #define rep(i,a,b) for (int i = (a); i < (b); i++) using namespace std; typedef long long int ll; typedef pair<ll,ll> P; typedef complex<double> com; const int mod = 1e9 + 7; const int MOD = 998244353; const int inf = 2e9; int main() { int n; cin >> n; vector<P> a(n); rep(i, 0, n) cin >> a[i].first >> a[i].second; rep(i, 0, n) { int idx = 0; double b[201]; rep(j, 0, n) { if (i == j) continue; b[idx++] = atan2(a[j].second - a[i].second, a[j].first - a[i].first); } sort(b, b + idx); rep(j, 0, idx) b[j + idx] = b[j] + 2 * M_PI; double ans = 0; rep(j, 0, idx) ans = max(ans, M_PI - b[j + idx - 1] + b[j]); printf("%.12f\n", ans/2/M_PI); } return 0; }
#define _USE_MATH_DEFINES #include <iostream> #include <string> #include <utility> #include <stack> #include <vector> #include <queue> #include <algorithm> #include <map> #include <climits> #include <set> #include <cmath> #include <numeric> #include <iomanip> using namespace std; pair <long long, long long> xy[100]; long long x[100]; long long y[100]; vector <pair <long long, long long> > c_hull; template <typename T,typename U> std::pair<T,U> operator+(const std::pair<T,U> & l,const std::pair<T,U> & r) { return {l.first+r.first,l.second+r.second}; } template <typename T,typename U> std::pair<T,U> operator-(const std::pair<T,U> & l,const std::pair<T,U> & r) { return {l.first-r.first,l.second-r.second}; } long long cross(pair <long long, long long> xy1, pair <long long, long long> xy2){ return xy1.first * xy2.second - xy1.second * xy2.first; } long long dot(pair <long long, long long> xy1, pair <long long, long long> xy2){ return xy1.first * xy2.first + xy1.second * xy2.second; } double norm(pair <long long, long long> xy){ return sqrt((double)(xy.first * xy.first) + (double) (xy.second * xy.second)); } void convex_hull(int N){ sort(xy, xy + N); int k = 0; c_hull.resize(2 * N); for(int i = 0; i < N; i++){ while(k > 1 && cross(c_hull[k - 1] - c_hull[k - 2], xy[i] - c_hull[k - 1]) <= 0){ k--; } c_hull[k] = xy[i]; k++; } int lower_half = k; for(int i = N - 2; i >= 0; i--){ while(k > lower_half && cross(c_hull[k - 1] - c_hull[k - 2], xy[i] - c_hull[k - 1]) <= 0){ k--; } c_hull[k] = xy[i]; k++; } c_hull.resize(k - 1); } int main(){ int N; cin >> N; for(int i = 0; i < N; i++){ cin >> xy[i].first >> xy[i].second; x[i] = xy[i].first; y[i] = xy[i].second; } convex_hull(N); map <pair <long long, long long>, double> ans; if(c_hull.size() == 2){ ans[c_hull[0]] = 0.5; ans[c_hull[1]] = 0.5; } else { for(int i = 1; i < c_hull.size() + 1; i++){ double cos_value = (double) dot(c_hull[i % c_hull.size()] - c_hull[i - 1], c_hull[(i + 1) % c_hull.size()] - c_hull[i % c_hull.size()]) / norm(c_hull[i % c_hull.size()] - c_hull[i - 1]) / norm(c_hull[(i + 1) % c_hull.size()] - c_hull[i % c_hull.size()]); double prob = acos(cos_value) / (2 * M_PI); ans[c_hull[i % c_hull.size()]] = prob; } } for(int i = 0; i < N; i++){ if(ans.find(make_pair(x[i], y[i])) == ans.end()){ cout << fixed << setprecision(17) << 0.0 << endl; } else { cout << fixed << setprecision(17) << ans[make_pair(x[i], y[i])] << endl; } } return 0; }
1
// include // ------------------------------------------------ #include <bits/stdc++.h> #include <math.h> using namespace std; // func // ------------------------------------------------ int CalcSumOfDigit(int n); // 各桁の和を計算する。 int getDigit(int n); // 数字の桁数を取得する。 string upper(string str); // 英字を大文字に変換する。 string lower(string str); // 英字を小文字に変換する。 vector<pair<long long,long long>> prime_factorize(long long p); // 素因数分解 vector<pair<char, long long>> runLengthEncoding(string s); // ランレングス圧縮 // class // ------------------------------------------------ class Combi { public: Combi(); long long Combination(long long n, long long k); long long nPk_modp(long long n, long long k, long long p); private: map<long long, map<long long, long long>> memo; long long n_num; long long k_num; }; // define // ------------------------------------------------ #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(),(a).rend() #define sz(a) int((a).size()) #define rep(i,n) for(long long(i)=0;(i)<(n);(i)++) #define repe(i,n) for(long long(i)=0;(i)<=(n);(i)++) #define vsort(v) sort((v).begin(),(v).end()) #define rvsort(v) sort(rall((v))) #define vi vector<int> #define GCD(a,b) __gcd((a),(b)) #define LCM(a,b) (a)/GCD((a),(b))*(b) #define kiriage(a,b) ((a)+(b)-1)/(b) const int INF = 1e9; typedef long long ll; typedef unsigned long long ull; typedef vector<long> vll; // code // ------------------------------------------------ int main() { string s; cin >> s; if(s.length() != 2){ reverse(all(s)); } cout << s << endl; return 0; } // funcの実体 // ------------------------------------------------ int getDigit(int n) { int i = 1; while(1) { n = n / 10; if(n == 1) break; i++; } return i; } int CalcSumOfDigit(int n) { int s = 0; while(n) { s += n % 10; n = n / 10; } return s; } string upper(string str) { for(auto itr = str.begin();itr != str.end() ; itr++) { if(97 <= *itr && *itr <= 122) { *itr = *itr - 32; } } return str; } string lower(string str) { for(auto itr = str.begin();itr != str.end() ; itr++) { if(65 <= *itr && *itr <= 90) { *itr = *itr + 32; } } return str; } Combi::Combi() { n_num = -1; k_num = -1; }; ll Combi::Combination(ll n, ll k) { ll ret; if(memo[n][k] != 0) { ret = memo[n][k]; } else if(n == k || k == 0) { memo[n][k] = 1; ret = 1; } else { ret = Combination(n - 1, k - 1) + Combination(n - 1, k); memo[n][k] = ret; } return ret; } long long Combi::nPk_modp(long long n, long long k, long long p) { ll ans = 1; for(long long i = k; i <= n; i++) { ans = (ans * i) % p; } return ans; }; vector<pair<long long,long long>> prime_factorize(long long p) { vector<pair<long long,long long>> ret; for(long long x = 2; x * x <= p; ++x) { if(p % x != 0) continue; long long num = 0; while(p % x == 0) { num++; p /= x; } ret.push_back(make_pair(x,num)); } if(p != 1) ret.push_back(make_pair(p, 1)); return ret; } vector<pair<char, long long>> runLengthEncoding(string s){ vector<pair<char, long long>> ret; for(long long i = 0; i < s.length(); i++){ char cur = s[i]; long long cnt = 0; } return ret; }
#include <bits/stdc++.h> using namespace std; typedef pair<int,int> P; typedef long long ll; #define int ll main(){ string s; cin >> s; if(s.length()==2){ cout << s << endl; }else{ cout << s[2] << s[1] << s[0] << endl; } return 0; }
1
#include <iostream> #include <cstdio> #include <string> #include <algorithm> #include <utility> #include <cmath> #include <vector> #include <stack> #include <queue> #include <deque> #include <set> #include <map> #include <tuple> #include <numeric> #include <functional> using namespace std; typedef long long ll; typedef vector<ll> vl; typedef vector<vector<ll>> vvl; typedef pair<ll, ll> P; #define rep(i, n) for(ll i = 0; i < n; i++) #define exrep(i, a, b) for(ll i = a; i <= b; i++) #define out(x) cout << x << endl #define exout(x) printf("%.10f\n", x) #define chmax(x, y) x = max(x, y) #define chmin(x, y) x = min(x, y) #define all(a) a.begin(), a.end() #define rall(a) a.rbegin(), a.rend() #define pb push_back #define re0 return 0 const ll mod = 1000000007; const ll INF = 1e16; const double pi = acos(-1); struct Edge { ll to, cost; Edge(ll to, ll cost):to(to),cost(cost) {} }; struct Data { ll v, x; // いまいる頂点v, 進んだ距離x Data(ll v, ll x):v(v),x(x) {} bool operator<(const Data& hoge) const { return x > hoge.x; } }; int main() { ll n; cin >> n; vector<vector<Edge>> G(n); rep(i, n-1) { ll a, b, c; cin >> a >> b >> c; a--; b--; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } ll q, sv; cin >> q >> sv; sv--; vl dp(n, INF); priority_queue<Data> Q; auto push = [&](ll v, ll x) { if(dp[v] <= x) { return; } dp[v] = x; Q.emplace(v, x); }; // ダイクストラ法。計算量はO((V+E)*log(V)) push(sv, 0); while(!Q.empty()) { Data hoge = Q.top(); Q.pop(); ll v = hoge.v, x = hoge.x; if(dp[v] != x) { continue; } for(Edge e : G[v]) { push(e.to, x + e.cost); } } rep(i, q) { ll x, y; cin >> x >> y; x--; y--; out(dp[x] + dp[y]); } re0; }
/* * Since g++10 is released, some characters is not valid inside #if 0 :( * So, why not using clang++? :D * Date: echo -n ' ' && date +%Y.%m.%d # Just Run this (Type !!sh in Vim). * Solution: * 考虑边界,推导结论,子问题递归求解 如果 S <= p[1] 或者 S >= p[n] ,显然所有人投票都是一个方向,不妨假设 p[1] < S < p[n] 。 考虑处在最边上的 1 号点和 n 号点,如果 a[1] >= a[n] ,那么一定先到 1 ,否则先到 n 。归 纳证明,如果 n = 2 显然成立,对于 n >= 3 ,若 S >= a[n - 1] 显然成立,否则 S < a[n - 1] , 此时考虑 1 和 n - 1 ,如果先到 n - 1 就会回到 S >= a[n - 1] 的情况。 不妨假设 a[1] >= a[n] ,那么到 n 的时间就一定是到 1 的时间加上 1 到 n 的距离,那么 n 想 要最小化到达 n 的时间其实就是等价于最小化到达 1 的时间,可以赋值 a[1] = a[1] + a[n] 并赋 值 n = n - 1 转换为子问题,递归求解,可以求出每个点的到达时间。 * Digression: * CopyRight: ▁▃▄▄▄▃▃▃▃▄▶ ▗▇▀▔ ▔▔▔▔ ▄▛ ▃▅━━■▄▂ ▟▊ ▐▘ ▀▙ ▟▜▌ ▐▖ ▋ ▐▍ ▟▘ ▜ ▝▀▇▆●▘ ▐▌ ▗▟▘ ▜▃ ▁▅▛ ▔▀▼▅▄▃▃██▅▄▄▄▅■▀▔ ▔▔▔▔▔▔ */ #include <cstdio> #include <algorithm> #define debug(...) fprintf(stderr, __VA_ARGS__) typedef long long ll; struct { inline operator int () { int x; return scanf("%d", &x), x; } inline operator ll () { ll x; return scanf("%lld", &x), x; } template<class T> inline void operator () (T &x) { x = *this; } template<class T, class ...A> inline void operator () (T &x, A &...a) { x = *this; this -> operator () (a...); } } read; const int maxn = 1000005; int p[maxn]; ll a[maxn]; ll t[maxn]; void solve (int l, int r, int S) { if (S <= p[l]) { for (int i = l; i <= r; i ++) t[i] = p[i] - S; return; } if (S >= p[r]) { for (int i = l; i <= r; i ++) t[i] = S - p[i]; return; } if (a[l] >= a[r]) { a[l] += a[r]; solve(l, r - 1, S); t[r] = t[l] + p[r] - p[l]; } else { a[r] += a[l]; solve(l + 1, r, S); t[l] = t[r] + p[r] - p[l]; } } int main () { int n = read, S = read; for (int i = 1; i <= n; i ++) read(p[i], a[i]); solve(1, n, S); ll ans = 0; for (int i = 1; i <= n; i ++) ans = std::max(ans, t[i]); printf("%lld\n", ans); }
0
#include<cstdio> char s[1000005]; long long sumM[1000005]; long long solve(int n,int k){ long long res = 0; long long cnt = 0, sum = 0; for(int i = 1; i <= n; i++){ int delet = i-k; if(delet>=1 && s[delet]=='D'){ long long minus = sumM[i-1]-sumM[delet]; sum -= minus; cnt--; } if(s[i]=='D') cnt++; else if(s[i]=='M') sum += cnt; else if(s[i]=='C') res += sum; } return res; } int main(){ int n; scanf("%d%s",&n,s+1); sumM[0] = 1; for(int i = 1; i <= n; i++) sumM[i] = sumM[i-1]+(s[i]=='M'); int q; scanf("%d",&q); while(q--){ int k; scanf("%d",&k); printf("%lld\n",solve(n,k)); } return 0; }
#include "bits/stdc++.h" using namespace std; int main() { long long N, Q; string S; cin >> N >> S >> Q; for (int i = 0; i < Q; i++) { long long K; cin >> K; long long CurX = 0, CurY = 0, COUNT = 0, ANS = 0; vector<long long> MCOUNT(N, 0); for (int j = 0; j < N; j++) { if (S[j] == 'D') CurX++; if (S[j] == 'M') CurY++; if (S[j] == 'M') COUNT += CurX; if (j - K >= 0) { if (S[j - K] == 'M') CurY--; if (S[j - K] == 'D') COUNT -= CurY, CurX--; } if (S[j] == 'C') ANS += COUNT; } cout << ANS << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int N,M; cin >> N >> M; vector<bool>vec(N,true); vector<int>F(N); int WA=0,AC=0; for(int i=0; i<M; i++){ int p; string S; cin >> p >> S; if(vec[p-1] && S == "WA"){ WA++; F[p-1]++; } if(vec[p-1] && S == "AC"){ AC++; vec[p-1]=false; } } for(int i=0; i<N; i++){ if(vec[i]){ WA -= F[i]; } } cout << AC << " " << WA << endl; }
#include<bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(); int n, m; cin>>n>>m; if(n == 0 || m == 0){ cout<<"0 0"; return 0; } bool accepted[n]; unordered_map<int, int> ma; for(int i = 0; i < n; i++){ accepted[i] = false; } for(int i = 0; i < m; i++){ int p; string s; cin>>p>>s; p-=1; if(s == "AC"){ accepted[p] = true; }else{ if(!accepted[p]){ if(ma.find(p) == ma.end()){ ma[p] = 1; }else { ma[p]++; } } } } int correct = 0, penalties = 0; for(int i = 0; i < n; i++){ if(accepted[i]){ correct++; penalties += ma[i]; } } cout<<correct<<" "<<penalties; return 0; }
1
#include <cstdio> int N; int C[10000][2]; void solve() { int a = 0; int b = 0; for (int i = 0; i < N; i++) { int ac = C[i][0]; int bc = C[i][1]; if (ac > bc) { a += ac + bc; } else if (ac < bc) { b += ac + bc; } else { a += ac; b += bc; } } printf("%d %d\n", a, b); } int main() { for (;;) { scanf("%d", &N); if (N == 0) { break; } for (int i = 0; i < N; i++) { scanf("%d%d", &C[i][0], &C[i][1]); } solve(); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<ll> vint; typedef vector< vector<ll> > vvint; #define rep(i,n) for(ll i=0;i<n;i++) #define repf(i,f,n) for(ll i=f;i<n;i++) #define repr(i,n) for(ll i=n-1;i>=0;i--) #define mp make_pair #define mt make_tuple #define ALL(obj) (obj).begin(), (obj).end() 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 dy[]={0, 0, 1, -1, 1, -1, -1, 1}; int dx[]={1, -1, 0, 0, 1, 1, -1, -1}; template< typename T > struct BinaryIndexedTree { vector< T > data; BinaryIndexedTree(int sz) { data.assign(++sz, 0); } T sum(int k) { T ret = 0; for(++k; k > 0; k -= k & -k) ret += data[k]; return (ret); } void add(int k, T x) { for(++k; k < data.size(); k += k & -k) data[k] += x; } }; int main() { cout<<fixed<<setprecision(10); ll n; cin>>n; string s; cin>>s; ll q; cin>>q; vint ruiM(n+1, 0); rep(i,n) ruiM[i+1] = ruiM[i] + (ll)(s[i] == 'M'); while(q-->0){ ll k; cin>>k; ll left = -k; ll right = 0; vint cnt(2,0); ll ans = 0; while(right < n){ if(left >= 0){ if(s[left] == 'D'){ cnt[0]--; cnt[1] -= ruiM[right] - ruiM[left+1]; } } if(s[right] == 'D'){ cnt[0]++; }else if(s[right] == 'M'){ cnt[1] += cnt[0]; }else if(s[right] == 'C'){ ans += cnt[1]; } left++; right++; } cout<<ans<<endl; } return 0; }
0
#include <iostream> using namespace std; #include <stdio.h> void ushm(char x); struct sai { int dicet[3][2]; int diceus[2], dicemh[2]; int dicenow; }; struct sai a = { { 1, 6, 2, 5, 3, 4 },{ 5, 2 },{ 4, 3 }, 1 }; int main() { int atai[6], i; char houi[101]; for (i = 0; i < 6; i++) { cin >> atai[i]; } cin >> houi; for (i = 0; i <= 100 && houi[i] != '\0'; i++) { ushm(houi[i]); } printf("%d\n", atai[a.dicenow - 1]); return 0; } void ushm(char x) { int now = a.dicenow, nowt, i; for (i = 0; i < 3; i++) { if (a.dicet[i][0] == now) nowt = a.dicet[i][1]; else if (a.dicet[i][1] == now) nowt = a.dicet[i][0]; } switch (x) { case 'N': a.dicenow = a.diceus[1]; a.diceus[0] = now; a.diceus[1] = nowt; break; case 'S': a.dicenow = a.diceus[0]; a.diceus[1] = now; a.diceus[0] = nowt; break; case 'W': a.dicenow = a.dicemh[1]; a.dicemh[0] = now; a.dicemh[1] = nowt; break; case 'E': a.dicenow = a.dicemh[0]; a.dicemh[1] = now; a.dicemh[0] = nowt; break; } }
#include <iostream> #include <algorithm> #include <vector> #include <string> #include <array> using namespace std; #define DEBUG(x) cerr << #x << " = " << x << endl enum FACE { TOP, FRONT, RIGHT, LEFT, BACK, BOTTOM }; // 1..6 typedef array<int, 6> Dice; FACE tbl[6][4] = { { TOP, BACK, BOTTOM, FRONT }, { TOP, RIGHT, BOTTOM, LEFT }, { TOP, FRONT, BOTTOM, BACK }, { TOP, LEFT, BOTTOM, RIGHT }, { FRONT, RIGHT, BACK, LEFT }, { FRONT, LEFT, BACK, RIGHT } }; enum MOVE { TOP_TO_BACK, TOP_TO_RIGHT, TOP_TO_FRONT, TOP_TO_LEFT, FRONT_TO_RIGHT, FRONT_TO_LEFT }; void rotate(Dice &d, FACE t[4]) { int tmp = d[t[3]]; d[t[3]] = d[t[2]]; d[t[2]] = d[t[1]]; d[t[1]] = d[t[0]]; d[t[0]] = tmp; } vector<Dice> generate_all(Dice dice) { vector<Dice> res; for(int i = 0; i < 6; ++i) { for(int j = 0; j < 4; ++j) { res.emplace_back(dice); rotate(dice, tbl[FRONT_TO_RIGHT]); } if(i % 2 == 0) rotate(dice, tbl[TOP_TO_BACK]); else rotate(dice, tbl[TOP_TO_RIGHT]); } return res; } int main() { ios::sync_with_stdio(false); Dice d; cin >> d[TOP]; cin >> d[FRONT]; cin >> d[RIGHT]; cin >> d[LEFT]; cin >> d[BACK]; cin >> d[BOTTOM]; string s; cin >> s; for(char c : s) { if(c == 'S') { rotate(d, tbl[TOP_TO_FRONT]); } if(c == 'E') { rotate(d, tbl[TOP_TO_RIGHT]); } if(c == 'W') { rotate(d, tbl[TOP_TO_LEFT]); } if(c == 'N') { rotate(d, tbl[TOP_TO_BACK]); } } cout << d[TOP] << endl; }
1
#include <bits/stdc++.h> #define F first #define S second using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef priority_queue<int> HEAP; typedef priority_queue<int, vector<int>, greater<int> > RHEAP; const int N = 100010, M = 1010; string s; int main() { cin >> s; for (int i = 0; i < s.size() - 1; i ++ ) if (s[i] == s[i + 1]) { cout << i + 1 << ' ' << i + 2 << endl; return 0; } for (int i = 0; i < s.size() - 2; i ++ ) if (s[i] == s[i + 2]) { cout << i + 1 << ' ' << i + 3 << endl; return 0; } puts("-1 -1"); return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { string s; cin >> s; bool found = 0; int i, j; for (i = 0; i < (int)s.size() - 1; i++) { for (j = i + 1; j < (int)s.size(); j++) { if (s[i] == s[j]) { if (2 > (j - i + 1) / 2) found = 1; break; } } if (found) break; } if (found) cout << i + 1 << " " << j + 1 << endl; else cout << -1 << " " << -1 << endl; }
1
#include<bits/stdc++.h> using namespace std; using LL = long long; using ULL = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) int N; int A[3][100001]; LL dp[3][100001] = {}; int main() { scanf("%d", &N); rep(j, 3) { rep(i, N) scanf("%d", &A[j][i]); A[j][N] = 1000000001; } rep(j, 3) sort(A[j], A[j] + N); rep(i, N + 1) dp[0][i] = i; rep(j, 2) { int p = 0; rep(i, N) { while (A[j][p] < A[j + 1][i]) p++; dp[j + 1][i + 1] += dp[j + 1][i] + dp[j][p]; } } printf("%lld\n", dp[2][N]); return 0; }
#include <bits/stdc++.h> #define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define endl "\n" #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() using namespace std; using ll = long long; const double PI = 3.14159265358979; void solve() { int n; cin >> n; vector<ll> a(n); vector<ll> b(n); vector<ll> c(n); vector<ll> na(n, 0); vector<ll> nb(n, 0); for(int i = 0; i < n; ++i) cin >> a[i]; for(int i = 0; i < n; ++i) cin >> b[i]; for(int i = 0; i < n; ++i) cin >> c[i]; sort(ALL(b)); sort(ALL(c)); for(int i = 0; i < n; ++i) { auto itc = upper_bound(ALL(c), b[i]); nb[i] = distance(itc, c.end()); } for(int i = n - 2; i >= 0; --i) { nb[i] += nb[i + 1]; } for(int i = 0; i < n; ++i) { auto itb = upper_bound(ALL(b), a[i]); na[i] = nb[distance(b.begin(), itb)]; } cout << accumulate(ALL(na), 0LL); } int main() { fastio; solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(){ string N,m; cin >> N; int x,y = 0; for(int i = 0; i < int(N.size()); i++){ istringstream n; m = N[i]; n = istringstream(m); n >> x; y += x; } if (stoi(N) % y == 0){ cout << "Yes" << endl; } else{ cout << "No" << endl; } }
#include <bits/stdc++.h> using namespace std; int main () { int n; cin >> n; int a,b,c,d,e,f,g,h,i,s; a = n/100000000; b = n/10000000-10*a; c = n/1000000-100*a-10*b; d = n/100000-1000*a-100*b-10*c; e = n/10000-10000*a-1000*b-100*c-10*d; f = n/1000-100000*a-10000*b-1000*c-100*d-10*e; g = n/100-1000000*a-100000*b-10000*c-1000*d-100*e-10*f; h = n/10-10000000*a-1000000*b-100000*c-10000*d-1000*e-100*f-10*g; i = n-100000000*a-10000000*b-1000000*c-100000*d-10000*e-1000*f-100*g-10*h; s = a+b+c+d+e+f+g+h+i; if (n/s == (n+s-1)/s) { cout << "Yes" << endl; } else { cout << "No" << endl; } }
1
#include<bits/stdc++.h> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; typedef long long ll; int main(){ int h,w,a,b; cin >> h >> w >> a >> b; rep(i,h){ bool rev_fl = i<b; rep(j,w){ if(j<a){ if(rev_fl) cout << 1; else cout << 0; }else { if(rev_fl) cout << 0; else cout << 1; } } cout << endl; } return 0; }
#include<iostream> using namespace std; int main(){ int n; int a[1000000]; cin>>n; for(int i=0;i<n;i++){ cin>>a[i]; } for(int i=n-1;i>=0;i--){ cout<<a[i]; if(i>0){ cout<<" "; } } cout<<endl; return 0; }
0
#include<iostream> #include<string> #include<cstdio> #include<vector> #include<cmath> #include<algorithm> #include<functional> #include<iomanip> #include<queue> #include<ciso646> #include<random> #include<map> #include<set> #include<complex> #include<bitset> #include<stack> #include<unordered_map> #include<utility> using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair<int, int> P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i<n;i++) #define per(i,n) for(int i=n-1;i>=0;i--) #define Rep(i,sta,n) for(int i=sta;i<n;i++) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; typedef complex<ld> Point; const ld eps = 1e-2; const ld pi = acos(-1.0); typedef pair<ll, ll> LP; string s; int n; ll dp[1 << 18][8]; ll mod_pow(ll x, ll n) { ll res = 1; while (n) { if (n & 1)res = res * x%mod; x = x * x%mod; n >>= 1; } return res; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> s; n = s.length(); int d = (n - 3) / 2; if (n == 1) { if (s[0] == '0')cout << 0 << endl; else cout << 1 << endl; return 0; } int num = 0; rep(i, n) { if (s[i] == '?')num++; } int cnt = 0; rep(i, 8) { bool valid = true; rep(j, 3) { int t = 0; if (i&(1 << j))t = 1; if (t == 0 && s[2-j] == '1')valid = false; if (t == 1 && s[2-j] == '0')valid = false; } if (valid)dp[0][i] = 1; } ll ans = 0; rep(i, 3) { if (s[i] == '?')cnt++; } rep(i, d) { ans = (ans + dp[i][6] * mod_pow(2, num - cnt) % mod + dp[i][7] * mod_pow(2, num - cnt) % mod) % mod; char le = s[2 * i + 3], ri = s[2 * i + 4]; vector<int> v; if (le != '1'&&ri != '1')v.push_back(0); if (le != '1'&&ri != '0')v.push_back(1); if (le != '0'&&ri != '1')v.push_back(2); if (le != '0'&&ri != '0')v.push_back(3); int len = v.size(); rep(j, len) { dp[i + 1][v[j]] = (dp[i + 1][v[j]] + dp[i][0] + dp[i][1]+dp[i][2]) % mod; dp[i + 1][v[j] + 4] = (dp[i + 1][v[j] + 4] + dp[i][3] + dp[i][5]) % mod; dp[i + 1][4 + v[j] % 2] = (dp[i + 1][4 + v[j] % 2] + dp[i][4]) % mod; } if (le == '?')cnt++; if (ri == '?')cnt++; } ans = (ans + dp[d][3] + dp[d][5] + dp[d][6] + dp[d][7]) % mod; cout << ans << endl; //stop return 0; }
#include<bits/stdc++.h> using namespace std; const int mod=1e9+7; int add(int x,int y){return x+y<mod?x+y:x+y-mod;} void ade(int& x,int y){x+=y;if(x>=mod)x-=mod;} int n; char s[300300]; int tran[2][9]={{1,3,4,1,6,7,4,8,7},{2,0,5,1,2,5,4,5,7}}; int dp[300300][10]; int main() { scanf("%s",s+1); n=strlen(s+1); dp[0][0]=1; for(int i=0;i<n;++i) for(int j=0;j<=8;++j) { if(s[i+1]!='1')ade(dp[i+1][tran[0][j]],dp[i][j]); if(s[i+1]!='0')ade(dp[i+1][tran[1][j]],dp[i][j]); } printf("%d\n",add(dp[n][2],add(dp[n][5],add(dp[n][7],dp[n][8])))); return 0; }
1
#include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,l,r) for(int i=(int)(l);i<(int)(r);i++) #define all(x) (x).begin(),(x).end() #define sz(x) ((int)x.size()) template<class T>bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;} template<class T>bool chmin(T &a,T b){if(a>b){a=b;return 1;}return 0;} typedef pair<int, int> pii; typedef vector<int> vi; typedef vector<vi> vvi; const int inf = 1LL<<60; const int mod = 1e9 + 7; const double eps = 1e-9; /*{ }*/ signed main(){ int n; string s; cin >> n >> s; vi a(2*n); rep(i, 0, 2*n){ a[i] = ((s[i] == 'B') + (i%2 == 0)) % 2; } int ans = 1, cnt = 0; rep(i, 0, 2*n){ if(a[i]){ (ans *= cnt) %= mod; cnt--; }else{ cnt++; } } if(cnt) ans = 0; rep(i, 1, n+1) (ans *= i) %= mod; cout << ans << endl; // rep(i, 0, 2*n) cout << a[i] << " "; // cout << endl; return 0; }
#include <bits/stdc++.h> #define FOR(i,n) for(int (i)=0;(i)<(int)(n);++(i)) #define FORUL(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i)) #define FORULE(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i)) #define FOREACH(it, X) for(__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++) #if defined(_MSC_VER) || __cplusplus > 199711L #define AUTOVAR(newvar,v) auto newvar = (v) #else #define AUTOVAR(newvar,v) __typeof(newvar) r = (v) #endif #define PB(x) push_back(x) #define MP(x,y) make_pair((x),(y)) #define MEMSET(m,v) memset(m,v,sizeof(m)) typedef long long ll; typedef unsigned long long ull; #define DEBUG 0 #if DEBUG #define DOUT cout<<"["<<__LINE__<<"]:" #else #define DOUT 0 && cout #endif using namespace std; #define MAX_N (1000*100) #define MOD (ll(1E9+7)) int N; string S; int W,B; ll kai[MAX_N+5]; ll kaijo(int n) { DOUT<<"---- kaijo("<<n<<") ----"<<endl; if(kai[n]!=0) { return kai[n]; } ll i=n; for(;i>=1; i--) { if (kai[i]!=0) { break; } } ll ret = kai[i]; for(;i<n;i++) { DOUT<<"(i+1)* kai[i]=="<<((i+1)* kai[i])<<endl; kai[i+1] = ((i+1)* kai[i])%MOD; } return (kai[n])%MOD; } int main(int argc, char *argv[]) { kai[0]=1; kai[1]=1; ios::sync_with_stdio(false); cin >> N; cin >> S; FOR(n,2*N) { DOUT << "S["<<n<<"]=="<<S[n]<<endl; DOUT << "(2*N-n)%2=="<<(2*N-n)%2<<endl; if((2*N-n)%2==0) { if(S[n]=='B') { S[n]='W'; } else if(S[n]=='W') { S[n]='B'; } } DOUT << "S["<<n<<"]=="<<S[n]<<endl; } W=0; B=0; FOR(n,2*N) { DOUT << "S["<<n<<"]=="<<S[n]<<endl; if(S[n]=='B') { B++; } else if(S[n]=='W') { W++; } } if(B!=W) { cout << 0 << endl; return 0; } int count = 0; ll ans = 1; count = 0; FOR(n,2*N) { DOUT << "S["<<n<<"]=="<<S[n]<<", ans="<<ans<<", count=="<<count<<endl; if(S[n]=='B') { DOUT<<"ans*count="<<(ans*count)%MOD<<endl; ans = ans*count%MOD; count--; } else { count++; } DOUT << "ans=="<<ans<<", count=="<<count<<endl; } cout << ans*kaijo(N)%MOD << endl; DOUT << kaijo(1000*100) << endl; return 0; }
1
// lcmとか__builtin_popcountとかはg++ -std=c++17 default.cppみたいなかんじで // g++ hoge.cpp -std=c++17 -I . でコンパイルできる // -fsanitize=undefinedでオーバーフロー検出 #include <bits/stdc++.h> //#include <atcoder/all> #define mod 1000000007 #define INF LLONG_MAX #define ll long long #define ln cout<<endl #define Yes cout<<"Yes"<<endl #define NO cout<<"NO"<<endl #define YES cout<<"YES"<<endl #define No cout<<"No"<<endl #define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++) #define rep(i,n) REP(i,0,n) #define all(x) (x).begin(),(x).end() #define rall(x) (x).rbegin(),(x).rend() using namespace std; //using namespace atcoder; typedef pair<ll,ll> P; ll dx[4]={1,0,-1,0}; ll dy[4]={0,1,0,-1}; int main() { cin.tie(0); ios::sync_with_stdio(false); ll a,b,c,d,m,n,k,x,y,maxi=0,f=0,mini=INF,sum=0; string str; ll q; cin>>n>>q; cin>>str; P p[q]; rep(i,q){ cin>>a>>b; a--; b--; p[i]=P(a,b); } vector<ll> imos(n+1); rep(i,n-1){ if(i!=0)imos[i]=imos[i-1]; if(str.substr(i,2).compare("AC")==0) { imos[i]++; } //cout<<imos[i]<<" "; } a=0;b=0; rep(i,q){ if(p[i].first!=0)sum=imos[p[i].second-1]-imos[p[i].first-1]; else sum=imos[p[i].second-1]; cout<<sum<<endl; } return 0; }
/* confirm 0LL and 1LL confirm cornercases such as 0 confirm times of cin < 10^6 */ #include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using P = pair<ll, ll>; using Pld = pair<ld, ld>; using Vec = vector<ll>; using VecP = vector<P>; using VecB = vector<bool>; using VecC = vector<char>; using VecD = vector<ld>; using VecS = vector<string>; using Graph = vector<VecP>; template <typename T> using Vec1 = vector<T>; template <typename T> using Vec2 = vector<Vec1<T> >; #define REP(i, m, n) for(ll (i) = (m); (i) < (n); ++(i)) #define REPN(i, m, n) for(ll (i) = (m); (i) <= (n); ++(i)) #define REPR(i, m, n) for(ll (i) = (m)-1; (i) >= (n); --(i)) #define REPNR(i, m, n) for(ll (i) = (m); (i) >= (n); --(i)) #define rep(i, n) REP(i, 0, n) #define repn(i, n) REPN(i, 1, n) #define repr(i, n) REPR(i, n, 0) #define repnr(i, n) REPNR(i, n, 1) #define all(s) (s).begin(), (s).end() #define pb push_back #define fs first #define sc second 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;} template <typename T> ll pow2(const T n){return (1LL << n);} template <typename T> void cosp(const T n){cout << n << ' ';} void co(void){cout << '\n';} template <typename T> void co(const T n){cout << n << '\n';} template <typename T1, typename T2> void co(pair<T1, T2> p){cout << p.fs << ' ' << p.sc << '\n';} template <typename T> void co(const Vec1<T> &v){for(T i : v) cosp(i); co();} template <typename T> void co(initializer_list<T> v){for(T i : v) cosp(i); co();} template <typename T> void ce(const T n){cerr << n << endl;} void sonic(){ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);} void setp(const ll n){cout << fixed << setprecision(n);} constexpr ll INF = 1e9+1; constexpr ll LINF = 1e18+1; constexpr ll MOD = 1e9+7; //constexpr ll MOD = 998244353; constexpr ld PI = acos(-1); constexpr ld EPS = 1e-11; int main(void){ ll n; cin >> n; string s; cin >> s; ll q; cin >> q; rep(i, q){ ll k; cin >> k; ll ans = 0, sum = 0; ll cntD = 0, cntM = 0; rep(i, n){ if(i >= k){ if(s[i - k] == 'D'){ cntD--; sum -= cntM; }else if(s[i - k] == 'M') cntM--; } if(s[i] == 'D') cntD++; else if(s[i] == 'M'){ cntM++; sum += cntD; }else if(s[i] == 'C') ans += sum; } co(ans); } return 0; }
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; string S; cin >> N >> S; vector<string> ans = {"SS", "SW", "WS", "WW"}; rep(i, 4) { rep(j, N) { if (ans[i][j + 1] == 'S') { if (S[j] == 'o') { if (ans[i][j] == 'S') ans[i].push_back('S'); else ans[i].push_back('W'); } else { if (ans[i][j] == 'S') ans[i].push_back('W'); else ans[i].push_back('S'); } } else { if (S[j] == 'o') { if (ans[i][j] == 'S') ans[i].push_back('W'); else ans[i].push_back('S'); } else { if (ans[i][j] == 'S') ans[i].push_back('S'); else ans[i].push_back('W'); } } } if (ans[i].substr(0, 2) == ans[i].substr(N, 2)) { cout << ans[i].substr(1, N) << "\n"; return 0; } } cout << "-1\n"; }
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MOD=1000000007; #define INF 1LL<<30 #define rep(i,n) for (int i = 0; i < (n); ++i) #define all(x) (x).begin(),(x).end() int n; string s; bool judge(string ans){ bool ok=true; if(ans[n-1]=='S'){ if(s[n-1]=='o' && ans[n-2]!=ans[0]) ok=false; if(s[n-1]=='x' && ans[n-2]==ans[0]) ok=false; } else{ if(s[n-1]=='o' && ans[n-2]==ans[0]) ok=false; if(s[n-1]=='x' && ans[n-2]!=ans[0]) ok=false; } if(ans[0]=='S'){ if(s[0]=='o' && ans[n-1]!=ans[1]) ok=false; if(s[0]=='x' && ans[n-1]==ans[1]) ok=false; } else{ if(s[0]=='o' && ans[n-1]==ans[1]) ok=false; if(s[0]=='x' && ans[n-1]!=ans[1]) ok=false; } return ok; } int main(){ cin>>n>>s; string ans="SS"; for(int i=1;i<n-1;i++){ if(ans[i]=='S'){ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } else{ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } } else{ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } else{ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } } } if(judge(ans)){ cout<<ans<<endl; return 0; } ans="SW"; for(int i=1;i<n-1;i++){ if(ans[i]=='S'){ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } else{ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } } else{ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } else{ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } } } if(judge(ans)){ cout<<ans<<endl; return 0; } ans="WS"; for(int i=1;i<n-1;i++){ if(ans[i]=='S'){ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } else{ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } } else{ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } else{ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } } } if(judge(ans)){ cout<<ans<<endl; return 0; } ans="WW"; for(int i=1;i<n-1;i++){ if(ans[i]=='S'){ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } else{ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } } else{ if(s[i]=='o'){ if(ans[i-1]=='S') ans+='W'; else ans+='S'; } else{ if(ans[i-1]=='S') ans+='S'; else ans+='W'; } } } if(judge(ans)){ cout<<ans<<endl; return 0; } cout<<-1<<endl; }
1
#include <bits/stdc++.h> #define mod 1000000007 #define sp ' ' #define intmax 2147483647 #define llmax 9223372036854775807 #define nyan "(=^・ω・^=)" #define mkp make_pair #define mkt make_tuple #define lP pair<ll, ll> #define iP pair<int,int> typedef long long ll; using namespace std; int n, q, s, t, x, u; iP a[1<<18]; bool b; int update(int s, int t, int x, int k, int l, int r, int i) { if (t < l || r < s)return 0; else if (s <= l&&r <= t) { a[k] = mkp(x, i); return 0; } else { update(s, t, x, k * 2, l, (l + r) / 2, i); update(s, t, x, k * 2 + 1, (l + r) / 2 + 1, r, i); return 0; } } int find(int x) { int y, z = -2; x += u; while (x) { if (z < a[x].second) { y = a[x].first; z = a[x].second; } x /= 2; } return y; } int main(){ cin >> n >> q; for (int i = 1;; ++i) { if ((1 << i) / n >= 2) { u = (1 << i - 1); break; } } for (int i = 1; i != u * 2; ++i) a[i] = mkp(intmax, -1); for (int i = 0; i != q; ++i) { cin >> b >> s; if (b) { cout << find(s) << endl; } else { cin >> t >> x; update(s, t, x, 1, 0, u - 1, i); } } return 0; }
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define inf 10e17 #define rep(i,n) for(long long i=0; i<n; i++) #define repr(i,n,m) for(long long i=m; i<n; i++) #define mod 1000000007 #define sorti(x) sort(x.begin(), x.end()) #define sortd(x) sort(x.begin(), x.end(), std::greater<long long>()) #define debug(x) std::cerr << (x) << std::endl; #define roll(x) for (auto&& itr : x) { cerr << (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;} template <typename _Tp> struct LazySegmentTree { _Tp n; vector<_Tp> node, lazy; LazySegmentTree(vector<_Tp> const& v) { _Tp sz = v.size(); n = 1; while (sz > n) n *= 2; node.resize(2*n-1); lazy.resize(2*n-1, -1); for (int i = 0; i < sz; ++i) { node[i+n-1] = v[i]; } /* for (int i = n-2; i >= 0; --i) { node[i] = node[i*2+1] + node[i*2+2]; } */ } void eval(int k, int l, int r) { if (lazy[k] != -1) { node[k] = lazy[k]; // 自分自身に遅延評価を反映 // 最下段でないならば評価を伝搬させる。 if (r - l > 1) { lazy[2*k+1] = lazy[k]; lazy[2*k+2] = lazy[k]; } lazy[k] = -1; } } void update(int a, int b, _Tp value, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k,l,r); // 範囲外ならば切る。 if (b <= l || r <= a) { // cout << 1 << endl; return; } if (a <= l && r <= b) { lazy[k] = value; eval(k,l,r); return; } else { update(a,b,value,2*k+1,l,(r+l)/2); update(a,b,value,2*k+2,(l+r)/2,r); node[k] = value; } } _Tp get(int idx, int k = 0, int l = 0, int r = -1) { int a = idx, b = idx+1; if (r < 0) { r = n; } eval(k,l,r); // cout << a << " "<< b << " " << l << " " << r << endl; if (a <= l && r <= b) { return node[k]; } if (b <= l || r <= a) return -1; else { auto lv = get(idx, 2*k+1, l, (l+r)/2); auto rv = get(idx, 2*k+2, (l+r)/2, r); return max(lv, rv); } } }; ll fi = (1L << 31) - 1; int main() { int n, q; cin >> n >> q; vector<ll> v(n, fi); LazySegmentTree<ll> seg_tree(v); rep(i, q) { int com; cin >> com; if (com == 0) { int s,t,n; cin >> s >> t >> n; seg_tree.update(s,t+1,n); } else { int idx; cin >> idx; cout << seg_tree.get(idx) << "\n"; } } }
1
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { long long n, m; cin >> n >> m; for (long long i = 1; i < m + 1; i++) { if (i <= m - m / 2) { cout << i << " " << n + 1 - i << endl; } else { cout << n + 1 - i - (m - i + 1) * 2 << " " << n + 1 - i << endl; } } }
#include <bits/stdc++.h> //#pragma GCC optimize(2) #include<iostream> #include<cstdio> #include<algorithm> #include<string> #include<vector> #include<set> #include<map> #include<queue> #include<cmath> #include<cstring> #include<bitset> #include<stack> #include<time.h> #define X first #define Y second #define PB push_back #define MP make_pair #define scd(a) scanf("%d",&a) #define scdd(a,b) scanf("%d%d",&a,&b) #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c) #define ALL(x) x.begin(),x.end() #define sz(a) ((int)a.size()) #define getmid ((l+r)>>1) #define mst(var,val) memset(var,val,sizeof(var)) #define IOS ios::sync_with_stdio(false);cin.tie(0) #define lowbit(x) x&(-x) #define rep(i,n) for(int i=0;i<n;++i) #define rep1(i,n) for(int i=1;i<=n;++i) #define ls rt<<1 #define rs rt<<1|1 using namespace std; #ifdef local #define dbg(args...) cout << #args << " -> ", err(args); void err(){ cout << endl; } template<typename T, typename... Args> void err(T a, Args... args){ cout << a << ' '; err(args...); } #else #define dbg(args...) #endif // local typedef long long ll; typedef pair <int, int> pii; typedef pair <ll, ll> pll; typedef pair <ll, int> pli; typedef pair <double, double> pdd; const int inf=0x3f3f3f3f; const long long INF=0x3f3f3f3f3f3f3f3fLL; const double PI=acos(-1.0); const long double eps=1e-8; const int mod=1e9+7; const int maxn=3e5+100; const int N=1e6+1000; const int M=500+10; const ll mm=(1LL<<32); template <class T> inline void read(T &x) { x = 0; char c = getchar(); bool f = 0; for (; !isdigit(c); c = getchar()) f ^= c == '-'; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); x = f ? -x : x; } template <class T> inline void write(T x) { if (x < 0) { putchar('-'); x = -x; } T y = 1; int len = 1; for (; y <= x / 10; y *= 10) ++len; for (; len; --len, x %= y, y /= 10) putchar(x / y + 48); } ll qpow(ll a,ll b,ll mod) { ll ans=1; while(b) { if(b&1) ans=(ans*a)%mod; b>>=1; a=(a*a)%mod; } return ans; } struct node{ ll lazy[N<<2],mi[N<<2]; void pushdown(int rt) { if(lazy[rt]) { mi[ls]+=lazy[rt];mi[rs]+=lazy[rt]; lazy[ls]+=lazy[rt];lazy[rs]+=lazy[rt]; lazy[rt]=0; } } void pushup(int rt) { mi[rt]=min(mi[ls],mi[rs]); } void update(int rt,int l,int r,int L,int R,ll val) { if(L<=l&&r<=R) { lazy[rt]+=val; mi[rt]+=val; return ; } pushdown(rt); int mid=getmid; if(L<=mid) update(ls,l,mid,L,R,val); if(R>mid) update(rs,mid+1,r,L,R,val); pushup(rt); } ll query(int rt,int l,int r,int L,int R) { if(L<=l&&r<=R) return mi[rt]; pushdown(rt); int mid=getmid; ll res=INF; if(L<=mid) res=min(res,query(ls,l,mid,L,R)); if(R>mid) res=min(res,query(rs,mid+1,r,L,R)); return res; } }x,y; int A[N],B[N]; int main() { #ifdef local freopen("in.txt","r",stdin); #endif // local IOS;cout.tie(0); int H,W;cin>>H>>W; rep1(i,W) y.update(1,1,W,i,i,-i); rep1(i,H) cin>>A[i]>>B[i]; for(int i=1;i<=H;++i) { int r=B[i]+1; if(r<=W) { ll mi=y.query(1,1,W,1,B[i]); ll now=x.query(1,1,W,r,r); if(now>mi+r) { x.update(1,1,W,r,r,mi+r-now); y.update(1,1,W,r,r,mi-y.query(1,1,W,r,r)); } } x.update(1,1,W,A[i],B[i],inf); y.update(1,1,W,A[i],B[i],inf); //x.update(1,1,W,1,W,1); //y.update(1,1,W,1,W,1); ll ans=x.query(1,1,W,1,W); if(ans>=inf) cout<<-1<<"\n"; else cout<<ans+i<<"\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; #define all(v) v.begin(), v.end() const ll mod=1000000007; ll countBits(ll in){ int res=0; for(;in>0;in>>=1){ if((in&0x01)!=0){ res++; } } return res; } template<typename T> void show2dVector(vector<vector<T>>& v){ for(int i=0;i<v.size();i++){ int m=v[i].size(); cout<<i<<" : "; for(int j=0;i<m;i++){ cout<<v[i][j]<<" "; } cout<<endl; } } void bfs(const vector<vector<int>>&g, int v, vector<bool>& seen){ seen[v]=true; cout<<v<<" "; for(auto next: g[v]){ if(seen[next]){ continue; } bfs(g,next,seen); } } bool dfs(vector<vector<int>> &g,int start,int goal,vector<bool>&seen){ bool res=false; seen[start]=true; if(start==goal){ return true; } for(auto next: g[start]){ if(seen[next]){ continue; } res=dfs(g,next,goal,seen); if(res){ break; } } return res; } ll gcd(ll a,ll b) { return b? gcd(b,a%b): a; } ll lcm(ll a,ll b) { return a*b/gcd(a,b); } bool isLowerCase(char c){ return (c>='a'&&c<='z'); } char toLowerCase(char c){ if(isLowerCase(c)){ return c; }else{ return c+'a'-'A'; } } char toUpperCase(char c){ if(isLowerCase(c)){ return c-('a'-'A'); }else{ return c; } } ll powm(ll a,ll n, ll m){ ll ret=1; while(n>0){ if(n%2==1){ ret=(ret*a)%m; } n>>=1; a=(a*a)%m; } return ret; } const string yesno(bool ans){ return (ans?"Yes":"No"); } int main() { string n;cin>>n; if(n.size()==3){ char c=n[0]; n[0]=n[2]; n[2]=c; } cout<<n<<endl; return 0; }
#include <bits/stdc++.h> using namespace std; #define rep0(i, n) for (long long i = 0; i < (long long)(n); i++) #define rep1(i,n) for(long long i=1;i<=(long long)(n);i++) #define all(v) v.begin(),v.end() typedef long long ll; int main() { string S; char s; cin>>S; if(S.size()==2){cout<<S<<endl;} else{s=S[0]; S[0]=S[2]; S[2]=s; cout<<S<<endl;} }
1
#include <iostream> #include <cstdio> using namespace std; #define rep2(x,from,to) for(int x=(from);(x)<(to);(x)++) #define rep(x,to) rep2(x,0,to) typedef struct{ int head; int tail; int right; int left; int up; int down; } dice; dice s(dice a) { int buf; buf = a.tail; a.tail = a.down; a.down = a.head; a.head = a.up; a.up = buf; return a; } dice n(dice a) { return s(s(s(a))); } dice e(dice a) { int buf; buf = a.tail; a.tail = a.right; a.right = a.head; a.head = a.left; a.left = buf; return a; } dice w(dice a) { return e(e(e(a))); } dice rev(dice a) { return e(e(a)); } dice round(dice a) { int buf; buf = a.right; a.right = a.up; a.up = a.left; a.left = a.down; a.down = buf; return a; } bool same(dice a, dice b) { dice c = a; rep(i,4) { if(c.head == b.head && c.tail == b.tail && c.up == b.up && c.down == b.down && c.right == b.right && c.left == b.left) return true; c = round(c); } return false; } int main() { int k, a[105][6]; cin >> k; rep(i,k) { rep(j,6) { cin >> a[i][j]; } } rep(i,k) { rep2(j,i+1,k) { dice da, db; da.head = a[i][0]; da.down = a[i][1]; da.right = a[i][2]; da.left = a[i][3]; da.up = a[i][4]; da.tail = a[i][5]; db.head = a[i+j][0]; db.down = a[i+j][1]; db.right = a[i+j][2]; db.left = a[i+j][3]; db.up = a[i+j][4]; db.tail = a[i+j][5]; if(same(da,db) || same(s(da), db) || same(n(da), db) || same(e(da), db) || same(w(da), db) || same(rev(da), db)) { cout << "No" <<endl; return 0; } } } cout << "Yes" << endl; return 0; }
#include <cstdio> #include <iostream> #include <string> using namespace std; const int COUNT = 6; class Dice { public: int num[6]; public: Dice(); void play_dice(string); void play(int, int, int, int); void print_(int x) {printf("%d\n", num[x]);} void to_top_and_front(int, int); }; Dice::Dice() { for (int i = 0; i < COUNT; ++i) { cin >> num[i]; } } void Dice::play_dice(string s) { if(s == "E") {play(0, 2, 5, 3); } if(s == "N") {play(0, 4, 5, 1); } if(s == "S") {play(0, 1, 5, 4); } if(s == "W") {play(0, 3, 5, 2); } } void Dice::play(int a, int b, int c, int d) { int temp; temp = num[d]; num[d] = num[c]; num[c] = num[b]; num[b] = num[a]; num[a] = temp; } void Dice::to_top_and_front(int top, int front) { string str[7] = {"N", "N", "N", "W", "N", "N", "N"}; for (int i = 0; i < 7; ++i) { if(front == num[1]) break; play_dice(str[i]); } for (int k = 0; k < 4; k++) { if(top == num[0]) break; play_dice("W"); } } bool compare(Dice a, Dice b) { a.to_top_and_front(b.num[0], b.num[1]); for (int i = 0; i < COUNT; ++i) { if (a.num[i] != b.num[i]) { return false; } } return true; } int main(int argc, char const *argv[]) { Dice *cs; int times; cin >> times; cs = new Dice[times]; for (int i = 0; i < times; ++i) { for (int k = i+1; k < times; ++k) { if(compare(cs[i], cs[k])) { delete[] cs; printf("No\n"); return 0; } } } delete[] cs; printf("Yes\n"); return 0; }
1
//#include "debug.h" #include <string.h> #include <limits.h> #include <map> #include <set> #include <vector> #include <algorithm> using namespace std; const long long M=1000000007; //typedef long T; //#include "math/mod.h" //#include "math/modulo.h" template<class T, long long M> struct modulo { T n; T gcd_ext(T a, T b, T *x, T *y) { if (a == 0) { *x = 0; *y = 1; return b; } T x1, y1; T gcd = gcd_ext(b%a, a, &x1, &y1); *x = y1 - (b/a) * x1; *y = x1; return gcd; } modulo(T n): n(n<0? n%M+M: n%M) {} operator T() const { return n; } modulo operator-() { return modulo(-n); } modulo operator+(const modulo &o) { return modulo(n+o.n); } modulo operator-(const modulo &o) { return modulo(n-o.n); } modulo operator*(const modulo &o) { return modulo(n*o.n); } modulo operator/(const modulo &o) { T x, y; T g = gcd_ext(o.n, M, &x, &y); if (g != 1) return 0; else return modulo(n * (x % M)); } bool operator<(const modulo &o) const { return n<o.n; } bool operator<=(const modulo &o) const { return n<=o.n; } bool operator>(const modulo &o) const { return n>o.n; } bool operator>=(const modulo &o) const { return n>=o.n; } bool operator==(const modulo &o) const { return n==o.n; } bool operator!=(const modulo &o) const { return n!=o.n; } }; typedef modulo<long long, M> T; //#include "math/comb.h" void comb_inc_k(T &r, int n, int &k) { //C(n,k+1) = C(n,k) * (n-k) / (k+1) r = r * T(n-k) / T(k+1); k++; } void comb_dec_k(T &r, int n, int &k) { //C(n,k-1) = C(n,k) * k / (n-k+1) r = r * T(k) / T(n-k+1); k--; } void comb_inc_n(T &r, int &n, int k) { //C(n+1,k) = C(n,k) * (n+1) / (n+1-k) r = r * T(n+1) / T(n+1-k); n++; } void comb_dec_n(T &r, int &n, int k) { //C(n-1,k) = C(n,k) * (n-k) / n r = r * T(n-k) / T(n); n--; } T comb(int n, int k) { if (k*2 > n) k = n-k; if (n==0) return 0; T r = 1; //C(n,0) for (int i=0; i<k; ) comb_inc_k(r, n, i); return r; } int h,w,a,b; int input() { if (scanf("%d %d %d %d", &h, &w, &a, &b) < 0) return 0; return 1; } void init() { } int solve() { T r = comb(h+w-2, h-1); int xn = h-a+b-1, xk = b-1; T x = comb(xn, xk); int yn = a-1+w-b-1, yk = w-b-1; T y = comb(yn, yk); for (int i=0; i<a; i++) { //T x = mod_combinations(h-a+i+b-1, b-1, M); //T y = mod_combinations(a-1-i+w-b-1, w-b-1, M); if (i) { comb_inc_n(x, xn, xk); comb_dec_n(y, yn, yk); } r = r-x*y; } /* T r = mod_combinations(h+w-2, h-1, M); T x = mod_combinations(h-a+b-1, b-1, M); T y = mod_combinations(a-1+w-b-1, w-b-1, M); for (int i=0; i<a; i++) { //T x = mod_combinations(h-a+i+b-1, b-1, M); //T y = mod_combinations(a-1-i+w-b-1, w-b-1, M); if (i) { T n = h-a+i+b-1, m = b-1; x = mod_div(x*n, n-m, M); n = a-1-i+w-b-1, m = w-b-1; y = mod_div(y*(n+1-m), n+1, M); } T z = mod_mul(x, y, M); //printf("%d: r=%ld %ld %ld %ld\n", i, r, x, y, z); r = mod_sub(r, z, M); } */ return r; } void output(int ans) { printf("%d\n", ans); } void cleanup() { } int main() { //precalc(); //int ca; scanf("%d", &ca); while (input()) { init(); output(solve()); cleanup(); //break; } }
#include <bits/stdc++.h> #define ll long long #define pb push_back #define mp make_pair #define F first #define S second #define pii pair<int,int> #define pll pair<ll,ll> #define pcc pair<char,char> #define vi vector <int> #define vl vector <ll> #define sd(x) scanf("%d",&x) #define slld(x) scanf("%lld",&x) #define pd(x) printf("%d",x) #define plld(x) printf("%lld",x) #define pds(x) printf("%d ",x) #define pllds(x) printf("%lld ",x) #define pdn(x) printf("%d\n",x) #define plldn(x) printf("%lld\n",x) #define INF 2e9 #define INFLL 4e18 using namespace std; ll powmod(ll base,ll exponent,ll mod){ // with mod < 1e9 ll ans=1; while(exponent){ if(exponent&1)ans=(ans*base)%mod; base=(base*base)%mod; exponent/=2; } return ans; } ll gcd(ll a, ll b){ if(b==0) return a; else return gcd(b,a%b); } const int upperlimit = 2e5+100; const int mod = 1e9+7; ll fact[upperlimit]; ll invfact[upperlimit]; ll ways(int x1,int y1,int x2,int y2){ ll ans=fact[abs(x2-x1)+abs(y2-y1)]; ans*=invfact[abs(x2-x1)];ans%=mod; ans*=invfact[abs(y2-y1)];ans%=mod; return ans; } int main() { fact[0]=1;invfact[0]=1; for(int i = 1; i < upperlimit; i++){ fact[i]=fact[i-1];fact[i]*=i;fact[i]%=mod; invfact[i]=powmod(fact[i],mod-2,mod); } int h,w,r,c; sd(h);sd(w);sd(r);sd(c); ll ans=ways(0,0,h-1,w-1); for(int i = 0; i < c; i++){ ll calc=ways(0,0,h-r-1,i); calc*=ways(h-r,i,h-1,w-1);calc%=mod; ans-=calc; if(ans<0) ans+=mod; } plld(ans); return 0; }
1
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ ll N, M; cin >> N >> M; vector<ll> L(M+1); vector<ll> R(M+1); for(int i = 1; i <= M; ++i) cin >> L[i] >> R[i]; if(*min_element(R.begin()+1, R.end())-*max_element(L.begin()+1, L.end()) >= 0)cout << *min_element(R.begin()+1, R.end())-*max_element(L.begin()+1, L.end()) +1 << endl; else cout << 0 << endl; return 0; }
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } #define N 300010 #define ll long long int n,m,p[N],fa[N],deep[N],degree[N],q[N],t,root; bool flag[N]; struct data{int to,nxt; }edge[N]; void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;} void topsort() { int head=0,tail=0; for (int i=1;i<=n;i++) if (!degree[i]) q[++tail]=i; while (tail<n) { int x=q[++head]; for (int i=p[x];i;i=edge[i].nxt) { degree[edge[i].to]--; if (deep[x]+1>deep[edge[i].to]) { deep[edge[i].to]=deep[x]+1; fa[edge[i].to]=x; } if (!degree[edge[i].to]) q[++tail]=edge[i].to; } } } int main() { n=read(),m=read(); for (int i=1;i<n+m;i++) { int x=read(),y=read(); addedge(x,y);degree[y]++; } topsort(); for (int i=1;i<=n;i++) printf("%d\n",fa[i]); return 0; }
0