2019-2020 Nowcoder Girl初赛重现赛
A.牛妹爱整除
莽一波就过了???
https://ac.nowcoder.com/acm/contest/3405/A
#include <bits/stdc++.h>
using namespace std;
int K;
int main() {
scanf("%d", &K);
printf("%d\n", K + 1);
return 0;
}
B.吃桃
dfs
https://ac.nowcoder.com/acm/contest/3405/B
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int N, K;
vector<int> v[maxn];
int len[maxn], nx[maxn];
void dfs(int st, int pre) {
for(int i = 0; i < v[st].size(); i ++) {
if(v[st][i] == pre) continue;
dfs(v[st][i], st);
if(len[st] < len[v[st][i]] + 1) {
len[st] = len[v[st][i]] + 1;
nx[st] = v[st][i];
} else if(v[st][i] < nx[st] && len[st] == len[v[st][i]] + 1){
nx[st] = v[st][i];
}
}
}
int main() {
scanf("%d%d", &N, &K);
for(int i = 0; i < N - 1; i ++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
memset(len, 0, sizeof(len));
memset(nx, 0, sizeof(nx));
dfs(K, 0);
while(K) {
printf("%d\n", K);
K = nx[K];
}
return 0;
}
C.背包问题
01 背包
https://ac.nowcoder.com/acm/contest/3405/C
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
int N, V, sum = 0;
int w[220], v[220];
int dp[maxn];
int main() {
scanf("%d%d", &N, &V);
for(int i = 0; i < N; i ++) {
scanf("%d%d", &v[i], &w[i]);
sum += w[i];
}
memset(dp, 0, sizeof(dp));
for(int i = 0; i < N; i ++) {
for(int j = sum; j >= w[i]; j --)
dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
}
for(int i = 0; i <= sum; i ++) {
if(dp[i] >= V) {
printf("%d\n", i);
return 0;
}
}
return 0;
}
D.泡面
优先队列
https://ac.nowcoder.com/acm/contest/3405/D
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+ 10;
int N, p;
long long t[maxn];
pair<int, int> person[maxn];
long long ans[maxn];
int main() {
scanf("%d%d", &N, &p);
for(int i = 0; i < N; i ++) {
scanf("%d", &person[i].first);
person[i].second = i;
}
sort(person, person + N);
long long pos = 0;
priority_queue<int, vector<int>, greater<int> > q;
for(int i = 0; i < N; i ++) {
while(!q.empty() && pos < person[i].first) {
int top = q.top();
q.pop();
pos += p;
ans[top] = pos;
}
q.push(person[i].second);
if(pos < person[i].first) pos = person[i].first;
}
while(!q.empty()) {
int top = q.top();
q.pop();
pos += p;
ans[top] = pos;
}
for(int i = 0; i < N; i ++)
printf("%lld%s", ans[i], i == N - 1 ? "\n" : " ");
return 0;
}
E.伪直径
树的直径 - 1
不得不说太久不写代码写的真的乱 乱到我都不想再多看它一眼
https://ac.nowcoder.com/acm/contest/3405/E
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int N;
vector<int> v[maxn];
int st, en,temp;
int len[maxn], nx[maxn];
vector<int> ans;
void dfs(int act, int pre) {
for(int i = 0; i < (int)v[act].size(); i ++) {
if(v[act][i] == pre) continue;
dfs(v[act][i], act);
if(len[act] < len[v[act][i]] + 1) {
len[act] = len[v[act][i]] + 1;
nx[act] = v[act][i];
}
}
}
int main() {
scanf("%d", &N);
for(int i = 0; i < N - 1; i ++) {
int x, y;
scanf("%d%d", &x, &y);
v[x].push_back(y);
v[y].push_back(x);
}
memset(nx, 0, sizeof(nx));
memset(len, 0, sizeof(len));
st = 1;
dfs(st, 0);
while(st) {
if(nx[st] == 0) en = st;
st = nx[st];
}
st = en;
memset(nx, 0, sizeof(nx));
memset(len, 0, sizeof(len));
dfs(en, 0);
temp = st;
while(en) {
ans.push_back(en);
if(nx[en] == 0) st = en;
en = nx[en];
}
printf("%d\n", ans.size() - 2);
return 0;
}
F.最大最小差
https://ac.nowcoder.com/acm/contest/3405/F
感 jio 是 RMQ 但是真的不想写了 过年之前一定一定把这道题补掉 555~