codeforces日记371e

本文介绍了一种算法,用于从现有地铁站中选择最优的k个站点以最小化乘客平均通勤时间。通过排序和计算不同站点组合间的距离,该算法能够有效地找到最佳方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:

E. Subway Innovation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland is going through tough times — the dirt price has dropped and that is a blow to the country's economy. Everybody knows that Berland is the top world dirt exporter!

The President of Berland was forced to leave only k of the currently existing n subway stations.

The subway stations are located on a straight line one after another, the trains consecutively visit the stations as they move. You can assume that the stations are on the Ox axis, the i-th station is at point with coordinate xi. In such case the distance between stations iand j is calculated by a simple formula |xi - xj|.

Currently, the Ministry of Transport is choosing which stations to close and which ones to leave. Obviously, the residents of the capital won't be too enthusiastic about the innovation, so it was decided to show the best side to the people. The Ministry of Transport wants to choose such k stations that minimize the average commute time in the subway!

Assuming that the train speed is constant (it is a fixed value), the average commute time in the subway is calculated as the sum of pairwise distances between stations, divided by the number of pairs (that is ) and divided by the speed of the train.

Help the Minister of Transport to solve this difficult problem. Write a program that, given the location of the stations selects such kstations that the average commute time in the subway is minimized.

Input

The first line of the input contains integer n (3 ≤ n ≤ 3·105) — the number of the stations before the innovation. The second line contains the coordinates of the stations x1, x2, ..., xn ( - 108 ≤ xi ≤ 108). The third line contains integer k (2 ≤ k ≤ n - 1) — the number of stations after the innovation.

The station coordinates are distinct and not necessarily sorted.

Output

Print a sequence of k distinct integers t1, t2, ..., tk (1 ≤ tj ≤ n) — the numbers of the stations that should be left after the innovation in arbitrary order. Assume that the stations are numbered 1 through n in the order they are given in the input. The number of stations you print must have the minimum possible average commute time among all possible ways to choose k stations. If there are multiple such ways, you are allowed to print any of them.

Sample test(s)
input
3
1 100 101
2
output
2 3 
Note

In the sample testcase the optimal answer is to destroy the first station (with x = 1). The average commute time will be equal to 1 in this way.

集体思路:

将输入的车站以及位置用pair保存,按位置进行排序。可知所求的必为排序后连续的k个车站,计算前k个车站所有对的距离之和(后发现不用计算即代码中注释掉的,只用看距离的大小即可),根据前一个计算后推一个车站的距离之和,记录最小值并输出。

AC代码:

#include<iostream>
#include<algorithm>
using namespace std;
pair<int, int> x[300008];
long long S[300008],i,n,k,m,mi,f;

int main() {
 for (cin>>n; i<n; i++) {
  cin >> x[i].first;
  x[i].second = i+1;
 }
 cin>>k; k--;
 sort(x, x+n);
 for (i=1; i<n; i++) S[i] = S[i-1] + x[i].first;
 f=0;
 //for (i=1; i<=k; i++) f += x[i].first * i - S[i-1];
 for (m=f,i=1; i<n-k; i++)
  if ((f += x[i+k].first*k-2*S[i+k-1]+2*S[i-1]+x[i-1].first*k) < m) m=f,mi=i;
 for (i=0; i<=k; i++)
  cout << x[mi+i].second << " ";
}

### Codeforces Round 1007 Problem E 解析 对于Codeforces1007场比赛中的E题,虽然具体题目描述未直接提供,但从相似类型的编程竞赛问题可以推测该类问题通常涉及算法设计和优化。 在处理此类问题时,理解输入输出格式至关重要。例如,在某些情况下,程序需打印单个整数作为解答[^1]。这表明最终解决方案应简洁明了地给出计算结果。 当面对复杂度较高的挑战时,可能涉及到特定的数据结构或算法技巧的应用。比如位运算操作(如异或),其定义为对两个相同长度的二进制数按位执行逻辑异或操作;如果仅有一个位置上的比特不同,则结果为1,否则为0[^5]。 针对此轮次的具体问题E,建议参考同系列其他赛题解析视频来获取灵感与思路[^2]。这些资源往往能提供有效的提示以及实现细节说明,有助于理解和解决当前遇到的问题。 此外,注意遵循交互式查询限制条件下的最佳实践,确保不超过规定的最大询问次数,并正确响应错误情况以避免不必要的惩罚分数损失[^3]。 最后,考虑到多组测试数据的可能性,应当合理规划代码框架以便高效处理多个实例而不会超时或违反约束条件[^4]。 ```cpp // 示例C++代码模板用于处理多组测试案例 #include <iostream> using namespace std; int main() { int T; cin >> T; // 输入测试用例数量 while(T--) { // 处理每组测试数据 int n, k; cin >> n >> k; // 实现具体的业务逻辑 cout << "Output based on logic" << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值