我的结果。能通过

张三  •  5个月前


include

include

include

using namespace std;

int main() {

int m, n;
cin >> m >> n;

vector<int> rainfall(m + 2, 0);  // 使用m+2大小的数组,方便处理差分数组的边界

// 读取云朵的覆盖范围和降雨量,并使用差分数组记录变化
for (int i = 0; i < n; ++i) {
    int l, r, rf;
    cin >> l >> r >> rf;
    rainfall[l] += rf;
    if (r + 1 <= m) {
        rainfall[r + 1] -= rf;
    }
}

// 使用前缀和数组计算每个城市的降雨量
vector<int> city_rainfall(m + 1, 0);
city_rainfall[1] = rainfall[1];
for (int i = 2; i <= m; ++i) {
    city_rainfall[i] = city_rainfall[i - 1] + rainfall[i];
}

// 找到降雨量最少和最多的城市
int min_rainfall_city = 1, max_rainfall_city = 1;
for (int i = 2; i <= m; ++i) {
    if (city_rainfall[i] < city_rainfall[min_rainfall_city]) {
        min_rainfall_city = i;
    }
    if (city_rainfall[i] > city_rainfall[max_rainfall_city]) {
        max_rainfall_city = i;
    }
}

// 输出结果
cout << min_rainfall_city << " " << max_rainfall_city << endl;

return 0;

}


评论:

请先登录,才能进行评论