ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-A-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.c++

输入数组

There are no more than 20 test cases.

For each test case:this

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

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

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.code

输出orm

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

样例输入
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
题目分析:
Ming要去旅游n天,要在北京玩m天,在北京的m天里,他要用m天里的第一天和另一天去北京大学玩。而后在这n天里面天天的排队时间都是不一样的。要求你算出排队时间之和最少的两天。然而,北京有交通管制日,在交通管制日内,他哪里也去不了。因此,他的行程能够改变,原本在北京待的m天能够改成k天,可是这k天里面,交通管制日的天数必须是k-m天,也就是说,多出来的天数必须是交通管制日。如第一个样例:
第几天 0 1 2 3 4 5 6
排队时间 6 9 10 1 0 8 35
是否为交通管制日
第一个样例中Ming要在北京玩3天,只有四天不是交通管制日,说明第一天只能在0和1中选择,2是交通管制日,那就有两种选择0~3或者1~4,0~3这个选择中选择0和3排队的时间最少,为7,而在1~4这个选择中选择1和4排队的时间最少为9(m天中,第一天必须选择),因此最后选择去北京大学的是第0天和第三天。
代码:
#include<bits/stdc++.h>

using namespace std;

const int INF=0x3f3f3f3f;

int Myfind(int a,int w[],int q)
{
    int i;
    for(i=0;i<q;i++)
    {
        if(a==w[i])break;
    }
    if(i<q) return 1;
    else return 0;
}//查找长度为q的数组w中是否有a元素
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int p[105];
        for(int i=0;i<n;i++)
        {
            cin>>p[i];
        }
        int q;
        cin>>q;
        int w[105];
        for(int i=0;i<q;i++)
        {
            cin >> w[i];
        }
        int k=n-1;//k表示要从第几天开始

        for(int i=0;i<m;)
        {
            int res;
            res=Myfind(k,w,q);
            //cout << res <<endl ;
            if(res==1) k--;
            else {k--;i++;}
        }
        //cout <<endl<< k;
        int ans1,ans2;
        int summax=0x3f3f3f3f;
        for(int i=0;i<=k+1;i++)
        {
            if(Myfind(i,w,q)==1) continue;
            int h=0;//h用来记录在北京旅游期间的交通管制天数
            for(int j=i+1;j<i+m+h;j++)
            {
                if(Myfind(j,w,q)==1) {h++;continue;}
                if(p[i]+p[j]<summax) {ans1=i;ans2=j; summax=p[i]+p[j];}
            }
        }
        cout << ans1 <<" " << ans2<< endl;
    }
    return 0;
}