1. 程式人生 > >hihoCoder 1578 Visiting Peking University 【貪心】 (ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)

hihoCoder 1578 Visiting Peking University 【貪心】 (ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)

stdout ins nts coo csdn another head under sting

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

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

題目鏈接:

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

題目大意:

  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的位置。

  然後將交通管制恢復,得到最終正確的答案。

技術分享
 1 /****************************************************
 2 
 3     Author : Coolxxx
 4     Copyright 2017 by Coolxxx. All rights reserved.
 5     BLOG : http://blog.csdn.net/u010568270
 6 
 7 ****************************************************/
 8 #include<bits/stdc++.h>
 9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double EPS=0.00001;
15 const int J=10;
16 const int MOD=1000000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=104;
20 using namespace std;
21 typedef long long LL;
22 double anss;
23 LL aans;
24 int cas,cass;
25 int n,m,lll,ans;
26 int a[N],b[N];
27 bool u[N];
28 int main()
29 {
30     #ifndef ONLINE_JUDGE
31 //    freopen("1.txt","r",stdin);
32 //    freopen("2.txt","w",stdout);
33     #endif
34     int i,j,k;
35     int x,y,z;
36 //    for(scanf("%d",&cass);cass;cass--)
37 //    init();
38 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
39     while(~scanf("%d",&n))
40     {
41         mem(u,0);
42         scanf("%d",&m);
43         for(i=0;i<n;i++)
44         {
45             scanf("%d",&a[i]);
46         }
47         scanf("%d",&cas);
48         for(i=1;i<=cas;i++)
49         {
50             scanf("%d",&x);
51             u[x]=1;
52         }
53         for(i=0,j=0;i<n;i++)
54         {
55             if(u[i])continue;
56             b[j++]=a[i];
57         }
58         ans=MAX;
59         for(i=0;i<=j-m;i++)
60         {
61             for(k=i+1;k<min(i+m,j);k++)
62             {
63                 if(b[i]+b[k]<ans)
64                 {
65                     ans=b[i]+b[k];
66                     y=i;z=k;
67                 }
68             }
69         }
70         for(i=0;i<n;i++)
71         {
72             if(u[i])continue;
73             if(!y)break;
74             y--;
75         }
76         y=i;
77         for(i=0;i<n;i++)
78         {
79             if(u[i])continue;
80             if(!z)break;
81             z--;
82         }
83         z=i;
84         printf("%d %d\n",y,z);
85     }
86     return 0;
87 }
88 /*
89 //
90 
91 //
92 */
View Code

hihoCoder 1578 Visiting Peking University 【貪心】 (ACM-ICPC國際大學生程序設計競賽北京賽區(2017)網絡賽)