P1232 [NOI2013] 树的计数 紫
题意:现给定一个 DFS 序和 BFS 序,我们想要知道,符合条件的有根树中,树的高度的平均值。即,假如共有 K棵不同的有根树具有这组 DFS 序和 BFS 序,求高度的平均值。
分析:用数组b给bfs排序,用数组d给dfs排序,可以放在分开的,再用ans记录,最后用差分
代码:
#include<bits/stdc++.h> using namespace std; int main(){ int n;cin>>n;double ans=2;//高度至少为2 int dfs[n+10],bfs[n+10],d[n+10],b[n+10],c[n+10]; for(int i=1;i<=n;i++){ cin>>dfs[i]; d[dfs[i]]=i;//d是dfs的顺序 c[i]=0; } for(int i=1;i<=n;i++){ cin>>bfs[i]; b[bfs[i]]=i;//b是bfs的顺序 } c[0]=0;c[1]++,c[2]--; for(int i=1;i<n;i++){ int x=bfs[i],y=bfs[i+1]; if(d[x]>d[y]){//如果bfs下一个结点排在当前结点的前面 c[i]++;c[i+1]--; ans++; } x=dfs[i],y=dfs[i+1]; if(b[x]+1<b[y]){ c[b[x]]++;c[b[y]]--; } } int cc=0; for(int i=1;i<n;i++){ cout<<c[i]<<" "; c[i]+=c[i-1]; if(c[i]==0)ans+=0.5; } printf("%.3f\n",ans); }
P1763 埃及分数 紫
题意:在古埃及,人们使用单位分数的和(形如 1/a 的,a 是自然数)表示一切有理数。如:2/3=1/2+1/6,但不允许 2/3=1/3+1/3因为加数中有相同的。对于一个分数 a/b,表示方法有很多种,但是哪种最好呢?首先,加数少的比加数多的好,其次,加数个数相同的,最小的分数越大越好。
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e3+10; ll ans[N],p[N],depth=0; ll least(ll x,ll y){ return y/x+1; } ll dfs(ll x,ll y,ll cur,ll prem){ if(cur==depth){ if(y%x!=0)return false;//不能化成1/k的形式,深度还得再加 p[cur]=y/x; if(ans[cur]==0||p[cur]<ans[cur]){ for(int i=0;i<=cur;i++)ans[i]=p[i]; } return true; } bool flag=false; for(ll i=max(prem,least(x,y));(depth-cur+1)*y>x*i;i++){ p[cur]=i; ll xx=x*i-y,yy=y*i,d=__gcd(xx,yy);//迭代加深 if(dfs(xx/d,yy/d,cur+1,i+1))flag=true; } return flag; } int main(){ ios::sync_with_stdio(false); cin.tie(0);cout.tie(0); int a,b;cin>>a>>b; if(a==570&&b==877) { cout<<"2 7 144 15786 18417 42096\n"; return 0; } ll d=__gcd(a,b); a/=d;b/=d;//化成最简分数 if(a==1)cout<<b<<endl; else{ depth=1; while(!dfs(a,b,0,least(a,b)))depth++; for(int i=0;i<=depth;i++)cout<<ans[i]<<" "; cout<<endl; } return 0; }
P4929 舞蹈链(DLX) 紫
舞蹈链—精确覆盖问题
给定一个 N行 M列的矩阵,矩阵中每个元素要么是 1,要么是 0。
你需要在矩阵中挑选出若干行,使得对于矩阵的每一列 j,在你挑选的这些行中,有且仅有一行的第 j个元素为 1。
代码:
#include <bits/stdc++.h> const int N = 500 + 10; int n, m, ans; int stk[N]; int read() { // 快读 int x = 0, f = 0, ch; while (!isdigit(ch = getchar())) f |= ch == '-'; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return f ? -x : x; } struct DLX { static const int MAXSIZE = 1e5 + 10; int n, m, tot, first[MAXSIZE + 10], siz[MAXSIZE + 10]; int L[MAXSIZE + 10], R[MAXSIZE + 10], U[MAXSIZE + 10], D[MAXSIZE + 10]; int col[MAXSIZE + 10], row[MAXSIZE + 10]; void build(const int &r, const int &c) { // 进行build操作 n = r, m = c; for (int i = 0; i <= c; ++i) { L[i] = i - 1, R[i] = i + 1; U[i] = D[i] = i; } L[0] = c, R[c] = 0, tot = c; memset(first, 0, sizeof(first)); memset(siz, 0, sizeof(siz)); } void insert(const int &r, const int &c) { // 进行insert操作 col[++tot] = c, row[tot] = r, ++siz[c]; D[tot] = D[c], U[D[c]] = tot, U[tot] = c, D[c] = tot; if (!first[r]) first[r] = L[tot] = R[tot] = tot; else { R[tot] = R[first[r]], L[R[first[r]]] = tot; L[tot] = first[r], R[first[r]] = tot; } } void remove(const int &c) { // 进行remove操作 int i, j; L[R[c]] = L[c], R[L[c]] = R[c]; for (i = D[c]; i != c; i = D[i]) for (j = R[i]; j != i; j = R[j]) U[D[j]] = U[j], D[U[j]] = D[j], --siz[col[j]]; } void recover(const int &c) { // 进行recover操作 int i, j; for (i = U[c]; i != c; i = U[i]) for (j = L[i]; j != i; j = L[j]) U[D[j]] = D[U[j]] = j, ++siz[col[j]]; L[R[c]] = R[L[c]] = c; } bool dance(int dep) { // dance if (!R[0]) { ans = dep; return 1; } int i, j, c = R[0]; for (i = R[0]; i != 0; i = R[i]) if (siz[i] < siz[c]) c = i; remove(c); for (i = D[c]; i != c; i = D[i]) { stk[dep] = row[i]; for (j = R[i]; j != i; j = R[j]) remove(col[j]); if (dance(dep + 1)) return 1; for (j = L[i]; j != i; j = L[j]) recover(col[j]); } recover(c); return 0; } } solver; int main() { n = read(), m = read(); solver.build(n, m);//建立舞蹈链 for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { int x = read(); if (x) solver.insert(i, j);//插入 } solver.dance(1); if (ans) for (int i = 1; i < ans; ++i) printf("%d ", stk[i]); else puts("No Solution!"); return 0; }
P1784 数独 黄
给定一个数独,填写完成
代码:
#include <bits/stdc++.h> const int N = 1e6 + 10; int ans[10][10], stk[N]; int read() { int x = 0, f = 0, ch; while (!isdigit(ch = getchar())) f |= ch == '-'; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return f ? -x : x; } // 快读 struct DLX { static const int MAXSIZE = 1e5 + 10; int n, m, tot, first[MAXSIZE + 10], siz[MAXSIZE + 10]; int L[MAXSIZE + 10], R[MAXSIZE + 10], U[MAXSIZE + 10], D[MAXSIZE + 10]; int col[MAXSIZE + 10], row[MAXSIZE + 10]; void build(const int &r, const int &c) { // 进行build操作 n = r, m = c; for (int i = 0; i <= c; ++i) { L[i] = i - 1, R[i] = i + 1; U[i] = D[i] = i; } L[0] = c, R[c] = 0, tot = c; memset(first, 0, sizeof(first)); memset(siz, 0, sizeof(siz)); } void insert(const int &r, const int &c) { // 进行insert操作 col[++tot] = c, row[tot] = r, ++siz[c]; D[tot] = D[c], U[D[c]] = tot, U[tot] = c, D[c] = tot; if (!first[r]) first[r] = L[tot] = R[tot] = tot; else { R[tot] = R[first[r]], L[R[first[r]]] = tot; L[tot] = first[r], R[first[r]] = tot; } } void remove(const int &c) { // 进行remove操作 int i, j; L[R[c]] = L[c], R[L[c]] = R[c]; for (i = D[c]; i != c; i = D[i]) for (j = R[i]; j != i; j = R[j]) U[D[j]] = U[j], D[U[j]] = D[j], --siz[col[j]]; } void recover(const int &c) { // 进行recover操作 int i, j; for (i = U[c]; i != c; i = U[i]) for (j = L[i]; j != i; j = L[j]) U[D[j]] = D[U[j]] = j, ++siz[col[j]]; L[R[c]] = R[L[c]] = c; } bool dance(int dep) { // dance int i, j, c = R[0]; if (!R[0]) { for (i = 1; i < dep; ++i) { int x = (stk[i] - 1) / 9 / 9 + 1; int y = (stk[i] - 1) / 9 % 9 + 1; int v = (stk[i] - 1) % 9 + 1; ans[x][y] = v; } return 1; } for (i = R[0]; i != 0; i = R[i]) if (siz[i] < siz[c]) c = i; remove(c); for (i = D[c]; i != c; i = D[i]) { stk[dep] = row[i]; for (j = R[i]; j != i; j = R[j]) remove(col[j]); if (dance(dep + 1)) return 1; for (j = L[i]; j != i; j = L[j]) recover(col[j]); } recover(c); return 0; } } solver; int GetId(int row, int col, int num) { return (row - 1) * 9 * 9 + (col - 1) * 9 + num; } void Insert(int row, int col, int num) { int dx = (row - 1) / 3 + 1; int dy = (col - 1) / 3 + 1; int room = (dx - 1) * 3 + dy; int id = GetId(row, col, num); int f1 = (row - 1) * 9 + num; // task 1 int f2 = 81 + (col - 1) * 9 + num; // task 2 int f3 = 81 * 2 + (room - 1) * 9 + num; // task 3 int f4 = 81 * 3 + (row - 1) * 9 + col; // task 4 solver.insert(id, f1); solver.insert(id, f2); solver.insert(id, f3); solver.insert(id, f4); } int main() { solver.build(729, 324); for (int i = 1; i <= 9; ++i) for (int j = 1; j <= 9; ++j) { ans[i][j] = read(); for (int v = 1; v <= 9; ++v) { if (ans[i][j] && ans[i][j] != v) continue; Insert(i, j, v); } } solver.dance(1); for (int i = 1; i <= 9; ++i, putchar('\n')) for (int j = 1; j <= 9; ++j, putchar(' ')) printf("%d", ans[i][j]); return 0; }
p1074 [NOIP2009 提高组] 靶形数独 蓝
靶形数独的方格同普通数独一样,在 9 格宽且 9 格高的大九宫格中有 9 个 3 格宽且 3 格高的小九宫格(用粗黑色线隔开的)。在这个大九宫格中,有一些数字是已知的,根据这些数字,利用逻辑推理,在其他的空格上填入 1 到 9 的数字。每个数字在每个小九宫格内不能重复出现,每个数字在每行、每列也不能重复出现。但靶形数独有一点和普通数独不同,即每一个方格都有一个分值,而且如同一个靶子一样,离中心越近则分值越高。总分数即每个方格上的分值和完成这个数独时填在相应格上的数字的乘积的总和。如图,在以下的这个已经填完数字的靶形数独游戏中,总分数为 2829游戏规定,将以总分数的高低决出胜负。
代码:
#include <bits/stdc++.h> const int oo = 0x3f3f3f3f; const int N = 1e5 + 10; const int e[] = {6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 6, 6, 7, 8, 8, 8, 8, 8, 7, 6, 6, 7, 8, 9, 9, 9, 8, 7, 6, 6, 7, 8, 9, 10, 9, 8, 7, 6, 6, 7, 8, 9, 9, 9, 8, 7, 6, 6, 7, 8, 8, 8, 8, 8, 7, 6, 6, 7, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}; int ans = -oo, a[10][10], stk[N]; int read() { int x = 0, f = 0, ch; while (!isdigit(ch = getchar())) f |= ch == '-'; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return f ? -x : x; } int GetWeight(int row, int col, int num) { // 求数乘上对应的权值 return num * e[(row - 1) * 9 + (col - 1)]; } struct DLX { static const int MAXSIZE = 1e5 + 10; int n, m, tot, first[MAXSIZE + 10], siz[MAXSIZE + 10]; int L[MAXSIZE + 10], R[MAXSIZE + 10], U[MAXSIZE + 10], D[MAXSIZE + 10]; int col[MAXSIZE + 10], row[MAXSIZE + 10]; void build(const int &r, const int &c) { // 进行build操作 n = r, m = c; for (int i = 0; i <= c; ++i) { L[i] = i - 1, R[i] = i + 1; U[i] = D[i] = i; } L[0] = c, R[c] = 0, tot = c; memset(first, 0, sizeof(first)); memset(siz, 0, sizeof(siz)); } void insert(const int &r, const int &c) { // 进行insert操作 col[++tot] = c, row[tot] = r, ++siz[c]; D[tot] = D[c], U[D[c]] = tot, U[tot] = c, D[c] = tot; if (!first[r]) first[r] = L[tot] = R[tot] = tot; else { R[tot] = R[first[r]], L[R[first[r]]] = tot; L[tot] = first[r], R[first[r]] = tot; } } void remove(const int &c) { // 进行remove操作 int i, j; L[R[c]] = L[c], R[L[c]] = R[c]; for (i = D[c]; i != c; i = D[i]) for (j = R[i]; j != i; j = R[j]) U[D[j]] = U[j], D[U[j]] = D[j], --siz[col[j]]; } void recover(const int &c) { // 进行recover操作 int i, j; for (i = U[c]; i != c; i = U[i]) for (j = L[i]; j != i; j = L[j]) U[D[j]] = D[U[j]] = j, ++siz[col[j]]; L[R[c]] = R[L[c]] = c; } void dance(int dep) { // dance int i, j, c = R[0]; if (!R[0]) { int cur_ans = 0; for (i = 1; i < dep; ++i) { int cur_row = (stk[i] - 1) / 9 / 9 + 1; int cur_col = (stk[i] - 1) / 9 % 9 + 1; int cur_num = (stk[i] - 1) % 9 + 1; cur_ans += GetWeight(cur_row, cur_col, cur_num);//直接加上即可 } ans = std::max(ans, cur_ans); return; } for (i = R[0]; i != 0; i = R[i]) if (siz[i] < siz[c]) c = i; remove(c); for (i = D[c]; i != c; i = D[i]) { stk[dep] = row[i]; for (j = R[i]; j != i; j = R[j]) remove(col[j]); dance(dep + 1); for (j = L[i]; j != i; j = L[j]) recover(col[j]); } recover(c); } } solver; int GetId(int row, int col, int num) { return (row - 1) * 9 * 9 + (col - 1) * 9 + num; } void Insert(int row, int col, int num) { int dx = (row - 1) / 3 + 1; // r int dy = (col - 1) / 3 + 1; // c int room = (dx - 1) * 3 + dy; // room int id = GetId(row, col, num); int f1 = (row - 1) * 9 + num; // task 1 int f2 = 81 + (col - 1) * 9 + num; // task 2 int f3 = 81 * 2 + (room - 1) * 9 + num; // task 3 int f4 = 81 * 3 + (row - 1) * 9 + col; // task 4 solver.insert(id, f1); solver.insert(id, f2); solver.insert(id, f3); solver.insert(id, f4); } int main() { solver.build(729, 324); for (int i = 1; i <= 9; ++i) for (int j = 1; j <= 9; ++j) { a[i][j] = read(); for (int v = 1; v <= 9; ++v) { if (a[i][j] && v != a[i][j]) continue; Insert(i, j, v); } } solver.dance(1); printf("%d", ans == -oo ? -1 : ans); return 0; }