【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- codechef : TREDEG , Trees and Degrees
其实有原题,生成树计数 然鹅这题里面是两道题, 50pts 可以用上面那题的做法直接过掉,另外 50pts 要推推式子,搞出 O n 的做法才行(毕竟多项式常数之大您是知道的) 虽说这道题里面是没有 ...
- [转]深入C语言内存区域分配(进程的各个段)详解
一般情况下,一个可执行二进制程序(更确切的说,在Linux操作系统下为一个进程单元,在UC/OSII中被称为任务)在存储(没有调入到内存运行)时拥有3个部分,分别是代码段(text).数据段(data ...
- ACM_同余+暴力找规律
小光的忧伤 Time Limit: 2000/1000ms (Java/Others) Problem Description: 锴神:我尊重作者原意,你们出什么我就加什么.于是小光打了道水题(也就是 ...
- WCF 相关配置
WCF错误:413 Request Entity Too Large 在我们用WCF传输数据的时候,如果启用默认配置,传输的数据量过大,经常会出这个错误. WCF包含服务端与客户端,所以这个错误可能出 ...
- Ceph在手,天下我有
有人问我,你是如何做到统一存储的?我微微一笑,大声告诉他:Ceph在手,天下我有. Ceph是一个统一的分布式存储系统,旨在实现出色的性能,可靠性和可扩展性.认了OpenStack做大哥之后更是一发不 ...
- 【工具】Github
项目目录结构设计与git远程仓库的建立 git码云仓库建立:在码云网站上新建组织和项目. 配置sshkey认证和公钥:命令行ssh-keygen -t rsa -C "xxxxx@xxxxx ...
- [C#源码]自动更改桌面背景
操作代码:ChangeDesktop.cs using System;using System.Collections.Generic;using System.ComponentModel;usin ...
- WinRT ListView间隔变色(一)
我们知道,在WPF里,MSDN提供了三种方法 1.使用转换器Converter 2.继承ListView类,自己处理 3.使用StyleSelctor 到了WinRT的世界了 1. Winrt中Set ...
- HDU_1072_Nightmare
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目描述:矩阵表示迷宫,0表示墙,1表示路,2表示起点,3表示终点,4表示重置炸弹时间(6秒),你需 ...
- 关于Apache mod_rewrite的中文配置、使用和语法介绍(实现URL重写和防盗链功能)
以数据库后台驱动的动态内容的网站,经常会遇到这些的问题: 当在浏览器的地址栏输入一个无效的参数时,会出现数据库的错误提示,这是一个安全的隐患 搜索引擎无法收录你的所有网页 网页的链接地址是一系列的参数 ...