公平分队
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; int cmp(const void * x, const void * y) { #define datatype int datatype dx = *((datatype *)(x)), dy = *((datatype *)(y)); //x < y return dx < dy ? -1 : 1; #undef datatype } int a[200005]; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif std::ios::sync_with_stdio(0), cin.tie(0); int n; cin >> n; for (int i = 0; i < 2 * n; i++) cin >> a[i]; sort(a, a + 2 * n); lint ans = 0; for (int i = 1; i < n; i++) ans += a[i]; ans += a[2 * n - 1]; cout << ans << endl; return 0; }
XY游戏
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; int cmp(const void * x, const void * y) { #define datatype int datatype dx = *((datatype *)(x)), dy = *((datatype *)(y)); //x < y return dx < dy ? -1 : 1; #undef datatype } map<int, int> mp; struct state { int a[4][4]; int step; }; queue<state> q; int hash_state(state st) { int rtn = 0; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) rtn = rtn * 3 + st.a[i][j]; } return rtn; } const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, 1, 0, -1}; int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif std::ios::sync_with_stdio(0), cin.tie(0); state initial, st, stt; char ch; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { cin >> ch; while (ch != 'O' && ch != 'X' && ch != 'Y') cin >> ch; if (ch == 'O') initial.a[i][j] = 0; else if (ch == 'X') initial.a[i][j] = 1; else initial.a[i][j] = 2; } } initial.step = 0; q.push(initial); mp[hash_state(initial)]++; while (!q.empty()) { st = q.front(); q.pop(); bool ok = false; for (int i = 0; i < 4; i++) { if (st.a[i][0] == 1 && st.a[i][1] == 1 && st.a[i][2] == 1 && st.a[i][3] == 1) ok = true; if (st.a[i][0] == 2 && st.a[i][1] == 2 && st.a[i][2] == 2 && st.a[i][3] == 2) ok = true; if (st.a[0][i] == 1 && st.a[1][i] == 1 && st.a[2][i] == 1 && st.a[3][i] == 1) ok = true; if (st.a[0][i] == 2 && st.a[1][i] == 2 && st.a[2][i] == 2 && st.a[3][i] == 2) ok = true; if (st.a[0][0] == 1 && st.a[1][1] == 1 && st.a[2][2] == 1 && st.a[3][3] == 1) ok = true; if (st.a[0][0] == 2 && st.a[1][1] == 2 && st.a[2][2] == 2 && st.a[3][3] == 2) ok = true; if (st.a[0][3] == 1 && st.a[1][2] == 1 && st.a[2][1] == 1 && st.a[3][0] == 1) ok = true; if (st.a[0][3] == 2 && st.a[1][2] == 2 && st.a[2][1] == 2 && st.a[3][0] == 2) ok = true; } if (ok) { cout << st.step << endl; return 0; } stt = st; stt.step = st.step + 1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (st.a[i][j] == 0) continue; for (int k = 0; k < 4; k++) { if (0 <= i + dx[k] && i + dx[k] < 4 && 0 <= j + dy[k] && j + dy[k] < 4 && st.a[i + dx[k]][j + dy[k]] == 0) { stt.a[i + dx[k]][j + dy[k]] = stt.a[i][j]; stt.a[i][j] = 0; int h = hash_state(stt); if (mp.find(h) == mp.end()) { mp[h]++; q.push(stt); } stt.a[i][j] = stt.a[i + dx[k]][j + dy[k]]; stt.a[i + dx[k]][j + dy[k]] = 0; } } } } } cout << -1 << endl; return 0; }
第K小最简真分数
#pragma comment(linker, "/STACK:102400000,102400000") #include<stdio.h> #include<string.h> #include<stdlib.h> #include<vector> #include<algorithm> #include<iostream> #include<map> #include<queue> #include<stack> #include<string> #include<functional> //#include<bits/stdc++.h> using namespace std; typedef long long lint; typedef vector<int> VI; typedef pair<int, int> PII; int cmp(const void * x, const void * y) { #define datatype int datatype dx = *((datatype *)(x)), dy = *((datatype *)(y)); //x < y return dx < dy ? -1 : 1; #undef datatype } lint p[100]; int m = 0; lint calc(lint x) { lint rtn = x; for (int i = 1; i < (1 << m); i++) { int cnt = 0, mlt = 1; for (int j = 0; j < m; j++) { if (i & (1 << j)) { cnt++; mlt *= p[j]; } } if (cnt & 1) rtn -= x / mlt; else rtn += x / mlt; } return rtn; } int main() { #ifndef ONLINE_JUDGE freopen("input.txt", "r", stdin); #endif std::ios::sync_with_stdio(0), cin.tie(0); lint n, k; cin >> n >> k; lint l = 1, r = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { p[m++] = i; while (n % i == 0) n /= i; } } if (n != 1) p[m++] = n; while (l < r) { lint mid = (l + r) >> 1; lint fk = calc(mid); if (fk < k) l = mid + 1; else r = mid; } cout << r << endl; return 0; }
前缀后缀查询
#include <bits/stdc++.h> using namespace std; #define pii pair<int,int> #define vi vector<int> #define pb push_back #define mp make_pair #define x first #define y second #define SZ(x) x.size() #define all(x) x.begin(),x.end() #define rep(i,a,b) for(int i=a;i<b;i++) #define per(i,a,b) for(int i=b-1;i>=a;i--) #define DBG(x) cerr<<(#x)<<"="<<x<<" "; #define inf 1000000007 #define mod 1000000007 #define ll long long #define N 100010 template<class T,class U> void Max(T &a,U b){if(a<b)a=b;} template<class T,class U> void Min(T &a,U b){if(a>b)a=b;} template<class T,class U> void add(T &a,U b){a+=b;if(a>=mod)a-=mod;} template<class T,class U> int min(T a,U b){return a>=b?b:a;} int a[12],b[12],sz; char s[12]; int ch[1000007][26]; void add(char s[],int a[]){ int n=strlen(s),p=0; rep(i,0,n){ int j=s[i]-'a'; if(!ch[p][j])ch[p][j]=++sz; p=ch[p][j]; a[i+1]=p; } } int find(string s){ int p=0; rep(i,0,SZ(s)){ int j=s[i]-'a'; if(!ch[p][j])return -1; p=ch[p][j]; } return p; } int main(){ int i,j,k,ca=0,T,n,m,K; scanf("%d%d",&n,&K); unordered_map<ll,int>g; int w;sz=0; rep(i,0,n){ scanf("%s%d",s,&w); m=strlen(s); add(s,a); reverse(s,s+m); add(s,b); rep(j,0,m+1){ rep(k,0,m+1){ ll x=a[j]*1000007LL+b[k]; if(!g.count(x))g[x]=w; else if(w>g[x])g[x]=w; } } } string s,t; while(K--){ cin>>s>>t; reverse(all(t)); int ans=-1; int x=find(s),y=find(t); if(x!=-1&&y!=-1){ ll w=x*1000007LL+y; if(g.count(w))ans=g[w]; } printf("%d ",ans); } }