Hdoj 5090
Water questions, sorted from small to large, can be filled to meet the conditions, first filled, then adjusted.
Portal: Click to open the link
#include <cstdio>#include <cmath>#include <queue>#include <map>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1e2+10;int t, n, k, ia[MAXN];int main(){ scanf("%d", &t); while(t--) { scanf("%d%d", &n, &k); bool flag = true; for(int i=1; i<=n; ++i) { scanf("%d", ia+i); } sort(ia+1, ia+n+1); for(int i=1; i<=n; ++i) { if(i < ia[i]) { flag = false; break; } if(i == ia[i]) { continue; } if(0 == (i-ia[i])%k) { ia[i] = i; } else { int tp = 1; for(int j=i+1; j<=n; ++j) { if(0 == (j-ia[i])%k) { ia[i] = j; tp = 0; --i; break; } } if(tp) { flag = false; break; } } sort(ia+1, ia+n+1); } printf("%s\n", flag ? "Jerry" : "Tom"); } return 0;}
Hdoj 5092
Assume that each row has one number to minimize the sum. After obtaining the MP [I] [J], you can only take the next point in the bottom left, bottom right, and bottom right of the point, record path, please be sure to lean right as far as possible.
Analysis: a typical DP
Portal: Click to open the link
#include <cstdio>#include <cmath>#include <queue>#include <map>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 1e2+10;const int INF = 0x7fffffff;int t, m, n, dp[MAXN][MAXN];int s[MAXN][MAXN], mp[MAXN][MAXN], icase = 1;void output(int i, int j){ if(0 == i) { return ; } if(1 == s[i][j]) { output(i-1, j-1); printf("%d ", j-1); } else if(2 == s[i][j]) { output(i-1, j); printf("%d ", j); } else if(3 == s[i][j]) { output(i-1, j+1); printf("%d ", j+1); }}int main(){ scanf("%d", &t); while(t--) { scanf("%d%d", &n, &m); memset(dp, 0, sizeof(dp)); memset(mp, 0, sizeof(mp)); memset(s, 0, sizeof(s)); for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) { scanf("%d", &mp[i][j]); } } for(int i=1; i<=m; ++i) { dp[1][i] = mp[1][i]; } for(int i=2; i<=n; ++i) { for(int j=1; j<=m; ++j) { int mn = INF; if(j>=2 && dp[i-1][j-1] <= mn) { mn = dp[i-1][j-1]; s[i][j] = 1; } if(dp[i-1][j] <= mn) { mn = dp[i-1][j]; s[i][j] = 2; } if(j<=m-1 && dp[i-1][j+1] <= mn) { mn = dp[i-1][j+1]; s[i][j] = 3; } dp[i][j] = mn + mp[i][j]; } } int mn = INF, id = 0; for(int i=1; i<=m; ++i) { if(dp[n][i] <= mn) { mn = dp[n][i]; id = i; } }/* printf("%d\n", mn); for(int i=1; i<=n; ++i) { for(int j=1; j<=m; ++j) { printf("%d ", dp[i][j]); } printf("\n"); }*/ printf("Case %d\n", icase++); output(n, id); printf("%d\n", id); } return 0;}
Hdoj 5093
Analysis: bipartite graph.
My teammates wrote this article, but they didn't take it into consideration. I posted the code for my teammates. I will try again later.
Portal: Click to open the link
Code:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int match[1260];char map[60][60];int pic[1260][60];int vis[1260];int m,n;int nx=0,ny[60],nk=0,flag=0,yvis[60];int dp(int now){int i;for(i=0;pic[now][i]!=-1;i++){int t=pic[now][i];if(vis[t]==1)continue;vis[t]=1;if(match[t]==-1||dp(match[t])){match[t]=now;return 1;}}return 0;}int main(){//freopen("D:\\in.txt","r",stdin);int t,i,j;cin>>t;while(t--){cin>>m>>n;for(i=0;i<m;i++)scanf("%s",map[i]);memset(yvis,0,sizeof(yvis));for(i=0;i<n;i++)ny[i]=i;nx=nk=0;flag=0;memset(pic,-1,sizeof(pic));for(i=0;i<m;i++){for(j=0;j<n;j++){if(map[i][j]=='*'){yvis[j]=1;pic[nx][flag++]=ny[j];}if(map[i][j]=='#'){if(flag){nx++;flag=0;}if(yvis[j]){ny[j]=n+nk;nk++;yvis[j]=0;}}}if(flag){nx++;flag=0;}}int ans=0;memset(match,-1,sizeof(match));for(i=0;i<=nx;i++){memset(vis,0,sizeof(vis));if(dp(i))ans++;}cout<<ans<<endl;}}
Hdoj 5094
Analysis: BFS + pressure (otherwise MLE will be performed), WA point: a location may have several different keys.
Portal: Click to open the link
#include <cstdio>#include <cmath>#include <queue>#include <map>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 55;int n, m, p, k, s, mk[MAXN][MAXN];int vis[1<<11][MAXN][MAXN];int dir[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };struct P{ int x, y, time, key;};map<int, int> mp;int BFS(){ memset(vis, 0, sizeof(vis)); queue<P> que; P s; s.x = s.y = 1; s.key = s.time = 0; if(mk[s.x][s.y]) { s.key = mk[s.x][s.y]; } vis[s.key][s.x][s.y] = 1; que.push(s); while(!que.empty()) { P u = que.front(); que.pop(); if(u.x==n && u.y==m) { return u.time; } for(int i=0; i<4; ++i) { P v; v.x = u.x + dir[i][0]; v.y = u.y + dir[i][1]; if(v.x<1 || v.x>n || v.y<1 || v.y>m) { continue; } int q = 51*51*51*u.x + 51*51*u.y + 51*v.x + v.y; int tp = mp[q]; if(4e8 == tp) { continue; } if(0 == tp) { v.time = u.time + 1; v.key = u.key; if(mk[v.x][v.y] && 0==(mk[v.x][v.y]&u.key)) { v.key += mk[v.x][v.y]; } if(vis[v.key][v.x][v.y]) { continue; } vis[v.key][v.x][v.y] = 1; que.push(v); } else { if(u.key & (1<<tp)) { v.time = u.time + 1; v.key = u.key; if(mk[v.x][v.y] && 0==(mk[v.x][v.y]&u.key)) { v.key += mk[v.x][v.y]; } if(vis[v.key][v.x][v.y]) { continue; } vis[v.key][v.x][v.y] = 1; que.push(v); } } // printf("%d %d %d %d\n", v.x, v.y, v.time, v.key); } } return -1;}int main(){ while(~scanf("%d%d%d", &n, &m, &p)) { scanf("%d", &k); mp.clear(); memset(mk, 0, sizeof(mk)); while(k--) { int ux, uy, vx, vy, g, hx, hy; scanf("%d%d%d%d%d", &ux, &uy, &vx, &vy, &g); hx = 51*51*51*ux + 51*51*uy + 51*vx + vy; hy = 51*51*51*vx + 51*51*vy + 51*ux + uy; if(0 == g) g = 4e8; mp[hx] = mp[hy] = g; } scanf("%d", &s); while(s--) { int x, y, q; scanf("%d%d%d", &x, &y, &q); mk[x][y] += (1<<q); } printf("%d\n", BFS()); } return 0;}
Hdoj 5095
Analysis: When writing a question, pay attention to processing-1, 0, 1 is almost the same, there is a positive first, do not need +.
Portal: Click to open the link
Code:
#include <cstdio>#include <cmath>#include <queue>#include <map>#include <vector>#include <cstring>#include <algorithm>using namespace std;typedef long long lint;const int MAXN = 20;char ch[] = {'p', 'q', 'r', 'u', 'v', 'w', 'x', 'y', 'z'};int ia[MAXN], t;int main(){ scanf("%d", &t); while(t--) { for(int i=0; i<10; ++i) { scanf("%d", ia+i); } int first = 1, zero = 1; for(int i=0; i<10; ++i) { if(0 == ia[i]) { continue; } zero = 0; if(ia[i] < 0) { if(-1 == ia[i]) { if(i < 9) { printf("-"); } else { printf("-1"); } } else { printf("%d", ia[i]); } first = 0; } else { if(1 == ia[i]) { if(!first) { printf("+"); } first = 0; if(i == 9) { printf("1"); } } else { if(!first) { printf("+"); } first = 0; printf("%d", ia[i]); } } if(i < 9) { printf("%c", ch[i]); } } if(zero) printf("0"); printf("\n"); } return 0;}
Hdoj 5098
The teammates wrote, pasted a piece of code, and then supplemented.
Portal: Click to open the link
# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <map> # include <algorithm> using namespace STD; char info [1010] [1030]; int need [1010] [1010], NN [1010]; int num [1010]; int res [1010], ns; // NS indicates the number of software, and Res indicates whether to restart Map <string, int> list; int DFS (INT now) {If (Num [now]! =-1) return num [now]; int MA = 0, I; for (I = 0; need [now] [I]! =-1; I ++) {int T = need [now] [I]; If (RES [T]) Ma = max (MA, DFS (t) + 1); elsema = max (MA, DFS (t);} num [now] = MA; return num [now];} int main () {// freopen ("D: \ in.txt", "r", stdin); int T, I, j, Count = 1; CIN> T; getchar (); getchar (); While (t --) {memset (Info, 0, sizeof (Info); list. clear (); memset (Res, 0, sizeof (RES); I = 0; while (gets (info [I]) // start data processing {If (strlen (info [I]) = 0) break; j = 0; while (info [I] [J]! = '*' & Info [I] [J]! = ':') J ++; list [String (info [I], j)] = I; If (info [I] [J] = '*') res [I] = 1; I ++;} NS = I; memset (need,-1, sizeof (need); memset (NN, 0, sizeof (NN); for (I = 0; I <ns; I ++) {int S = 0; while (info [I] [s]! = ''& S! = Strlen (info [I]) s ++; int e = s; while (E! = Strlen (info [I]) {S = E; e ++; while (E! = Strlen (info [I]) & info [I] [e]! = '') E ++; need [I] [NN [I] ++] = list [String (info [I] + S + 1, e-s-1)];} // data processing is complete. Need [I] indicates the dependent package of I, ending memset (Num,-1, sizeof (Num) by-1; int MA = 0; for (I = 0; I <ns; I ++) {If (RES [I]) Ma = max (MA, DFS (I) + 1 ); elsema = max (MA, DFS (I);} printf ("case % d: % d \ n", Count ++, Ma );}}
Hdoj 5099
Analysis: Water question, string comparison.
Portal: Click to open the link
Code:
#include <cstdio>#include <cmath>#include <queue>#include <map>#include <vector>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 2e3+10;char cha[MAXN], chb[MAXN];int t, icase = 1;int main(){ scanf("%d", &t); while(t--) { scanf("%s%s", cha, chb); // puts(cha); puts(chb); printf("Case %d: ", icase++); if(cha[0] > chb[0]) { printf("> "); } else if(cha[0] < chb[0]) { printf("< "); } else { printf("= "); } if(cha[1] != chb[1]) { cha[5] = chb[5] = '\0'; } int ret = strcmp(cha+2, chb+2); if(ret > 0) { printf(">"); } else if(ret < 0) { printf("<"); } else { printf("="); } printf("\n"); } return 0;}
Question of the 2014 Shanghai national invitational competition hdoj 5090-5099