我不理解
  • 板块P1250 种树
  • 楼主YangXiaopei
  • 当前回复2
  • 已保存回复2
  • 发布时间2023/11/8 15:21
  • 上次更新2023/11/8 18:26:17
查看原帖
我不理解
785630
YangXiaopei楼主2023/11/8 15:21

这个代码能 AC。

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n, m, ans;
struct node{
	int b, e, t;
}a[50005];
bool vis[300005];
bool cmp(node x, node y){
	return x.e < y.e;
}
signed main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> a[i].b >> a[i].e >> a[i].t;
	}
	sort(a + 1, a + m + 1, cmp);
	for(int i = 1; i <= m; i++){
		for(int j = a[i].b; j <= a[i].e; j++){
			if(vis[j]){
				a[i].t--;
			}
		}
		for(int j = a[i].e; j >= a[i].b; j--){
			if(a[i].t <= 0){
				break;
			}
			if(vis[j] == 0){
				vis[j] = 1;
				a[i].t--;
				ans++;
			} 
		} 
	}
	cout << ans;
	return 0;
}

但我只改了一个地方就只有 2727 了。

#include<bits/stdc++.h>
#define int long long 
using namespace std;
int n, m, ans;
struct node{
	int b, e, t;
}a[50005];
bool vis[300005];
bool cmp(node x, node y){
	return x.e < y.e;
}
signed main(){
	cin >> n >> m;
	for(int i = 1; i <= m; i++){
		cin >> a[i].b >> a[i].e >> a[i].t;
	}
	sort(a + 1, a + m + 1, cmp);
	for(int i = 1; i <= m; i++){
		for(int j = a[i].b; j <= a[i].e; j++){
			if(vis[j]){
				a[i].t--;
			}
		}
		for(int j = a[i].e; j >= a[i].b; j--){
			if(a[i].t == 0){//把 <= 改成了 ==
				break;
			}
			if(vis[j] == 0){
				vis[j] = 1;
				a[i].t--;
				ans++;
			} 
		} 
	}
	cout << ans;
	return 0;
}

明明每次只会减 11,<= 不应该和 == 没区别吗?

求问大佬

2023/11/8 15:21
加载中...