当前位置:网站首页>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;
}边栏推荐
- Comprehensive cases
- Data extraction 1
- What is a rebound shell? What's the use of bouncing shells?
- API version control [eolink translation]
- Debug: generic related "unresolved external symbols"
- [MRCTF2020]Ezpop 1
- What are the software tuning methods? Let's see what Feiteng technology experts say about dragon lizard technology
- Development of three database general SQL code based on PG Oracle and MySQL
- Local Oracle reported ora-12514: tns: the listener cannot recognize the requested service at present
- Gossip: is rotting meat in the pot to protect students' rights and interests?
猜你喜欢

信息化项目风险控制与应用

Gossip: is rotting meat in the pot to protect students' rights and interests?

Redis configuration file download

软件调优方法有哪些?看看飞腾技术专家怎么说 | 龙蜥技术

数据提取2

SETTA 2020 国际学术会议即将召开,欢迎大家参加!

I can't figure out why MySQL uses b+ trees for indexing?

Things come to conform, the future is not welcome, at that time is not miscellaneous, neither love

Record a PG master-slave setup and data synchronization performance test process

The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions
随机推荐
"Intermediate and advanced test questions": what is the implementation principle of mvcc?
[netding cup 2020 Qinglong group]areuserialz (buuctf)
Internet of things industrial UART serial port to WiFi to wired network port to Ethernet Gateway WiFi module selection
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
"PHP Basics" uses echo statements to output information
Use of "PHP Basics" Boolean
What is a rebound shell? What's the use of bouncing shells?
How does slf4j configure logback?
What are the software tuning methods? Let's see what Feiteng technology experts say about dragon lizard technology
Five day travels to Beijing
CommonTitleBar hide left right
The code interface is a little automated
Methods of server network testing
浅谈数据安全
服务器网络测试的方法
[resolved] the new version of pychart (2022) connects to the server to upload files and reports an error of "command Rsync is not found in path", and the files cannot be synchronized
SSTI template injection
Leetcode54. Spiral matrix
Enhancement: BTE process introduction
On data security