【链接】 我是链接,点我呀:)

【题意】

让你把n个字符串重新排序,然后按顺序连接在一起
使得这个组成的字符串的"sh"子序列最多

【题解】

```java
/*
* 假设A的情况好于B
* 也就对应了A排在B的前面
* 那么
* As*Bh>Ah*Bs ①
* 现在假设B又比C来得好
* 那么有
* Bs*Ch>Bh*Cs ②
* 由①可得
* As/Ah>Bs/Bh
* 由②可得
* Bs/Bh>Cs/Ch
* 那么有
* As/Ah>As/Ch
* 即As*Ch>Ah*As
* 也就是说A排在C的前面比较优秀
* 也就是说这个大小关系有传递性
* 那么就直接排个序就好啦,按照两两之间排序的规则来写个比较函数就好了。
*/
```
StringBuilder比直接用字符串的"+"来得快

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = (int)1e5;
static class Task{ class Pair{
int s,h,i;
public Pair(int s,int h,int i) {
this.s = s;
this.h = h;
this.i = i;
}
} public void solve(InputReader in,PrintWriter out) {
Pair a[] = new Pair[N+10];
String s[] = new String[N+10];
int n;
n = in.nextInt();
for (int i = 1;i <= n;i++) {
s[i] = in.next();
int ss = 0,hh = 0;
for (int j = 0;j < (int)s[i].length();j++) {
char key = s[i].charAt(j);
if (key=='s') ss++;
if (key=='h') hh++;
}
a[i] = new Pair(ss,hh,i);
}
Arrays.sort(a, 1,n+1,new Comparator<Pair>() { @Override
public int compare(Pair A, Pair B) {
// TODO Auto-generated method stub
long As,Ah,Bs,Bh;
As = A.s;Ah = A.h;
Bs = B.s;Bh = B.h;
long temp1 =As*Bh;long temp2 =Ah*Bs;
if (temp1>temp2) {
return -1;
}else if (temp1==temp2) {
return 0;
}else {
return 1;
}
} });
StringBuilder sb = new StringBuilder();
for (int i = 1;i <= n;i++) {
sb.append(s[a[i].i]);
}
String v = sb.toString();
n = v.length();
long ans = 0;
long cnts = 0;
for (int i = 0;i < n;i++) {
if (v.charAt(i)=='s') {
cnts++;
}else if (v.charAt(i)=='h'){
ans = ans + cnts;
}
}
out.println(ans);
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 922D】Robot Vacuum Cleaner的更多相关文章

  1. Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)

    题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...

  2. CodeForces - 922D Robot Vacuum Cleaner (贪心)

    Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...

  3. Codeforces Round #461 (Div. 2) D. Robot Vacuum Cleaner

    D. Robot Vacuum Cleaner time limit per test 1 second memory limit per test 256 megabytes Problem Des ...

  4. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. centos6之前版本的启动流程

    centos6 之前的系统启动: linux启动流程: 1)加电自检 2)读取MBR,引导加载程序grub,完成grub的三个阶段. 3)加载系统内核kernel,执行系统初始化信息. 4)启动用户空 ...

  2. Objective-C 对象的类型与动态结合

    创建: 2018/01/21 更新: 2018/01/22 标题前增加 [Objective-C] 完成: 2018/01/24 更新: 2018/01/24 加红加粗属性方法的声明 [不直接获取内部 ...

  3. 洛谷 P1880 [NOI1995]石子合并

    题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...

  4. DHTML_____window对象方法

    <html> <head> <meta charset="utf-8"> <title>window对象方法</title&g ...

  5. SQLServer2005 维护计划 无法删除

    1.查看"维护计划"对象的ID use msdbselect * from sysmaintplan_plansselect * from sysmaintplan_logsele ...

  6. Asp.net:MVC认识

    用MVC框架好长一段时间,发现每天都是写业务代码,不想每天只为了工作而写代码,想把工作中认识的MVC框架,遇到的问题,有时候天天在用,但是不知道里面是什么东西,什么原理,为啥這样写等一系列问题.进行梳 ...

  7. scala-基础-映射(1)

    //映射(1)-构建,获取,更新,迭代,反转,映射(可变与不可变 互换) class Demo1 extends TestCase { //构建与获取 def test_create_^^(){ // ...

  8. 01-Entity FrameWork如何控制数据的变化

    在Entity Framework所有操作数据就是更新EF容器中的实体状态 public enum EntityState { Added = , Deleted = , Detached = , M ...

  9. (1)麻省理工:计算机科学和 Python 编程导论

    本门课用的语言是python2.7,我的主要学习语言是C++11,所以不是特殊说明,则认为和C++中的是一样的(不管是语法还是表达式),当然,也有我不懂而错认为与C++一样的东西~ Week1 第一讲 ...

  10. opencv边缘滤波

    2018-03-0422:16:11 import cv2 as cv import numpy as np def bi_demo (image): print ("ceshi" ...