8.12 网易笔试
感觉是暴力专场
1. 枚举147,258,369对应TBW
#include <bits/stdc++.h> using namespace std; int n, m; int ans; int main() { scanf("%d", &n); while(n--){ vector<int> M[3]; int x; char y; for(int i = 0 ; i < 7 ; i++){ scanf("%d%c", &x, &y); if(y == 'T') M[0].push_back(x); else if(y == 'B') M[1].push_back(x); else if(y == 'W') M[2].push_back(x); } for(int i = 0 ; i < 3 ; i++){ sort(M[i].begin(), M[i].end()); } int sum = 0; int a[3][3] = {{1,4,7},{2,5,8},{3,6,9}}; int b[3] = {0,1,2}; do{ sum = 0; for(int i = 0 ; i < 3 ; i++){ int id = b[i]; for(int j = 0 ; j < 3 ; j++){ if(binary_search(M[i].begin(), M[i].end(), a[id][j])) sum++; } } if(sum >= 7) break; }while(next_permutation(b, b+3)); if(sum >= 7) printf("YES\n"); else printf("NO\n"); } return 0; }
2.暴力模拟
#include <bits/stdc++.h> using namespace std; int n, m; int a[505][505]; int ans; int main() { scanf("%d", &n); for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ scanf("%d", &a[i][j]); } } for(int i = 1 ; i <= n ; i++){ int sum = 0; for(int j = 1 ; j <= n ; j++){ sum += a[i][j]; } a[i][0] = sum; } for(int i = 1 ; i <= n ; i++){ int sum = 0; for(int j = 1 ; j <= n ; j++){ sum += a[j][i]; } a[0][i] = sum; } for(int k = 0 ; k < n ; k++){ int mx = 0; int my = 0; int mmax = -1; for(int i = 1 ; i <= n ; i++){ for(int j = 1 ; j <= n ; j++){ if(a[i][0] + a[0][j] - a[i][j] > mmax){ mx = i; my = j; mmax = a[i][0] + a[0][j] - a[i][j]; } } } for(int i = 1 ; i <= n ; i++){ a[i][0] -= a[i][my]; } for(int i = 1 ; i <= n ; i++){ a[0][i] -= a[mx][i]; } a[mx][0] = -1; a[0][my] = -1; int x = mx; int y = my; for(int i = 1 ; i < mx ; i++){ if(a[i][0] <= -1) x--; } for(int i = 1 ; i < my ; i++){ if(a[0][i] <= -1) y--; } printf("%d %d\n", x, y); } return 0; }
3.有大佬帮忙看看第3题哪里错了吗?0分...
#include <bits/stdc++.h> using namespace std; int n, m; int a[1000050][5]; int b[1000050]; map<int, int> e; int ans; void dfs(int l, int r){ int p = l+1; int sum = a[r][0] - a[l][0]; while(p < r){ int ll = p; int rr = e[a[p][1]]; dfs(ll, rr); sum -= a[rr][0] - a[ll][0]; p = rr + 1; } b[l] = sum; } int main() { int T; scanf("%d", &T); while(T--){ e.clear(); memset(a, 0, sizeof(a)); memset(b, 0, sizeof(b)); ans = 0; scanf("%d", &n); for(int i = 1 ; i <= n ; i++){ scanf("%d %d %d", &a[i][0], &a[i][1], &a[i][2]); e[a[i][1]] = i; ans = max(ans, a[i][1]); } a[0][0] = a[1][0]; a[n+1][0] = a[n][0]; dfs(0, n+1); int mmax = 0; for(int i = 1 ; i <= n ; i++){ if(b[i] >= mmax){ mmax = b[i]; ans = min(ans, a[i][1]); } } printf("%d\n", ans); } return 0; }