2019-2020Nowcoder Girl初赛重现赛

Posted zlrrrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2019-2020Nowcoder 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
", K + 1);
    return 0;
}
View Code

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
", K);
        K = nx[K];
    }
     
    return 0;
}
View Code

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
", i);
            return 0;
        }
    }
     
    return 0;
}
View Code

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 ? "
" : " ");
     
    return 0;
}
View Code

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
", ans.size() - 2);
     
    return 0;
}
View Code

F.最大最小差

https://ac.nowcoder.com/acm/contest/3405/F

感 jio 是 RMQ 但是真的不想写了 过年之前一定一定把这道题补掉 555~

 

dfs 小公主重现江湖 23333 太久不写真的没手感 害

以上是关于2019-2020Nowcoder Girl初赛重现赛的主要内容,如果未能解决你的问题,请参考以下文章

2019-2020Nowcoder Girl初赛 题解

Nowcoder Girl 初赛 T5

2019-2020-1 20191330 《信息安全专业导论》第三周学习总结

# 2019-2020-3 《Java 程序设计》第四周总结

gossip girl chuck爸爸加入的那个神秘组织

matlab图像去噪的代码测试