【牛客周赛 34】D,E题

小红的陡峭值

https://ac.nowcoder.com/acm/contest/75766/D
题意:修改数组中为0的元素,使数组中的陡峭值不超过1.
陡峭值为相邻两数差的绝对值之和
分析:我们需要找一个分界点,这个分界点的两边值不同,但是每一边内部的所有值要相同
,我们可以遍历数组,只要遇到0,就将其变为前面一个元素,这样就可以使每一边的元素尽可能相同。

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    vector<int> a(n), b(n);
    for (int i = 0; i < n; i++) cin >> a[i], b[i] = a[i];
    if(n == 1) {
        cout << -1 << '\n';
        return 0;
    }
    if (count(a.begin(), a.end(), 0) == n) {
        for (int i = 0; i < n - 1; i++) cout << 1 << ' '; cout << 2;
    }
    else {
        for (int i = 1; i < n; i++) if (a[i] == 0) a[i] = a[i - 1];
        for (int i = n - 1; i >= 0; i--) if (a[i] == 0) a[i] = a[i + 1];
        int cnt = 0;
        for (int i = 1; i < n; i++) cnt += abs(a[i] - a[i - 1]);
        if (cnt == 1) {
            for (auto i : a) cout << i << ' ';
        }
        else if(cnt == 0) {
            if(b[0] == 0) a[0] += 1;
            else if(b[n - 1] == 0) a[n - 1] += 1;
            else {
                cout << -1 << '\n';
                return 0;
            }
            for (auto i : a) cout << i << ' ';
        }
        else cout << -1 << '\n';
    }
}

小红的树形 dp

https://ac.nowcoder.com/acm/contest/75766/E
题意:可以抽象成一棵树,树中每一层元素需相同,相邻树层之间元素需不同。染色法的思想

// Problem: 小红的树形 dp
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/75766/E
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<queue>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
#include<stack>
#include<random>
#include<ctime>
#define ll long long
#define ull unsigned long long 
#define pb push_back
#define fi first
#define se second
#define ios ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define __int128_t int128
using namespace std;
typedef pair<int, int> PII;
typedef pair<ll, int> PLI;
int dx[4] = {-1,0,1,0},dy[4] = {0,1,0,-1};
const int N = 1e5+10;
int n;
string s;
vector<int> g[N];

void dfs(int u,int fa){
	for(auto v : g[u]) if(v != fa){
		if(s[v] == '?'){
			if(s[u] == 'd') s[v] = 'p';
			else s[v] = 'd';
		}else {
			if(s[u] == s[v]){
				cout << -1 << '\n';
				exit(0) ;
			}
		}
		dfs(v,u);
	}
}

int main(){
	cin >> n;
	cin >> s;
	s = ' ' + s;
	
	
	for(int i =1 ;i<= n-1 ;i++){
		int u,v;
		cin >> u >> v;
		g[u].pb(v);
		g[v].pb(u);
	}
	bool ok = false;
	for(int i = 1; i<= n;i++){
		if(s[i] != '?'){
			dfs(i,0);
			ok = true;
			break;
		}
	}
	if(!ok){
		s[1] = 'd';
		dfs(1,0);
	}
	for(int i = 1; i<= n;i++) cout << s[i];
}

对于if(v != fa)的判断

v != fa 这个条件检查是为了确保在进行深度优先搜索(DFS)时不会向父节点方向回溯。
在树结构中,每个节点(除了根节点)都有一个父节点,以及可能有多个子节点。当你从一个节点递归地遍历到它的子节点时,你不希望再次回到这个节点的父节点,因为这会导致无限循环。相反,你想要继续向下深入到子节点,直到访问完所有节点。
v != fa 这个条件是DFS算法的一个常见部分,它防止了这种后退行为,并确保每个节点只被访问一次。简而言之,它确保了DFS只向前(向子节点方向)进行,而不会回到已经处理过的父节点。

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值