1. 程式人生 > >ACM-ICPC北京賽區(2017)網絡賽1【模擬+枚舉+數組操作】

ACM-ICPC北京賽區(2017)網絡賽1【模擬+枚舉+數組操作】

fine enter who bit some title head efi cat

題目1 : Visiting Peking University

時間限制:1000ms 單點時限:1000ms 內存限制:256MB

描述

Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, …, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. During these m days, he intends to use the first day and another day to visit Peking university. Before he made his plan, Ming investigated on the number of tourists who would be waiting in line to enter Peking university during his n-day trip, and the results could be represented by an integer sequence p[i] (0 ≤ i ≤ n-1, p[i] represents the number of waiting tourists on day i). To save time, he hopes to choose two certain dates a and b to visit PKU(0 ≤ a < b ≤ n-1), which makes p[a] + p[b] as small as possible.

Unfortunately, Ming comes to know that traffic control will be taking place in Beijing on some days during his n-day trip, and he won’t be able to visit any place in Beijing, including PKU, on a traffic control day. Ming loves Beijing and he wants to make sure that m days can be used to visit interesting places in Beijing. So Ming made a decision: spending k (m ≤ k ≤ n) consecutive days in Beijing is also acceptable if there are k - m traffic control days among those k days. Under this complicated situation, he doesn’t know how to make the best schedule. Please write a program to help Ming determine the best dates of the two days to visit Peking University. Data guarantees a unique solution.

輸入

There are no more than 20 test cases.

For each test case:

The first line contains two integers, above mentioned n and m (2 ≤ n ≤ 100, 2 ≤ m ≤ n).

The second line contains n integers, above mentioned p[0] , p[1] , … p[n-1]. (0 ≤ p[i] ≤ 1000, i = 0 ... n-1)

The third line is an integer q (0 ≤ q ≤ n), representing the total number of traffic control days during the n-day trip, followed by q integers representing the dates of these days.

輸出

One line, including two integers a and b, representing the best dates for visiting PKU.

樣例輸入
7 3
6 9 10 1 0 8 35
3 5 6 2
4 2
10 11 1 2
1 2
樣例輸出
0 3
1 3

【題意】:題意是一人去旅遊n天,在北京待連續的m天,他想去參觀PKU,他知道這n天參觀排隊的人數,但是有q天交通管制,哪都去不了。所以他決定待K天,但是有要求,在前K天中,只要有K~M天是交通管制時間,他可以在連續的m天區間內選擇任意兩天參觀,問最少排隊人數是多少,註意排隊人數的下標是0~n-1,但管制天數是1—n,最後輸出這兩天(下標)。

【分析】
重點是枚舉起點天。
第一組數據中2、6、5天交通管制,將它們從數組刪去。剩下6(0) 9(1) 1(3) 0(4).三天的組合—>6 9 1中選2天 or 9 1 0 中選2天。求和後比較取最小值。(註意每個情況的第一天是必須選的)。則必須選擇第0或1天作為遊覽的第一天,否則無法保證總共遊覽m=3天。若選第0天開始,則最佳方案是第0和第3天,排隊總人數是6+1=7。若選第1天作為開始,則最佳方案是第1和第4天,9+0=9。所以最終的方案是第0和第3天。

【代碼】
技術分享
#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
#define inf 0x3f3f3f3f
struct node
{
    int re,id;
}b[maxn];
int main()
{
    int n,m,k;
    int a[maxn];
    int q,x;
    int vis[maxn];
    while(~scanf("%d%d",&n,&m))
    {
        k=0;
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }

        scanf("%d",&q);
        for(int i=0; i<q; i++)
        {
            scanf("%d",&x);
            vis[x]=1;
        }

        for(int i=0;i<n;i++)
        {
            if(!vis[i])
            {
                b[k].re=a[i];
                b[k++].id=i;
                //k++;
            }
        }

        int minn=inf,l=0,r=0;//
        for(int i=0;i+m<=k;i++)
        {
            int x=b[i].re;
            for(int j=1;j<m;j++)
            {
                if(b[i+j].re+x < minn)
                {
                    minn=b[i+j].re+x;
                    l=b[i].id;
                    r=b[i+j].id;
                }
            }
        }
        printf("%d %d\n",l,r);
    }
}
0ms



ACM-ICPC北京賽區(2017)網絡賽1【模擬+枚舉+數組操作】