11093 - Just Finish it up

本文探讨了如何通过算法解决汽油循环问题,从起点出发,循环返回起点的解题策略,涉及枚举起点和终点,判断汽油量是否足够完成循环。通过循环检查,找出可能的解,最终确定解的存在与否。

从1开始枚举起点,如果起点大于n了仍未找到答案,则确定无解。 r枚举终点,如果可以循环一圈回到起点,则输出l,否则,在某个r点汽油将小于0,那么从l到r的所有点都不可能是解,因为那样汽油量将更小,更无法完成r点到下一点的任务 。 如此这般就行了,需要注意的是如果r<l 时汽油<0了,那么必然无解。

#include<bits/stdc++.h>
using namespace std;
int T,n,p[100005],q[100005],kase=0;
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        for(int i=1;i<=n;i++) scanf("%d",&p[i]);
        for(int i=1;i<=n;i++) scanf("%d",&q[i]);
        int l=1,r=1;
        bool ok = false;
        printf("Case %d: ",++kase);
        while(l<=n) {
            int ans = 0;
            bool flage = true;
            while(r!=l||flage){
                flage = false;
                ans+=p[r];
                ans-=q[r];
                if(ans<0) {
                    if(l>r) goto loop;
                    l = r+1; r = r+1; break;
                }
                else r++;
                if(r>n) r = 1;
                if(l==r) ok = true;
            }
            if(ok) { printf("Possible from station %d\n",l); break; }
        } loop:
         if(!ok) printf("Not possible\n");
    }
    return 0;
}


/* 3965 * Notes on Program-Order guarantees on SMP systems. 3966 * 3967 * MIGRATION 3968 * 3969 * The basic program-order guarantee on SMP systems is that when a task [t] 3970 * migrates, all its activity on its old CPU [c0] happens-before any subsequent 3971 * execution on its new CPU [c1]. 3972 * 3973 * For migration (of runnable tasks) this is provided by the following means: 3974 * 3975 * A) UNLOCK of the rq(c0)->lock scheduling out task t 3976 * B) migration for t is required to synchronize *both* rq(c0)->lock and 3977 * rq(c1)->lock (if not at the same time, then in that order). 3978 * C) LOCK of the rq(c1)->lock scheduling in task 3979 * 3980 * Release/acquire chaining guarantees that B happens after A and C after B. 3981 * Note: the CPU doing B need not be c0 or c1 3982 * 3983 * Example: 3984 * 3985 * CPU0 CPU1 CPU2 3986 * 3987 * LOCK rq(0)->lock 3988 * sched-out X 3989 * sched-in Y 3990 * UNLOCK rq(0)->lock 3991 * 3992 * LOCK rq(0)->lock // orders against CPU0 3993 * dequeue X 3994 * UNLOCK rq(0)->lock 3995 * 3996 * LOCK rq(1)->lock 3997 * enqueue X 3998 * UNLOCK rq(1)->lock 3999 * 4000 * LOCK rq(1)->lock // orders against CPU2 4001 * sched-out Z 4002 * sched-in X 4003 * UNLOCK rq(1)->lock 4004 * 4005 * 4006 * BLOCKING -- aka. SLEEP + WAKEUP 4007 * 4008 * For blocking we (obviously) need to provide the same guarantee as for 4009 * migration. However the means are completely different as there is no lock 4010 * chain to provide order. Instead we do: 4011 * 4012 * 1) smp_store_release(X->on_cpu, 0) -- finish_task() 4013 * 2) smp_cond_load_acquire(!X->on_cpu) -- try_to_wake_up() 4014 * 4015 * Example: 4016 * 4017 * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule) 4018 * 4019 * LOCK rq(0)->lock LOCK X->pi_lock 4020 * dequeue X 4021 * sched-out X 4022 * smp_store_release(X->on_cpu, 0); 4023 * 4024 * smp_cond_load_acquire(&X->on_cpu, !VAL); 4025 * X->state = WAKING 4026 * set_task_cpu(X,2) 4027 * 4028 * LOCK rq(2)->lock 4029 * enqueue X 4030 * X->state = RUNNING 4031 * UNLOCK rq(2)->lock 4032 * 4033 * LOCK rq(2)->lock // orders against CPU1 4034 * sched-out Z 4035 * sched-in X 4036 * UNLOCK rq(2)->lock 4037 * 4038 * UNLOCK X->pi_lock 4039 * UNLOCK rq(0)->lock 4040 * 4041 * 4042 * However, for wakeups there is a second guarantee we must provide, namely we 4043 * must ensure that CONDITION=1 done by the caller can not be reordered with 4044 * accesses to the task state; see try_to_wake_up() and set_current_state(). 4045 */ 4046 4047 /** 4048 * try_to_wake_up - wake up a thread 4049 * @p: the thread to be awakened 4050 * @state: the mask of task states that can be woken 4051 * @wake_flags: wake modifier flags (WF_*) 4052 * 4053 * Conceptually does: 4054 * 4055 * If (@state & @p->state) @p->state = TASK_RUNNING. 4056 * 4057 * If the task was not queued/runnable, also place it back on a runqueue. 4058 * 4059 * This function is atomic against schedule() which would dequeue the task. 4060 * 4061 * It issues a full memory barrier before accessing @p->state, see the comment 4062 * with set_current_state(). 4063 * 4064 * Uses p->pi_lock to serialize against concurrent wake-ups. 4065 * 4066 * Relies on p->pi_lock stabilizing: 4067 * - p->sched_class 4068 * - p->cpus_ptr 4069 * - p->sched_task_group 4070 * in order to do migration, see its use of select_task_rq()/set_task_cpu(). 4071 * 4072 * Tries really hard to only take one task_rq(p)->lock for performance. 4073 * Takes rq->lock in: 4074 * - ttwu_runnable() -- old rq, unavoidable, see comment there; 4075 * - ttwu_queue() -- new rq, for enqueue of the task; 4076 * - psi_ttwu_dequeue() -- much sadness :-( accounting will kill us. 4077 * 4078 * As a consequence we race really badly with just about everything. See the 4079 * many memory barriers and their comments for details. 4080 * 4081 * Return: %true if @p->state changes (an actual wakeup was done), 4082 * %false otherwise. 4083 */ 分析上述注释并给出设计思路以及解释
最新发布
07-04
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值