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

【题意】

让你把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. css实现左边div固定宽度,右边div自适应撑满剩下的宽度

    (1)使用float <div class="use-float"> <div></div> <div></div> & ...

  2. Java多线程系列三——实现线程同步的方法

    两种实现线程同步的方法 方法 特性 synchronized 不需要显式地加解锁,易实现 ReentrantLock 需要显式地加解锁,灵活性更好,性能更优秀,结合Condition可实现多种条件锁 ...

  3. 内核的ramdisk

    ramdisk 内核中的特性之一,使用缓冲和缓存来加速对磁盘上的文件访问,并加载相应的硬件驱. ramdisk --> ramfs,提高速度 CentOS 5: initrd 工具程序:mkin ...

  4. js滚轮事件需要注意的兼容性问题

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. codeforces 37 E. Trial for Chief【spfa】

    想象成一层一层的染,所以相邻的两个格子连边,边权同色为0异色为1,然后答案就是某个格子到距离它最远得黑格子的最短距离的最小值 注意特判掉不需要染色的情况 #include<iostream> ...

  6. bzoj 1709: [Usaco2007 Oct]Super Paintball超级弹珠【枚举】

    k是1e5范围的,吗? 注意到n只有100,这意味着k去重之后之后n^2,也就是1e4! 然后就可以愉快的n^4枚举了,枚举每个格子,再枚举每个敌人,如果当前格子射不到敌人则退出,否则满足所有敌人则a ...

  7. svg image 图片无法铺满 circle 的问题解决

    引子 使用d3.js绘制了力布图后,需要在circle中绘制图片,方法如下: // 绘制图片 drawPattern(gContainer) { let that = this; let gPatte ...

  8. python自动化测试学习笔记-8多线程

    线程模块 python的多线程只能利用cpu的一个核心,一个核心同时只能运行一个任务那么为什么你使用多线程的时候,它的确是比单线程快答:如果是一个计算为主的程序(专业一点称为CPU密集型程序),这一点 ...

  9. Winform学习知识汇总

    引用博客 http://www.cnblogs.com/peterzb/archive/2009/06/14/1502918.html

  10. vs2012 jsoncpp 链接错误

    解决: 项目->属性->C/C++->代码生成->运行库->设置与使用的.lib的版本一致.