Gas Station


这个问题网上的最简单版本的确是简单的一塌糊涂,O(n)的复杂度,我们来理一理这种简单的思路。

假设A的每个月的工资和花费是固定的,而且每年都是不变。如果到了第 i 个月发现自己的积蓄不足以满足花费的时候,当然这日子就没法过了。现在A要交个女朋友,但交女朋友有条件,必须在他们认识后的第一个九月份买个苹果5土豪金,我们帮他看下,以他的工资和花费情况,在几月份开始交女朋友,能够保证满足女友的要求,抱的佳人归。

假设A在一月份认识了个女孩,最终发现自己能活到九月就已经很勉强了,更别说金了,所以A在今年的九月份之前的任何一个月交女友都不行,为何,因为每个月至少收支平衡才能到下个月,所以既然坚持到九月份了,说明这期间的每个月的所剩都不是负的,那么少了任何一个月 i,最终的积蓄都少了,就更买不起了。所以只能在九月份以后的月份开始认识女孩才有可能,当然A最希望在下个月 10月份就认识,因为10月份是潜在的可能性。以便下一年的九月份能买的起。当然如果转一圈都发现买不起,那就算了,洗洗睡吧哥们,只能帮你到这了。

class Solution {
public:
    int canCompleteCircuit(vector<int> &gas, vector<int> &cost) {
        if(gas.size() == 0) return -1;
        int total = 0;
        int cur_sum = 0;
        int start_po = 0;
        for(int i = 0; i < gas.size(); ++i)
        {
            cur_sum += gas[i] - cost[i];
            total += gas[i] - cost[i];
            if(cur_sum <  0){
                cur_sum = 0;
                start_po = i + 1;
            } 
        }
        if(total < 0) return -1;
        return start_po;
    }
};

程序中的total就是全年的收支情况,若资不抵债,那就怎么开始都没办法了,cur_sum就是到目前为止的情况,若在某个 i 处,该值为负,那起始位置就只能从下个位置开始。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
用c++解决pipeline system consists of N transfer station, some of which are connected by pipelines. For each of M pipelines the numbers of stations A[i] and B[i], which are connected by this pipeline, and its profitability C[i] are known. A profitability of a pipeline is an amount of dollars, which will be daily yielded in taxes by transferring the gas through this pipeline. Each two stations are connected by not more than one pipeline. The system was built by Soviet engineers, who knew exactly, that the gas was transferred from Ukrainian gas fields to Siberia and not the reverse. That is why the pipelines are unidirectional, i.e. each pipeline allows gas transfer from the station number A[i] to the station number B[i] only. More over, if it is possible to transfer the gas from the station X to the station Y (perhaps, through some intermediate stations), then the reverse transfer from Y to X is impossible. It is known that the gas arrives to the starting station number S and should be dispatched to the buyers on the final station number F. The President ordered the Government to find a route (i.e. a linear sequence of stations which are connected by pipelines) to transfer the gas from the starting to the final station. A profitability of this route should be maximal. A profitability of a route is a total profitability of its pipelines. Unfortunately, the President did not consider that some pipelines ceased to exist long ago, and, as a result, the gas transfer between the starting and the final stations may appear to be impossible... Input The first line contains the integer numbers N (2 ≤ N ≤ 500) and M (0 ≤ M ≤ 124750). Each of the next M lines contains the integer numbers A[i], B[i] (1 ≤ A[i], B[i] ≤ N) and C[i] (1 ≤ C[i] ≤ 10000) for the corresponding pipeline. The last line contains the integer numbers S and F (1 ≤ S, F ≤ N; S ≠ F). Output If the desired route exists, you should output its profitability. Otherwise you should output "No solution".
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值