目录
总结一下,本场比赛就是个DFScoder
原因请见下:
A
题意大概是要你求是否有五个数,是否三个相等且另外两个也相等且与前者不同,
用map记录一下:
#include<bits/stdc++.h>
using namespace std;
#define M 5005
#define N 500005
string s;
template<class T>
bool comp(T a,T b ,T c){return a == b && b == c;}
int a[N];
map<int,int> vis;
signed main(){
int cnt = 0;
for(int i = 1 ; i <= 5 ; i ++) {
cin >> a[i];
if (!vis[a[i]]) cnt ++;
vis[a[i]] ++;
}
if (cnt != 2) cout << "No" << endl;
if (cnt == 2){
int cnt1 = 0 , cnt2 = 0;
for(int i = 1 ; i <= 5 ; i++){
if (vis[a[i]] == 3) cnt1 ++;
if (vis[a[i]] == 2) cnt2 ++;
}
if (cnt1 == 3 && cnt2 == 2) cout << "Yes" << endl;
else cout << "No" << endl;
}
}
B
大意是:a[i]是i的家长 。 问你:n是1的第几代。昨天晚上因读错题吃了3发罚时QAQ
DFS , 两个参数,第一个是当前的数字,第二个是现在到了第几代,
当现在数字是1时,直接返回到了第几代,否则返回他的家长
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1048586
#define M 5005
#define N 500005
#define int long long
int a[N],cnt,fa[N];
int DFS(int x,int cnt ){
if (x == 1) return cnt;
else return DFS(fa[x] , cnt + 1);
}
signed main(){
int n,m,k;
cin >> n;
fa[1] = 1;
for(int i = 2 ; i <= n ; i ++){
cin >> a[i];
fa[i] = a[i];
}
cout << DFS(n,0) << endl;
return 0;
}
C
其实题目我也没看懂,但在洛谷上做过原题(dogo)
具体见此题:组合的输出 - 洛谷
但要把n,m反一下,又因这个吃了一发罚时
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
int n,m,k;
#define N 500005
int path[N];
void DFS(int x){
if (x > m){
for(int i = 1 ; i <= m ; i ++){
cout << path[i] << " ";
}
cout << endl;
return ;
}
for(int i = path[x - 1] + 1 ; i <= n; i ++){
path[x] = i;
DFS(x + 1);
}
}
signed main(){
cin >> m >> n;
DFS(1);
return 0;
}
D
昨晚De了一个小时bug比赛完后发现是下标写错了!!!
题意简单明了,我就不赘述
大概是介么做的,求一个前缀和(pre[i]),再求一个后缀和(last[i]),
总的最小值为ans = min(ans , pre[i] + last[i + 1])
最后ans别忘了ans = min(pre[n] , min(ans,last[1]))
!!!要开long long
!!!要开long long
!!!要开long long
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXN 1048586
#define M 5005
#define N 500005
#define int long long
#define Mn(a,b,c) min(a,min(b,c))
int a[N] , pre[N] , last[N];
signed main(){
int n,l,r;
cin >> n >> l >> r;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
for(int i = 1 ; i <= n ; i++){
pre[i] = min(pre[i - 1] + a[i] , i * l);
}
for(int i = n ; i >= 1 ; i --){
last[i] = min(r * (n - i + 1),last[i + 1] + a[i]);
}
int Min = 1e18;
for(int i = 1 ; i <= n ; i++){
Min = min(Min , pre[i] + last[i + 1]);
// cout << pre[i] << " " << last[i + 1] << endl;
}
cout << Mn(Min,pre[n] , last[1]) << endl;
return 0;
}