我竟然每道題都能想出來
雖然不是每道題都能寫對,debug了好久/facepalmnode
排序後前N/2個數的中位數時排序後第N/2 + 1的數
其他的中位數都是排序後第N / 2的數ios
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <cstring> #include <string> #include <ctime> #include <algorithm> #include <map> #define MAXN 200005 #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define ba 823 #define mo 974711 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int N; int id[MAXN],X[MAXN],B[MAXN]; bool cmp(int a,int b) { return X[a] < X[b]; } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif scanf("%d",&N); for(int i = 1 ; i <= N ; ++i) { scanf("%d",&X[i]); id[i] = i; } sort(id + 1,id + N + 1,cmp); int T = N / 2; for(int i = 1 ; i <= T ; ++i) { B[id[i]] = X[id[T + 1]]; } for(int i = T + 1 ; i <= N ; ++i) { B[id[i]] = X[id[T]]; } for(int i = 1 ; i <= N ; ++i) { printf("%d\n",B[i]); } return 0; }
N取最大的,R取最接近N/2的數組
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <cstring> #include <ctime> #include <map> #include <algorithm> #include <cmath> #define MAXN 200005 #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define ba 823 #define mo 974711 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int N,A[100005]; int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif ios::sync_with_stdio(false); scanf("%d",&N); for(int i = 1 ; i <= N ; ++i) scanf("%d",&A[i]); sort(A + 1,A + N + 1); printf("%d ",A[N]); double T = A[N] / 2.0; int t = 1; for(int i = 2 ; i <= N ; ++i) { if(fabs(A[i] - T) < fabs(A[t] - T)) t = i; } printf("%d\n",A[t]); return 0; }
事實上我想的優化不用也是個奇快無比的搜索
複雜度是啥不知道,反正1ms
發現兩行能匹配上必定是兩行的字符集是相同的
兩列能匹配上必定是兩列的字符集相同
咱們枚舉某行做爲第一行,而後找它字符集相同的一行做爲最後一行,枚舉匹配,而後每一列都固定了,最後給剩下的N - 2行找匹配就行
匹配條件是某一行正着讀等於另外一行反着讀
特判奇數行奇數列和H = 1,W = 1優化
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <cstring> #include <ctime> #include <map> #include <algorithm> #include <cmath> #define MAXN 200005 #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define ba 823 #define ha 99994711 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int H,W,St; int Row[14][26],Col[14][26]; int matR[14][14],matC[14][14],idC[14],idR[14],mat[14]; bool vis[14],used[14]; char S[14][14]; void swapRow(int x,int y) { if(x == y) return; swap(idR[x],idR[y]); for(int i = 1 ; i <= W ; ++i) swap(S[x][i],S[y][i]); } void swapCol(int x,int y) { if(x == y) return; swap(idC[x],idC[y]); for(int i = 1 ; i <= H ; ++i) swap(S[i][x],S[i][y]); } bool match(int c) { if(c == 0) { memset(mat,0,sizeof(mat)); int cnt = 0; for(int i = 2 ; i <= H - 1; ++i) { if(mat[i]) continue; for(int j = i + 1 ; j <= H - 1; ++j) { if(mat[j]) continue; bool f = 1; for(int k = 1 ; k <= W ; ++k) { if(S[i][k] != S[j][W - k + 1]) { f = 0; break; } } if(f) {mat[i] = j;mat[j] = i;break;} } if(!mat[i] && cnt < 1 && (H & 1)) { ++cnt; for(int k = 1 ; k <= W ; ++k) { if(S[i][k] != S[i][W - k + 1]) return 0; } mat[i] = i; } else if(!mat[i]) return 0; } return 1; } if(c == W - c + 1) { for(int i = 1 ; i <= W ; ++i) { if(vis[i]) continue; if(S[1][i] == S[H][i]) { swapCol(i,c); vis[c] = 1; if(match(c - 1)) return 1; vis[c] = 0; swapCol(i,c); } } } else { for(int i = 1; i <= W; ++i) { if(vis[i]) continue; if(matC[idC[c]][idC[i]]) { if(S[1][c] == S[H][i] && S[1][i] == S[H][c]) { vis[c] = vis[W - c + 1] = 1; swapCol(i,W - c + 1); if(match(c - 1)) return 1; swapCol(i,W - c + 1); vis[c] = vis[W - c + 1] = 0; } } } } return 0; } bool check() { for(int i = 2 ; i <= H ; ++i) { if(matR[idR[1]][idR[i]]) { if(used[idR[i]]) continue; swapRow(i,H); if(match((W & 1) ? W / 2 + 1 : W / 2)) return 1; swapRow(i,H); } } return 0; } void Solve() { scanf("%d%d",&H,&W); for(int i = 1 ; i <= H ; ++i) scanf("%s",S[i] + 1); for(int i = 1 ; i <= H ; ++i) { for(int j = 1 ; j <= W ; ++j) { Row[i][S[i][j] - 'a']++; } } for(int j = 1 ; j <= W ; ++j) { for(int i = 1 ; i <= H ; ++i) { Col[j][S[i][j] - 'a']++; } } if(H == 1) { int cnt = 0; for(int i = 0 ; i <= 25 ; ++i) { if(Row[1][i] & 1) ++cnt; } if(cnt > 1) puts("NO"); else puts("YES"); return; } else if(W == 1) { int cnt = 0; for(int i = 0 ; i <= 25 ; ++i) { if(Col[1][i] & 1) ++cnt; } if(cnt > 1) puts("NO"); else puts("YES"); return; } for(int i = 1 ; i <= H ; ++i) { for(int j = i ; j <= H ; ++j) { bool f = 1; for(int k = 0 ; k <= 25 ; ++k) { if(Row[i][k] != Row[j][k]) {f = 0;break;} } matR[i][j] = matR[j][i] = f; } } for(int i = 1 ; i <= W ; ++i) { for(int j = i ; j <= W ; ++j) { bool f = 1; for(int k = 0 ; k <= 25 ; ++k) { if(Col[i][k] != Col[j][k]) {f = 0;break;} } matC[i][j] = matC[j][i] = f; } } for(int i = 1 ; i <= H ; ++i) idR[i] = i; for(int i = 1 ; i <= W ; ++i) idC[i] = i; for(int i = 1 ; i <= H ; ++i) { used[i] = 1; swapRow(i,1); if(check()) {puts("YES");return;} swapRow(i,1); } puts("NO"); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }
合法的只有一條鏈上長葉子
把葉子刪掉,搜出一條鏈,鏈的兩端若是有葉子就長出來
而後用一個數組記錄鏈上每一個點長的葉子的個數
而後從左右開始比較,若是左邊小於右邊就跳出,若是右邊小於左邊就翻轉後跳出
構造的時候一個長了k個葉子的點排序就是 p + 1,p + 2...p + k, p
代碼能力不行,跪了三次QAQspa
#include <iostream> #include <cstdio> #include <vector> #include <set> #include <cstring> #include <ctime> #include <map> #include <algorithm> #include <cmath> #define MAXN 100005 #define pii pair<int,int> #define pb push_back #define mp make_pair #define fi first #define se second #define ba 823 #define ha 99994711 //#define ivorysi using namespace std; typedef long long int64; typedef double db; int N; struct node { int to,next; }E[MAXN * 2]; int sumE,head[MAXN],ind[MAXN],fa[MAXN],st,ed,D[MAXN]; int line[MAXN],tot; void add(int u,int v) { E[++sumE].to = v; E[sumE].next = head[u]; head[u] = sumE; } void dfs(int u) { ed = u; for(int i = head[u] ; i ; i = E[i].next) { int v = E[i].to; if(v != fa[u] && ind[v] != -1) { fa[v] = u; dfs(v); } } } void Solve() { scanf("%d",&N); int u,v; for(int i = 1 ; i < N ; ++i) { scanf("%d%d",&u,&v); add(u,v);add(v,u); ind[u]++;ind[v]++; } if(N == 2) { puts("1 2");return; } memcpy(D,ind,sizeof(ind)); for(int i = 1 ; i <= N ; ++i) { if(ind[i] == 1) { for(int j = head[i] ; j ; j = E[j].next) { int v = E[j].to; D[v]--; } D[i] = -1; } } memcpy(ind,D,sizeof(ind)); for(int i = 1 ; i <= N ; ++i) { if(ind[i] > 2) {puts("-1");return;} } for(int i = 1 ; i <= N ; ++i) { if(ind[i] == 1) {st = i;dfs(st);break;} } if(!st) { for(int i = 1 ; i <= N ; ++i) { if(ind[i] == 0) {st = i;dfs(st);break;} } } for(int i = head[ed] ; i ; i = E[i].next) { int v = E[i].to; if(v != fa[ed]) { fa[v] = ed; ed = v; break; } } for(int i = head[st] ; i ; i = E[i].next) { int v = E[i].to; if(!fa[v]) { fa[st] = v; st = v; break; } } ind[ed] = ind[st] = 1; int p = ed; while(p) { ++tot; for(int i = head[p] ; i ; i = E[i].next) { int v = E[i].to; if(ind[v] == -1) { line[tot]++; } } p = fa[p]; } int L = 1,R = tot; while(L <= tot && R >= 1) { if(line[L] < line[R]) break; else if(line[L] > line[R]) {reverse(line + 1,line + tot + 1);break;} ++L;--R; } p = 2; printf("%d",1); for(int i = 2 ; i <= tot ; ++i) { for(int j = p + 1 ; j <= p + line[i] ; ++j) { printf(" %d",j); } printf(" %d",p); p = p + line[i] + 1; } putchar('\n'); } int main() { #ifdef ivorysi freopen("f1.in","r",stdin); #endif Solve(); return 0; }