SPOJ NWERC11E Please, go first

Please, go first

You are currently on a skiing trip with a group of friends. In general, it is going well: you enjoy the skiing during the day and, of course, the après-skiing during the night. However, there is one nuisance: the skiing lift. As always, it is too small, and can only serve one person every 5 seconds. To make matters worse, you and your friends generally don’t arrive simultaneously at the lift, which means that you spend time waiting at the bottom of the mountain for the lift and at the top again for your friends.

The waiting at the top is especially inefficient. In fact, you realize that if your friends haven’t arrived yet, you might as well let other people pass you in the queue. For you, it makes no difference, since otherwise you’d be waiting at the top. On the other hand, your actions might save them time if their friends have already arrived and are currently waiting for them at the top.

You are wondering how much time would be saved if everybody adopts this nice attitude. You have carefully observed the queue for a while and noticed which persons form groups of friends. Suppose someone lets another pass if doing this doesn’t change his own total waiting time, but saves time for the other person. Do this over and over again until it can’t be done anymore. How much time will this save, in total?

 

Input

On the first line a positive integer: the number of test cases, at most 100. After that per test case:

  • one line with an integer n (1 ≤ n ≤ 25 000): the number of people in the line for the lift.
  • one line with n alphanumeric characters (uppercase and lowercase letters and numbers): the queue. The first person in this line corresponds to the person at the head of the queue. Equal characters correspond to persons from the same group of friends.

 

Output

Per test case:

  • one line with an integer: the time saved, in seconds.

 

Sample in- and output

Input

Output

2
6
AABABB
10
Ab9AAb2bC2
15
45

这题太坑了 要靠自己理解    给你一队人 先求出原来的时间 再求出排好队之后的时间 求差

原来的人要等之前的人才一起上山  比如第二组的2有一个排在最后 所以 2要等最前面的人都走了才上 要等9个而且有2个2 所以时间为2*5*9

前面的2的时间不用算入了 这是最差的时间

优化的话  结果是9AAABBBC22 虽然2还是要等9个人朋友也要等9个人 但是C之前要等8个人 现在只要7人 同理



#include <stdio.h>
#include <string.h>
int main()
{
    int cas,n,sum[300],i;
    char a[25005];
    scanf("%d",&cas);
    while(cas--)
    {
        scanf("%d %s",&n,a);
        memset(sum,0,sizeof sum);
        for(i=0;a[i]!=0;i++)
            sum[a[i]]++;
        int ans1=0,ans2=0;
        for(i=n-1;i>=0;i--)
        {
            if(sum[a[i]])
            {
                ans1+=i*5*sum[a[i]];
                ans2+=(n-1)*5*sum[a[i]];
                n-=sum[a[i]];
                sum[a[i]]=0;
            }
        }
        printf("%d\n",ans1-ans2);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
洛谷的SPOJ需要注册一个SPOJ账号并进行绑定才能进行交题。您可以按照以下步骤进行注册: 1. 打开洛谷网站(https://www.luogu.com.cn/)并登录您的洛谷账号。 2. 在网站顶部导航栏中找到“题库”选项,将鼠标悬停在上面,然后选择“SPOJ”。 3. 在SPOJ页面上,您会看到一个提示,要求您注册SPOJ账号并进行绑定。点击提示中的链接,将会跳转到SPOJ注册页面。 4. 在SPOJ注册页面上,按照要求填写您的用户名、密码和邮箱等信息,并完成注册。 5. 注册完成后,返回洛谷网站,再次进入SPOJ页面。您会看到一个输入框,要求您输入刚刚注册的SPOJ用户名。输入用户名后,点击“绑定”按钮即可完成绑定。 现在您已经成功注册并绑定了SPOJ账号,可以开始在洛谷的SPOJ题库上刷题了。祝您顺利完成编程练习!\[1\]\[2\] #### 引用[.reference_title] - *1* *3* [(洛谷入门系列,适合洛谷新用户)洛谷功能全解](https://blog.csdn.net/rrc12345/article/details/122500057)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [luogu p7492 序列](https://blog.csdn.net/zhu_yin233/article/details/122051384)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值