当前位置:网站首页>All in one 1319 - queue for water
All in one 1319 - queue for water
2022-07-27 08:14:00 【Bamboo monk-】
【 Title Description 】
Yes n People line up in front of a tap to get water , Suppose everyone gets water for Ti, Please program to find this n An order in which individuals line up , bring n The average waiting time of an individual is the smallest .
【 Input 】
There are two lines , First act n(1≤n≤1000); The second line represents the second 1 Personal to the third n Each person's water receiving time T1,T2,…,Tn Between each data is 1 A space .
【 Output 】
There are two lines , The first behavior is a queuing order , namely 1 To n An arrangement of ; The second behavior is the average waiting time under this arrangement scheme ( The output is accurate to two decimal places ).
【 sample input 】
10 56 12 1 99 1000 234 33 55 99 812
【 sample output 】
3 2 7 8 1 4 9 6 10 5 291.90
Topic summary :
The first i Personal waiting time = The first i-1 Personal water connection time + Waiting time , Our task is to minimize the average waiting time , That is, everyone should wait for the shortest time , Let the person who needs a short time get the water first . This problem requires a greedy algorithm .
analysis :
1. Let the time be short , Sorting is required
2. The average time is accumulated directly when entering , Directly divide the output by the total number of people
3. The output is required to keep two decimal places , Use printf
Code :
#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;
}边栏推荐
- Download and usage of sequel Pro
- mqtt指令收发请求订阅
- 一文速览EMNLP 2020中的Transformer量化论文
- Solid smart contract development - 3.3-solid syntax control structure
- Gossip: is rotting meat in the pot to protect students' rights and interests?
- The seta 2020 international academic conference will be held soon. Welcome to attend!
- How to obtain the cash flow data of advertising services to help analyze the advertising effect?
- 瑞芯微RK3399-I2C4挂载EEPROM的修改案例
- containerd拉取私库镜像失败(kubelet)
- JS access cookie example
猜你喜欢

1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward

redis配置文件下载

How to update PIP3? And running PIP as the 'root' user can result in broken permissions and conflicting behavior

北京五日游记

Attack and defense world MFW

Combined use of C WinForm form form event and delegate

如何获取广告服务流量变现数据,助力广告效果分析?

Usage scenarios for automated testing

idea远程调试

pytorch_demo1
随机推荐
vCenter7.0管理Esxi7.0主机
How does slf4j configure logback?
Dasctf2022.07 enabling game password WP
I can't figure out why MySQL uses b+ trees for indexing?
After installing mysql, docker entered the container and found that he could not log in to MySQL
物来顺应,未来不迎,当时不杂,既过不恋
一文速览EMNLP 2020中的Transformer量化论文
C commissioned use cases
Lu Xun: I don't remember saying it, or you can check it yourself!
[golang] golang develops wechat official account web page authorization function
Data extraction 2
浅谈数据安全
CommonTitleBar hide left right
抽象工厂模式
The response of the database interface is very slow
Lua有状态迭代器
代码接口自动化的有点
Debug: generic related "unresolved external symbols"
Promise details
Internet of things industrial UART serial port to WiFi to wired network port to Ethernet Gateway WiFi module selection