【Codeforces 922D】Robot Vacuum Cleaner
【链接】 我是链接,点我呀:)
【题意】
让你把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的更多相关文章
- Codeforces 922 C - Robot Vacuum Cleaner (贪心、数据结构、sort中的cmp)
题目链接:点击打开链接 Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that ...
- CodeForces - 922D Robot Vacuum Cleaner (贪心)
Pushok the dog has been chasing Imp for a few hours already. Fortunately, Imp knows that Pushok is a ...
- 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 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- npm更换为淘宝镜像源
1.通过config命令 1 2 npm config set registry http://registry.cnpmjs.org npm info underscore (如果上面配置正确这 ...
- [Swift通天遁地]一、超级工具-(13)使用PKHUD制作各种动态提示窗口
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 用 python 写一个模拟玩家移动的示例
实例:二维矢量模拟玩家移动 在游戏中,一般使用二维矢量保存玩家的位置,使用矢量计算可以计算出玩家移动的位置,下面的 demo 中,首先实现二维矢量对象,接着构造玩家对象,最后使用矢量对象和玩家对象共同 ...
- Cmake编译protobuf
编译指令,在powershell中执行 : .\protoc.exe .\ive.proto --cpp_out . .\protoc.exe .\ive.proto --csha ...
- Codefoces 828C
C. String Reconstruction time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Win7上安装Oracle数据库
由于ORACLE并没有FOR WIN7的版本,必须下载for vista_w2k8这个版本,将oralce 10G的安装镜像解压到硬盘,然后修改安装目录下的rehost.xml和oraparam.in ...
- 在idea中为类和方法自动生成注释
https://my.oschina.net/mojiayi/blog/1608746
- SQL远程连接
一.添加远程连接EXEC sp_addlinkedserver @server = '254', @srvproduct = '',--链接服务器的 OLE DB 数据源的产品名称 @provi ...
- gdb如何保存和读取断点
刚开始在linux下学编程使用gdb的同学可能会发现,每次用gdb设置断点调试程序,但下次打开的时候所有断点都没有了,很不方便.下面介绍保存和读取断点的方法. 1. 保存断点 先用info b 查看一 ...
- mongo 3.4分片集群系列之八:分片管理
这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...