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

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

【题解】

        /** 假设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 herenew 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>() {@Overridepublic int compare(Pair A, Pair B) {// TODO Auto-generated method stublong 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());}}
}

转载于:https://www.cnblogs.com/AWCXV/p/10582043.html

【Codeforces 922D】Robot Vacuum Cleaner相关推荐

  1. 【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)

    题干: A string t is called an anagram of the string s, if it is possible to rearrange letters in t so ...

  2. 【CodeForces - 574B】Bear and Three Musketeers (枚举边,思维,优秀暴力)

    题干: Do you know a story about the three musketeers? Anyway, you will learn about its origins now. Ri ...

  3. 【CodeForces - 608C】Chain Reaction (二分 或 dp ,思维)

    题干: 题目大意: 题意是在一条直线上坐落着不同位置的灯塔,每一个灯塔有自己的power level,当作是射程范围.现在从最右边的灯塔开始激发,如果左边的灯塔在这个灯塔的范围之内,那么将会被毁灭.否 ...

  4. 「一题多解」【CodeForces 85D】Sum of Medians(线段树 / 分块)

    题目链接 [CodeForces 85D]Sum of Medians 题目大意 实现一个setsetset,支持插入,删除,求∑a5k+3∑a5k+3\sum a_{5k+3}.注意,setsets ...

  5. 【CodeForces 997C】Sky Full of Stars(组合计数)

    题目链接:[CodeForces 997C]Sky Full of Stars 官方题解:Codeforces Round #493 - Editorial 题目大意:有一个n×nn×nn\times ...

  6. 【codeforces 752C】Santa Claus and Robot

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  7. 【codeforces 812C】Sagheer and Nubian Market

    [题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...

  8. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  9. 【codeforces 711B】Chris and Magic Square

    [题目链接]:http://codeforces.com/contest/711/problem/B [题意] 让你在矩阵中一个空白的地方填上一个正数; 使得这个矩阵两个对角线上的和; 每一行的和,每 ...

最新文章

  1. Ubuntu Tensorflow object_detection API 目标检测环境搭建
  2. js倒计时代码最简单的_32个史上最有用的js代码
  3. 为treeview添加客户端事件
  4. HDU 2125 Local area network
  5. 无监督和有监督的区别_无监督元学习(Unsupervised Meta-Learning)
  6. 修改Docker默认存储位置的方法
  7. mui拓展:flex布局:如何设置x轴横向隐藏,而又往左排列优先
  8. Linux编程 文件操作,linux高级编程(文件操作)
  9. Windows下判断文件是否存在
  10. 2016/4/22 图形用户界面
  11. vue项目中vue-router的使用
  12. getvalue函数怎么使用_单片机基础 —— 使用ADC读取电压值
  13. Pandas简单写入数据到csv文件
  14. Android应用文本字体设置
  15. php alert弹出框位置,jQuery_基于jquery的弹出提示框始终处于窗口的居中位置(类似于alert弹出框的效果),原理很简单: 获取当前屏幕( - phpStudy...
  16. FFmpeg开发实战(六):jpeg转换为yuv格式图像
  17. 假设检验实例(python)
  18. 服务器性能管理系统,服务器系统性能管理是什么
  19. TPS和事务响应时间的关系、计算公式
  20. 考研操作系统【1.1 操作系统的基本概念】

热门文章

  1. docker 删除默认连接_Net Core 中 Docker应用
  2. python语言程序设计基础第二版第七章答案-Python核心编程第二版 第七章课后答案...
  3. 爬虫python的爬取步骤-python爬虫实战之爬取京东商城实例教程
  4. python简单代码表白-如何正确使用Python进行表白
  5. python官网安装步骤-新手Windows下Python下载安装教程及配置注意事项
  6. python画曲线图例-Python数据可视化之Matplotlib(折线图)
  7. 用python绘制柱状图标题-Python笔记:用Python绘制炫酷的柱形图
  8. python3菜鸟教程-Python3 循环语句
  9. python 画三维函数图-Python画三维图-----插值平滑数据
  10. python 画三维函数图-Python之Numpy:二元函数绘制/三维数据可视化/3D