zancun

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 100005;
int n;
int avai[maxn], need[maxn];
bool dfs(int cur, int u, int f, bool lap)
{
    if(cur == u && lap) return true;

    if(f + avai[cur] < need[cur]) return false;
    f = f+avai[cur]-need[cur];
    //cout << f << endl;
    if(cur == (u-1+n)%n && !lap) lap = true;
    if(dfs((cur+1)%n, u, f, lap)) return true;
    return false;
}

int solve()
{
    for(int i = 0; i < n; i++)
    {
        if(dfs(i, i, 0, false)) return i;
        //cout << i << endl;
    }
    return -1;
}

int main()
{
    //freopen("out.txt", "w", stdout);
    int T; scanf("%d", &T);
    for(int kase = 1; kase <= T; kase++)
    {
        scanf("%d", &n);
        int sum1, sum2;
        sum1 = sum2 = 0;
        for(int i = 0; i < n; i++)
            scanf("%d", &avai[i]), sum1 += avai[i];
        for(int i = 0; i < n; i++)
            scanf("%d", &need[i]), sum2 += need[i];
        printf("Case %d: ", kase);
        if(sum1 < sum2) printf("Not possible\n");
        else
        {
            int flag = solve();
            if(flag == -1) printf("Not possible\n");
            else printf("Possible from station %d\n", flag+1);
        }
    }
    return 0;
}
时间: 2024-11-05 19:56:47

zancun的相关文章

小学生四则运算APP核心代码公布

Mainactivity类: package com.example.XXSCYS; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import android.os.Bundle; import android.app.Activity; import android.app.AlertDialog; import android.ap

佛曰这是冒泡

package Z4; import java.util.Scanner; public class ShuZu {public static void main (String[]args){ Scanner input= new Scanner(System.in); int i[]=new int[3]; for(int i1=0;i1<i.length;i1++){ i[i1]=input.nextInt(); } for(int q=0;q<i.length-1;q++){ for(