当前位置:网站首页>一本通1319——排队接水
一本通1319——排队接水
2022-07-27 05:04:00 【竹林居士-】
【题目描述】
有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小。
【输入】
共两行,第一行为n(1≤n≤1000);第二行分别表示第1个人到第n个人每人的接水时间T1,T2,…,Tn每个数据之间有1个空格。
【输出】
有两行,第一行为一种排队顺序,即1到n的一种排列;第二行为这种排列方案下的平均等待时间(输出结果精确到小数点后两位)。
【输入样例】
10 56 12 1 99 1000 234 33 55 99 812
【输出样例】
3 2 7 8 1 4 9 6 10 5 291.90
题意概括:
第i个人的等待时间=第i-1个人的接水时间+等待时间,我们的任务是让平均等待时间最短,也就是每个人等待时间要最短,让需要时间短的人先接水。此题要用道贪心算法。
分析:
1.让时间短的在前,需要用到排序
2.平均时间直接在输入时累加,输出时直接除以总人数即可
3.题中要求输出保留两位小数,要用printf
代码:
#include<bits/stdc++.h>
using namespace std;
struct node{
int id;
int t;
}a[1005];
bool cmp(node x,node y)
{
return x.t<y.t;
}
int main()
{
double ans=0;
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i].t;
a[i].id=i;
}
sort(a+1,a+n+1,cmp);
int j=1;
for(int i=n-1;i>0;i--)
{
ans+=i*a[j].t;
j++;
}
for(int i=1;i<=n;i++) cout<<a[i].id<<" ";
cout<<endl;
printf("%.2f",ans/n);
return 0;
}边栏推荐
猜你喜欢

页面布局中元素定位的几种方式

权限展示-左侧列表动态化

分享一道关于程序编译过程的选择题(内含编译过程浅谈,符号表的形成合并过程)

初始C语言——关键字static的作用

Trying to evolve_ My first CSDN blog

背景图片相关应用-铺满,自适应

Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)

Multiplication sorting in torch, * & torch. Mul () & torch. MV () & torch. Mm () & torch. Dot () & @ & torch. Mutmal ()

编辑删除用户

初识C语言——初识指针
随机推荐
维持登录,路由跳转
2021 Niuke multi school training camp 5 (question b)
Elment UI usage
商品图片的管理
C language string function: StrCmp, strncpy, strncat, strncmp, strstr, strtok, strError
MD5 password encryption
初识C语言——为什么每个C程序都有一个main函数
C语言做一个小迷宫
C语言初阶——分支语句(if,switch)
Introduction to C language functions
JS中arguments类数组
后台实现sku 管理
How to get started quickly and strengthen learning?
First knowledge of C language -- what is C language
while循环
Looking at the PK of alphago and Li Shishi from a deep perspective
Qsort - the sorting function in C language (with void*, callback function knowledge points
一本通1251——仙岛求药(广度优先搜索)
JS中什么是DOM和BOM
程序环境和预处理(下):#define、#undef、命令行编译、条件编译、文件包含(超全整理,建议收藏!!!