修桥问题
来源:互联网 发布:警方破获网络赌球案 编辑:程序博客网 时间:2024/05/16 16:12
Description
河上有一座桥,一场大洪水之后,桥上的 n 块木板被冲走了,只剩了一个支架立在水中间。现在你要去到桥的另一端买零食,所以需要买来木板修补桥面。
桥一共有 d 个支架,每个支架上需要长度为 1 的木板。而木材店的老板很懒,他不愿意把每块木板都切成 1 个长度的,他最多愿意给你提供 m 块木板。
你知道缺少木板的支架的编号,你要购买尽量短的木板来修桥,因为剩下的钱你就可以去买吃的了。
Input
输入仅包含 1 组测试数据
测试数据的第一行有3个整数 m d n(1 <= m <= 50 1 <= d <= 200 1 <= n <= d)
接着有 n 行,每行表示一个需要修补的支架的编号。
Output
输出你需要购买的最短的木板总长度
4 50 18
3
4
6
8
14
15
16
17
21
25
26
27
30
31
40
41
42
43
Sample Output
void bubbleSort(int a[],int lenght){
int i, j, temp;
for (j = 0; j < lenght - 1; j++)
for (i = 0; i < lenght - 1 - j; i++)
{
if(a[i] < a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
//排序,小到大
void _bubbleSort(int a[],int lenght){
int i, j, temp;
for (j = 0; j < lenght - 1; j++)
for (i = 0; i < lenght - 1 - j; i++)
{
if(a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
int main()
{
int m,d,n;//m:木板块数;d:总支架数;n:被冲掉木板数
int sum;
int a[200],b[200];//a记录冲掉木板位置,b记录相邻两块被冲掉木板的距离
cin>>m>>d>>n;
for(int i=0;i<n;i++)
cin>>a[i];
_bubbleSort(a,n);//从小到大排序
int Min=a[0];int Max=a[n-1];
if(m>=n)//提供的木板块数不小于被冲走的木板块数
sum=n;
else
{
sum=Max-Min;//假定需要的长度为Max-Min
for(int i=0;i<n-1;i++)
b[i]=a[i+1]-a[i];//计算相邻两块被冲掉木板的距离
bubbleSort(b,n-1);//从大到小排序
for(int i=0;i<m-1;i++)//取出两块被冲掉的木板间的最大差值到第m大差值
sum=sum-b[i];//不修
sum=sum+m;
}
cout<<sum;
return 0;
}
河上有一座桥,一场大洪水之后,桥上的 n 块木板被冲走了,只剩了一个支架立在水中间。现在你要去到桥的另一端买零食,所以需要买来木板修补桥面。
桥一共有 d 个支架,每个支架上需要长度为 1 的木板。而木材店的老板很懒,他不愿意把每块木板都切成 1 个长度的,他最多愿意给你提供 m 块木板。
你知道缺少木板的支架的编号,你要购买尽量短的木板来修桥,因为剩下的钱你就可以去买吃的了。
Input
输入仅包含 1 组测试数据
测试数据的第一行有3个整数 m d n(1 <= m <= 50 1 <= d <= 200 1 <= n <= d)
接着有 n 行,每行表示一个需要修补的支架的编号。
Output
输出你需要购买的最短的木板总长度
Sample Input
4 50 18
3
4
6
8
14
15
16
17
21
25
26
27
30
31
40
41
42
43
Sample Output
25
C++实现
#include<iostream>
using namespace std;
void bubbleSort(int a[],int lenght){
int i, j, temp;
for (j = 0; j < lenght - 1; j++)
for (i = 0; i < lenght - 1 - j; i++)
{
if(a[i] < a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
//排序,小到大
void _bubbleSort(int a[],int lenght){
int i, j, temp;
for (j = 0; j < lenght - 1; j++)
for (i = 0; i < lenght - 1 - j; i++)
{
if(a[i] > a[i + 1])
{
temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
}
}
}
int main()
{
int m,d,n;//m:木板块数;d:总支架数;n:被冲掉木板数
int sum;
int a[200],b[200];//a记录冲掉木板位置,b记录相邻两块被冲掉木板的距离
cin>>m>>d>>n;
for(int i=0;i<n;i++)
cin>>a[i];
_bubbleSort(a,n);//从小到大排序
int Min=a[0];int Max=a[n-1];
if(m>=n)//提供的木板块数不小于被冲走的木板块数
sum=n;
else
{
sum=Max-Min;//假定需要的长度为Max-Min
for(int i=0;i<n-1;i++)
b[i]=a[i+1]-a[i];//计算相邻两块被冲掉木板的距离
bubbleSort(b,n-1);//从大到小排序
for(int i=0;i<m-1;i++)//取出两块被冲掉的木板间的最大差值到第m大差值
sum=sum-b[i];//不修
sum=sum+m;
}
cout<<sum;
return 0;
}
0 0
- 修桥问题
- 七桥问题
- 七桥问题
- 网桥的问题
- <算法>七桥问题
- pta--七桥问题
- 七桥问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 问题
- 2016年360校招笔试题
- Oracle存储过程、存储函数
- 导航界面的搭建(完整demo)
- Java垃圾收集算法
- Quartz.NET教程_Lesson 11&Lesson 12(完)
- 修桥问题
- Field requires API level 5 (current min is 1): android.util.Pair#first
- 【安卓学习之常见问题】 ScrollView与其他组件的冲突问题
- 2016年阿里校招笔试题(JAVA研发岗)
- 【LeetCode-94】Binary Tree Inorder Traversal(C++)
- android学习第一天
- MFC软件运行时按下回车或ESC闪退问题的解决
- java.输出数字
- Strman-java – 好用的字符串处理工具