• luogu P2190 小Z的车厢


    题目链接:luogu P2190 小Z的车厢

    题目大意:

    题解:
    一开始认为很简单,就是一个普通的模拟,然后全WA。

    // 错误代码
    #include <iostream>
    using namespace std;
    #define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define N 1000000 + 10
    
    int n, m, up[N], down[N], train, sum;
    
    int main() {
    	io_speed_up;
    	cin >> n >> m;
    	for (int i = 1; i <= m; ++i) {
    		int x, y, z;
    		cin >> x >> y >> z;
    		up[x] += z;
    		down[y] += z;
    	}
    	for (int i = 1; i <= n; ++i) {
    		train += up[i] - down[i];
    		sum = sum >= train ? sum : train;
    	}
    	if (sum % 36) {
    		sum = sum / 36 + 1;
    	} else {
    		sum /= 36;
    	}
    	cout << sum;
    	return 0;
    }
    

    发现题目里的细节“环形铁轨”,所以存在(x)(y)大的情况,所以转换一下思想,假如(x)(y)大,那就相当于在(x)上车,在(n+1)下车,再在(1)上车,在(y)下车。(由于到(n)就结束,所以(n+1)的下车可以忽略)

    #include <iostream>
    using namespace std;
    #define io_speed_up ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define N 1000000 + 10
    
    int n, m, up[N], down[N], train, sum;
    
    int main() {
    	io_speed_up;
    	cin >> n >> m;
    	for (int i = 1; i <= m; ++i) {
    		int x, y, z;
    		cin >> x >> y >> z;
    		up[x] += z;
    		down[y] += z;
    		if (x > y) {
    			up[1] += z;
    		}
    	}
    	for (int i = 1; i <= n; ++i) {
    		train += up[i] - down[i];
    		sum = sum >= train ? sum : train;
    	}
    	if (sum % 36) {
    		sum = sum / 36 + 1;
    	} else {
    		sum /= 36;
    	}
    	cout << sum;
    	return 0;
    }
    
  • 相关阅读:
    pycharm出现乱码
    Name-based virtual servers 给予名称的虚拟服务
    预建报为稳定版本
    nginx指令
    Module ngx_http_index_module nginx的首页模块
    我还在坚持中~
    手机端页面自适应解决方案—rem布局进阶版
    前端页面的适配使用rem换算
    js零碎知识汇总
    让input不可编辑
  • 原文地址:https://www.cnblogs.com/IzumiSagiri/p/13986936.html
Copyright © 2020-2023  润新知