zoukankan      html  css  js  c++  java
  • ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days

    时间限制:1000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    80 Days is an interesting game based on Jules Verne's science fiction "Around the World in Eighty Days". In this game, you have to manage the limited money and time.

    Now we simplified the game as below:

    There are n cities on a circle around the world which are numbered from 1 to n by their order on the circle. When you reach the city i at the first time, you will get ai dollars (ai can even be negative), and if you want to go to the next city on the circle, you should pay bi dollars. At the beginning you have c dollars.

    The goal of this game is to choose a city as start point, then go along the circle and visit all the city once, and finally return to the start point. During the trip, the money you have must be no less than zero.

    Here comes a question: to complete the trip, which city will you choose to be the start city?

    If there are multiple answers, please output the one with the smallest number.

    输入

    The first line of the input is an integer T (T ≤ 100), the number of test cases.

    For each test case, the first line contains two integers n and c (1 ≤ n ≤ 106, 0 ≤ c ≤ 109).  The second line contains n integers a1, …, an  (-10≤ a≤ 109), and the third line contains n integers b1, …, bn (0 ≤ b≤ 109).

    It's guaranteed that the sum of n of all test cases is less than 106

    输出

    For each test case, output the start city you should choose.

    提示

    For test case 1, both city 2 and 3 could be chosen as start point, 2 has smaller number. But if you start at city 1, you can't go anywhere.

    For test case 2, start from which city seems doesn't matter, you just don't have enough money to complete a trip.

    样例输入
    2
    3 0
    3 4 5
    5 4 3
    3 100
    -3 -4 -5
    30 40 50
    样例输出
    2
    -1

    题目大意:

    给你初始资金c,再告诉你n个围成一圈(有序)的地点。每到一个地点,你可以得到ai元钱,然后你必须到下一个地点,这样你需要支出bi元。路途中不可以有一个时刻你的资金为负值。问字典序最小的起点,使得可以环游一周。

    首先每个地点的收益是wi=ai-bi,开辟一个数组保存w1w2...wn-1wnwn-1...w2w1。

    计算wi数组的前缀和,这样就可以O(1)地得到某段的收益了。

    顺序检索wi到wi+n-1的最小值与wi-1的差,若大于等于-c,就可以输出答案了。

    用线段树查询区间最小值。

    #include<cstdio>
    #include<queue>
    #include<algorithm>
    
    using namespace std;
    
    const int maxn=1000000;
    const int inf=2000000000;
    
    int a[maxn*2+10];
    
    struct ttree
    {
        int l,r;
        int mmin;
    };
    ttree tree[maxn*2*4+10];
    
    void pushup(int x)
    {
        if(tree[x].l==tree[x].r)
            return;
        tree[x].mmin=min(tree[x*2].mmin,tree[x*2+1].mmin);
    }
    
    void build(int x,int l,int r)
    {
        tree[x].l=l;
        tree[x].r=r;
        if(l==r)
        {
            tree[x].mmin=a[l];
        }
        else
        {
            int mid=(l+r)/2;
            build(x*2,l,mid);
            build(x*2+1,mid+1,r);
            pushup(x);
        }
    }
    
    int query(int x,int l,int r)
    {
        if(l<=tree[x].l&&r>=tree[x].r)
            return tree[x].mmin;
        int ret=inf;
        int mid=(tree[x].l+tree[x].r)/2;
        if(l<=mid)
            ret=min(ret,query(x*2,l,r));
        if(r>mid)
            ret=min(ret,query(x*2+1,l,r));
        return ret;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,c;
            scanf("%d%d",&n,&c);
            for(int i=1;i<=n;i++)
                scanf("%d",a+i);
            for(int i=1,b;i<=n;i++)
            {
                scanf("%d",&b);
                a[i]-=b;
            }
            for(int i=1;i<n;i++)
                a[n+i]=a[i];
    
            for(int i=2;i<n*2;i++)
                a[i]+=a[i-1];
    
            a[0]=0;
            int ans=-1;
            build(1,1,n*2-1);
            for(int i=1;i<=n;i++)
                if(query(1,i,i+n-1)-a[i-1]+c>=0)
                {
                    ans=i;
                    break;
                }
            printf("%d
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    外键的创建
    MySQL (1366, "Incorrect string value: '\xF0\x9F\x8E\xAC\xE5\x89...' for column 'description' at row 1")
    python 获取文件路径
    ModuleNotFoundError: No module named 'Crypto'
    Django---错误
    CentOS7系统ifconfig无法使用的解决方法
    NACOS集群搭建遇到的问题
    Mysql连接报错:1130-host ... is not allowed to connect to this MySql server如何处理
    CentOS7 yum方式安装MySQL5.7
    MySQL数据库的全局锁和表锁
  • 原文地址:https://www.cnblogs.com/acboyty/p/9704038.html
Copyright © 2011-2022 走看看