社区讨论

TLE+RE 求时间复杂度

P9755[CSP-S 2023] 种树参与者 2已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@locuj0ak
此快照首次捕获于
2023/10/30 19:58
2 年前
此快照最后确认于
2023/11/02 10:39
2 年前
查看原帖
rt.
CPP
#include<bits/stdc++.h>
using namespace std;

int n,grow_cnt,ti_global;

struct block{
	long long a,b,c,t;
	vector<int> neightbor;
	bool allowed,planted,growed;//允许种植,已经种下,已经成熟 
};

block forest[1010];

bool cmp(int xx,int yy){
	return forest[xx].a > forest[yy].a;
}

int main(){
	cin >> n;
	for(int i = 1;i <= n;i++){
		cin >> forest[i].a >> forest[i].b >> forest[i].c;
	}
	for(int i = 2,u,v;i <= n;i++){
		cin >> u >> v;
		forest[u].neightbor.push_back(v);
		forest[v].neightbor.push_back(u);
	}
	//遍历每个时间状态
	for(int ti = 0;;ti++){
		//特判刚开始的情况 
		if(ti == 0){
			forest[1].allowed = true;
			if(forest[1].b + forest[1].c * 1 > 1)
				forest[1].t = forest[1].b + forest[1].c * 1;
			else forest[1].t = 1;
		}
		//对于每棵树而言 
		int plant_check[1010];
		memset(plant_check,0,sizeof(plant_check));
		int pc_cnt = 0;
		for(int i = 1;i <= n;i++){
			//没种? 
			if(!forest[i].planted){
				//之前允许种了吗? 
				if(!forest[i].allowed){
					//之前不允许的现在检查允不允许 
					for(unsigned int j = 0;j < forest[i].neightbor.size();j++){
						//第j个邻居种了 
						if(forest[forest[i].neightbor[j]].planted){
							//那么允许种,进数组 
							forest[i].allowed = true; 
							plant_check[pc_cnt++] = i;
							break;
						}
					}
				}
				//允许种也让它进数组 
				else plant_check[pc_cnt++] = i;
			}
			//种了就更新状态 
			else if(forest[i].growed == false){
				if(forest[i].b + forest[i].c * ti > 1)
					forest[i].t += forest[i].b + forest[i].c * ti;
				else forest[i].t++;
				//够高了 
				if(forest[i].t >= forest[i].a){
					forest[i].growed = true;
					grow_cnt++;
					
				}
			}
		}
		if(grow_cnt == n){
			cout << ti;
			return 0;
		}
		//能种的都在里了,找谁熟的最慢谁先种
		if(pc_cnt > 0){
			ti_global = ti;
			sort(plant_check,pc_cnt + plant_check,cmp);
			forest[plant_check[0]].planted = true;
		}
	}
	return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...