这个代码能 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;
}
但我只改了一个地方就只有 27 了。
#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;
}
明明每次只会减 1,<= 不应该和 == 没区别吗?
求问大佬