Greedy---hnu 13320 please, go first

Source: Internet
Author: User

Please
, go
first
problem ' s Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13320

 

Mean:

n individuals to go skiing together, to take the elevator to the top of the mountain, elevators every 5 minutes can send a person up. These n individuals have a group of good groups to ski together, so you have to wait until the alignment to start sliding.

But they arrived in the elevator time is not uniform, for a single person, he is in the mountain and waiting in the mountain is the same time.

But for the collective of n individuals, it may be more time-saving if you let the person behind him go first.

How much time can be saved by adjusting the order of the elevators. (PS: By this ghost test instructions pit Dead ==| | )

Analyse:

A typical greedy problem.

Let's start by analyzing the states that were not adjusted before:

For a person, no matter how early you come, you still have to wait until the last person with your regiment to start skiing.

Where is this waste of time? If this is the case: AABBBBBBBBBBBBBBABBB, if we become aaabbbbbbbbbbbbbbbbb,

Although the time of Team B hasn't changed, it has saved a lot of time for Team A. The place to be greedy is here.

What about greed?

the first thing to be clear is that we cannot advance the last person in the same regiment (people are not yet), but only the middle-doped group of people ahead,

And the best situation is certainly: let the same group as far as possible aggregation together, so the time to wait is the least.

Combining these two conditions, we first find the leftmost position of each element before the formation is not adjusted.

The array is then sorted by string (1. People of the same team are aggregated together; 2. The different teams are sorted by the last person's position).

Finally, for each element, we seek the difference between the pre-adjustment and the adjusted position of this element, accumulating, and finally getting the answer.

Time complexity:o (N)

 

Source code:

/*
* This code was made by Crazyacking
* verdict:accepted
* Submission date:2015-07-26-22.01
* time:0ms
* memory:137kb
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define LL Long Long
#define ULL unsigned long long
using namespace STD;
Const int MAXN = 25010;
CharS[MAXN];
int ORP[ the], NRP[ the], T,N
BOOL CMP(Char a, Charb){return ORP[a] < ORP[B];}
int Main()
{
ios_base::Sync_with_stdio(false);
Cin.Tie(0);
Cin >> T;
while(T--)
{
Cin >>N>>S
for(int I = 0;I <N++I)ORP[S[I]] = I;
Sort(s,S+N, CMP);
for(int I = 0;I <N++I)NRP[S[I]] = I;
Long Long ans = 0;
for(int I = 0;I <N++I)ans += ABS(ORP[S[I]] - NRP[S[I]]);
cout << ans * 5 << Endl;
}
return 0;
}
/*

*/

Greedy---hnu 13320 please, go first

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.