2020.08.02 拼多多算法工程师 笔试题解
第一题(100%)按题意模拟,注意第N次到的话特判,否则只能过96%
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int k, n;
int x;
signed main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
while (~scanf("%d%d", &k, &n)) {
int cur = 0, cnt = 0;
int ok = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &x);
if (ok) continue;
if (cur + x == k) {
if(n != i) ok = 1;
cur += x;
} else if (cur + x > k) {
cnt++;
cur = k - (cur + x - k);
} else {
cur += x;
}
}
if (ok || k == 0) {
printf("paradox\n");
} else {
printf("%d %d\n", k - cur, cnt);
}
}
return 0;
}
第二题(100%)我是枚举了24种同构+并查集做的,一定有聪明的小朋友写出了预处理。
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define A 1
#define B 2
#define C 3
#define D 4
#define E 5
#define F 6
const int maxn = 1010;
int n;
int dice[maxn][7];
int pre[maxn];
int find(int x) {
return x == pre[x] ? x : pre[x] = find(pre[x]);
}
void unite(int x, int y) {
x = find(x); y = find(y);
pre[x] = y;
}
int check(int x, int y) {
if (dice[x][1]==dice[y][A]&&dice[x][2]==dice[y][B]&&dice[x][3]==dice[y][C]&&dice[x][4]==dice[y][D]&&dice[x][5]==dice[y][E]&&dice[x][6]==dice[y][F]) return 1;
if (dice[x][1]==dice[y][C]&&dice[x][2]==dice[y][D]&&dice[x][3]==dice[y][B]&&dice[x][4]==dice[y][A]&&dice[x][5]==dice[y][E]&&dice[x][6]==dice[y][F]) return 1;
if (dice[x][1]==dice[y][B]&&dice[x][2]==dice[y][A]&&dice[x][3]==dice[y][D]&&dice[x][4]==dice[y][C]&&dice[x][5]==dice[y][E]&&dice[x][6]==dice[y][F]) return 1;
if (dice[x][1]==dice[y][D]&&dice[x][2]==dice[y][C]&&dice[x][3]==dice[y][A]&&dice[x][4]==dice[y][B]&&dice[x][5]==dice[y][E]&&dice[x][6]==dice[y][F]) return 1;
if (dice[x][1]==dice[y][A]&&dice[x][2]==dice[y][B]&&dice[x][3]==dice[y][D]&&dice[x][4]==dice[y][C]&&dice[x][5]==dice[y][F]&&dice[x][6]==dice[y][E]) return 1;
if (dice[x][1]==dice[y][C]&&dice[x][2]==dice[y][D]&&dice[x][3]==dice[y][A]&&dice[x][4]==dice[y][B]&&dice[x][5]==dice[y][F]&&dice[x][6]==dice[y][E]) return 1;
if (dice[x][1]==dice[y][B]&&dice[x][2]==dice[y][A]&&dice[x][3]==dice[y][C]&&dice[x][4]==dice[y][D]&&dice[x][5]==dice[y][F]&&dice[x][6]==dice[y][E]) return 1;
if (dice[x][1]==dice[y][D]&&dice[x][2]==dice[y][C]&&dice[x][3]==dice[y][B]&&dice[x][4]==dice[y][A]&&dice[x][5]==dice[y][F]&&dice[x][6]==dice[y][E]) return 1;
if (dice[x][1]==dice[y][A]&&dice[x][2]==dice[y][B]&&dice[x][3]==dice[y][F]&&dice[x][4]==dice[y][E]&&dice[x][5]==dice[y][C]&&dice[x][6]==dice[y][D]) return 1;
if (dice[x][1]==dice[y][F]&&dice[x][2]==dice[y][E]&&dice[x][3]==dice[y][B]&&dice[x][4]==dice[y][A]&&dice[x][5]==dice[y][C]&&dice[x][6]==dice[y][D]) return 1;
if (dice[x][1]==dice[y][B]&&dice[x][2]==dice[y][A]&&dice[x][3]==dice[y][E]&&dice[x][4]==dice[y][F]&&dice[x][5]==dice[y][C]&&dice[x][6]==dice[y][D]) return 1;
if (dice[x][1]==dice[y][E]&&dice[x][2]==dice[y][F]&&dice[x][3]==dice[y][A]&&dice[x][4]==dice[y][B]&&dice[x][5]==dice[y][C]&&dice[x][6]==dice[y][D]) return 1;
if (dice[x][1]==dice[y][A]&&dice[x][2]==dice[y][B]&&dice[x][3]==dice[y][E]&&dice[x][4]==dice[y][F]&&dice[x][5]==dice[y][D]&&dice[x][6]==dice[y][C]) return 1;
if (dice[x][1]==dice[y][E]&&dice[x][2]==dice[y][F]&&dice[x][3]==dice[y][B]&&dice[x][4]==dice[y][A]&&dice[x][5]==dice[y][D]&&dice[x][6]==dice[y][C]) return 1;
if (dice[x][1]==dice[y][B]&&dice[x][2]==dice[y][A]&&dice[x][3]==dice[y][F]&&dice[x][4]==dice[y][E]&&dice[x][5]==dice[y][D]&&dice[x][6]==dice[y][C]) return 1;
if (dice[x][1]==dice[y][F]&&dice[x][2]==dice[y][E]&&dice[x][3]==dice[y][A]&&dice[x][4]==dice[y][B]&&dice[x][5]==dice[y][D]&&dice[x][6]==dice[y][C]) return 1;
if (dice[x][1]==dice[y][F]&&dice[x][2]==dice[y][E]&&dice[x][3]==dice[y][C]&&dice[x][4]==dice[y][D]&&dice[x][5]==dice[y][A]&&dice[x][6]==dice[y][B]) return 1;
if (dice[x][1]==dice[y][C]&&dice[x][2]==dice[y][D]&&dice[x][3]==dice[y][E]&&dice[x][4]==dice[y][F]&&dice[x][5]==dice[y][A]&&dice[x][6]==dice[y][B]) return 1;
if (dice[x][1]==dice[y][E]&&dice[x][2]==dice[y][F]&&dice[x][3]==dice[y][D]&&dice[x][4]==dice[y][C]&&dice[x][5]==dice[y][A]&&dice[x][6]==dice[y][B]) return 1;
if (dice[x][1]==dice[y][D]&&dice[x][2]==dice[y][C]&&dice[x][3]==dice[y][F]&&dice[x][4]==dice[y][E]&&dice[x][5]==dice[y][A]&&dice[x][6]==dice[y][B]) return 1;
if (dice[x][1]==dice[y][E]&&dice[x][2]==dice[y][F]&&dice[x][3]==dice[y][C]&&dice[x][4]==dice[y][D]&&dice[x][5]==dice[y][B]&&dice[x][6]==dice[y][A]) return 1;
if (dice[x][1]==dice[y][D]&&dice[x][2]==dice[y][C]&&dice[x][3]==dice[y][E]&&dice[x][4]==dice[y][F]&&dice[x][5]==dice[y][B]&&dice[x][6]==dice[y][A]) return 1;
if (dice[x][1]==dice[y][F]&&dice[x][2]==dice[y][E]&&dice[x][3]==dice[y][D]&&dice[x][4]==dice[y][C]&&dice[x][5]==dice[y][B]&&dice[x][6]==dice[y][A]) return 1;
if (dice[x][1]==dice[y][C]&&dice[x][2]==dice[y][D]&&dice[x][3]==dice[y][F]&&dice[x][4]==dice[y][E]&&dice[x][5]==dice[y][B]&&dice[x][6]==dice[y][A]) return 1;
return 0;
}
signed main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
while (~scanf("%d", &n)) {
for (int i = 1; i <= n; i++) {
pre[i] = i;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 6; j++) {
scanf("%d", &dice[i][j]);
}
}
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
if (check(i, j)) unite(i, j);
}
}
int ret1 = 0;
for (int i = 1; i <= n; i++) {
if (i == find(i)) ret1++;
}
printf("%d\n", ret1);
map<int, int> vis;
for (int i = 1; i <= n; i++) {
vis[find(i)]++;
}
vector<int> ret2;
for (const auto &x : vis) {
ret2.emplace_back(x.second);
}
sort(ret2.begin(), ret2.end(), greater<int>());
for (int i = 0; i < ret2.size(); i++) {
printf("%d%c", ret2[i], " \n"[i == ret2.size() - 1]);
}
}
return 0;
} 贴一下枚举的图吧。。
第三题(100%)按美味程度、能量数的顺序从小到大排序,然后枚举lunch,二分dinner的美味程度,更新答案。记得单独讨论只吃一顿或者一顿都不吃的情况。
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int maxn = 100100;
int n, m, t;
pii lunch[maxn], dinner[maxn];
int suf[maxn];
signed main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
while (~scanf("%d%d%d",&n,&m,&t)) {
for (int i = 1; i <= n; i++) {
scanf("%d%d",&lunch[i].second, &lunch[i].first);
}
for (int i = 1; i <= m; i++) {
scanf("%d%d",&dinner[i].second, &dinner[i].first);
}
if (t == 0) {
printf("0\n");
continue;
}
sort(lunch + 1, lunch + n + 1);
sort(dinner + 1, dinner + m + 1);
int ret = INT_MAX;
for (int i = 1; i <= n; i++) {
if (lunch[i].first >= t) ret = min(ret, lunch[i].second);
}
for (int i = 1; i <= m; i++) {
if (dinner[i].first >= t) ret = min(ret, dinner[i].second);
suf[i] = dinner[i].second;
}
for (int i = m - 1; i >= 1; i--) {
suf[i] = min(suf[i + 1], suf[i]);
}
for (int i = 1; i <= n; i++) {
if (lunch[i].first >= t) continue;
int require = t - lunch[i].first;
if (dinner[m].first < require) continue;
int lo = 1, hi = m;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (dinner[mid].first >= require) {
ret = min(ret, suf[mid] + lunch[i].second);
hi = mid - 1;
} else {
lo = mid + 1;
}
}
}
printf("%d\n", ret == INT_MAX ? -1 : ret);
}
return 0;
} 第四题(20%)我读错题了,我以为就6个'#'. 所以我写了个预处理暴力。正解插头DP
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const LL mod = 1E9 + 9;
const int dx[5] = {0, 0, 1, -1};
const int dy[5] = {1, -1, 0, 0};
const int maxn = 7;
char s[maxn][maxn];
vector<pii> pos;
int n = 6, m = 6;
LL ret;
vector<vector<int>> status;
vector<int> tmp;
void pre(int cnt, int cap) {
if (cnt == min((int)pos.size() + 1, 7)) {
status.emplace_back(tmp);
return;
}
for (int i = 1; i <= 6; i++) {
// if (i == cap) continue;
tmp.emplace_back(i);
pre(cnt + 1, i);
tmp.pop_back();
}
}
int check(int x, int y) {
return x >= 1 && x <= 6 && y >= 1 && y <= 6;
}
int ok() {
for (int i = 1; i <= 6; i++) {
for (int j = 1; j <= 6; j++) {
for (int k = 0; k < 4; k++) {
int x = i + dx[k];
int y = j + dy[k];
if (!check(x, y)) continue;
if (s[i][j] == '*') continue;
if (s[x][y] == s[i][j]) return 0;
}
}
}
return 1;
}
signed main() {
// freopen("in", "r", stdin);
// freopen("out", "w", stdout);
pos.clear();
for (int i = 1; i <= 6; i++) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= 6; j++) {
if (s[i][j] == '#') pos.emplace_back(i, j);
}
}
tmp.clear();
status.clear();
pre(1, -1);
for (int i = 0; i < status.size(); i++) {
for (int j = 0; j < pos.size(); j++) {
int x = pos[j].first, y = pos[j].second;
s[x][y] = status[i][j] + '0';
}
if (ok()) {
ret++;
if (ret >= mod) ret %= mod;
}
for (int j = 0; j < pos.size(); j++) {
int x = pos[j].first, y = pos[j].second;
s[x][y] = '#';
}
}
printf("%lld\n", ret);
return 0;
} #笔试题目##校招##拼多多#
