hihoCoder 1578 Visiting Peking University 【贪心】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1578 : 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.c++

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

输入

There are no more than 20 test cases.spa

For each test case:.net

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

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

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

输出

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


题目连接:内存

  http://hihocoder.com/problemset/problem/1578
ci

题目大意:

  n天旅游时间,小明打算花其中连续的m天去北京玩,其中第一天a和另一天b去参观清华,

  已知n天里参观清华排队的人数为p[i],目的是使得p[a]+p[b]最小。

  又由于北京有q天交通管制,因此实际上能够花连续k天,使得k天中恰有k-m天是交通管制,剩余m天游玩。

  

题目思路:

  【贪心】

  首先分析题目发现,其实要找连续k天知足其中有m天没交通管制,且第一天和其中一天p之和最小便可。

  k不固定,因此考虑将交通管制的天扣掉,这样找区间长度为m且知足p[a]+p[b]最小便可。

  直接暴力枚举,记录b的位置。

  而后将交通管制恢复,获得最终正确的答案。






/****************************************************

	Author : Coolxxx
	Copyright 2017 by Coolxxx. All rights reserved.
	BLOG : http://blog.csdn.net/u010568270

****************************************************/
#include<bits/stdc++.h>
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define mem(a,b) memset(a,b,sizeof(a))
const double EPS=0.00001;
const int J=10;
const int MOD=1000000007;
const int MAX=0x7f7f7f7f;
const double PI=3.14159265358979323;
const int N=104;
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
int n,m,lll,ans;
int a[N],b[N];
bool u[N];
int main()
{
	#ifndef ONLINE_JUDGE
//	freopen("1.txt","r",stdin);
//	freopen("2.txt","w",stdout);
	#endif
	int i,j,k;
	int x,y,z;
//	for(scanf("%d",&cass);cass;cass--)
//	init();
//	for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
	while(~scanf("%d",&n))
	{
		mem(u,0);
		scanf("%d",&m);
		for(i=0;i<n;i++)
		{
			scanf("%d",&a[i]);
		}
		scanf("%d",&cas);
		for(i=1;i<=cas;i++)
		{
            scanf("%d",&x);
            u[x]=1;
		}
        for(i=0,j=0;i<n;i++)
		{
			if(u[i])continue;
            b[j++]=a[i];
		}
		ans=MAX;
        for(i=0;i<=j-m;i++)
		{
			for(k=i+1;k<min(i+m,j);k++)
			{
				if(b[i]+b[k]<ans)
				{
					ans=b[i]+b[k];
                    y=i;z=k;
				}
			}
		}
        for(i=0;i<n;i++)
		{
			if(u[i])continue;
			if(!y)break;
			y--;
		}
		y=i;
        for(i=0;i<n;i++)
		{
			if(u[i])continue;
			if(!z)break;
			z--;
		}
		z=i;
		printf("%d %d\n",y,z);
	}
	return 0;
}
/*
//

//
*/