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.

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 */

时间: 2024-07-29 15:55:00

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

hihoCoder 1392 War Chess 【模拟】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1392 : War Chess 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Rainbow loves to play kinds of War Chess games. There are many famous War Chess games such as "Biography of Cao Cao", "Anecdotes of Wagang Mountain", etc. In this problem, let's c

hihocoder 1586 ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛-题目9 : Minimum【线段树】

https://hihocoder.com/problemset/problem/1586 线段树操作,原来题并不难..... 要求乘积最小只要求区间内最大值.最小值和绝对值小的数,再判断min,max正负就行了. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #define lson l,

hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)

#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a circle bouncing in a rectangle. As shown in the figure below, the rectangle is divided into N×M grids, and the circle fits exactly one grid. The bouncing

hihoCoder 1391 Countries 【预处理+排序+堆】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1391 : Countries 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 There are two antagonistic countries, country A and country B. They are in a war, and keep launching missiles towards each other. It is known that country A will launch N missiles. The i-th miss

hihoCoder 1389 Sewage Treatment 【二分+网络流+优化】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛)

#1389 : Sewage Treatment 时间限制:2000ms 单点时限:2000ms 内存限制:256MB 描述 After years of suffering, people could not tolerate the pollution in the city any more, and started a long-lasting protest. Eventually, the municipal government made up its mind to deal w

hihocoder 1584 Bounce (数学 &amp;&amp; 规律) ACM-ICPC北京赛区2017网络赛

题意: 给定一副n*m的格子图, 问从左上角的点开始往右下角滑,碰到墙壁就反弹, 碰到角落就停止, 问恰好经过一次的格子有多少个. 如图,恰好经过一次的格子有39个. 分析: 首先要引入两个概念, "路径长","格子数". 路径长指的是整段路程的长度,如果走过同一个格子两次那么就算是2步. 格子数指的是整段路程经过的格子. 如果一个图是9*9(形如n*n)的, 那么就是从左上角一直到右下角, 走过的"路径长"恰好等于"格子数"

《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就是一本题解书,简单暴力的通过题目的堆叠来提升解决编程问题的能力. 那么下面开始探索吧. zoj1037: BackgroundFor years, computer scientists have been trying to find efficient solutions to different computing p

第39届ACM国际大学生程序设计竞赛大陆地区赛站奖励方案

搬砖: 为了更好地吸引优秀选手参加第39届ACM国际大学生程序设计竞赛,全球赞助商的经费用于参赛队服和赛事的承办活动,中国赛区赞助商提供的经费用于优秀参赛队的奖励(包括奖金.奖牌.奖杯.获奖证书及奖品等), 竞赛命题,参赛手册,竞赛宣传方面的费用支出.每个赛区的具体奖励方式如下: 设冠军.亚军.季军三座奖杯,分别颁发奖金5000.3500.2000元人民币: 设15名金奖(包括冠.亚.季军)和30名银奖, 铜奖数量为参赛队数的30%: 获金奖的队(未获奖杯的队)每队颁发奖金1000元人民币; 获

第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安站

 第39届ACM国际大学生程序设计竞赛 亚洲区域赛(现场赛)西安赛区总结报告 报告人:田思明 队名:ACpioneer 队长:陈志阳,队员:顾振兴,田思明 西安区域赛告下帷幕,我和陈志阳,顾振兴组成的ACpioneer队最终获得了一块宝贵的铜牌.首先要感谢陈志阳和顾振兴两位杰出队友的努力训练和出色表现,我作为一个新人跟着他们学到了很多很多,也十分珍惜和他们在一起的训练,比赛时光,其次要感谢陈志老师,不辞辛劳陪我们5队和6队前往西安参加比赛,还要感谢集训队所有曾经帮过我们的所有队员们,记得cdy