CodeForces - 922D Robot Vacuum Cleaner (贪心)
Pushok the dog has been chasing Imp for a few hours already.
Fortunately, Imp knows that Pushok is afraid of a robot vacuum cleaner.
While moving, the robot generates a string t consisting of letters 's' and 'h', that produces a lot of noise. We define noise of string t as the number of occurrences of string "sh" as a subsequence in it, in other words, the number of such pairs (i, j), that i < j and and
.
The robot is off at the moment. Imp knows that it has a sequence of strings ti in its memory, and he can arbitrary change their order. When the robot is started, it generates the string t as a concatenation of these strings in the given order. The noise of the resulting string equals the noise of this concatenation.
Help Imp to find the maximum noise he can achieve by changing the order of the strings.
Input
The first line contains a single integer n (1 ≤ n ≤ 105) — the number of strings in robot's memory.
Next n lines contain the strings t1, t2, ..., tn, one per line. It is guaranteed that the strings are non-empty, contain only English letters 's' and 'h' and their total length does not exceed 105.
Output
Print a single integer — the maxumum possible noise Imp can achieve by changing the order of the strings.
Examples
4
ssh
hs
s
hhhs
18
2
h
s
1 题意:
给定n个字符串自由组合,求最多出现多少个sh这样的子序列。
思路:
对于s1,s2,产生字符串的个数就是S1.SH+s2*SH+max(s1.S*s2.H+s2.S*s1.H)
所以按照s1.S*s2.H-s2.S*s1.H排序即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); struct node{
ll S,H;
}a[maxn]; char s[maxn]; bool cmp(node a,node b){
return a.S*b.H>a.H*b.S;
} int main()
{
// ios::sync_with_stdio(false);
// freopen("in.txt","r",stdin); int n;
scanf("%d",&n);
ll ans=;
for(int i=;i<=n;i++){
scanf("%s",s);
for(int j=;s[j];j++){
if(s[j]=='s'){a[i].S++;}
else{
ans+=a[i].S;
a[i].H++;
}
}
} sort(a+,a++n,cmp); ll tmp=;
for(int i=;i<=n;i++){ ans+=a[i].H*tmp;
tmp+=a[i].S;
}
printf("%lld\n",ans); return ;
}
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 ...
- CF922D Robot Vacuum Cleaner 贪心+排序
正确的贪心方法:按照比例排序. code: #include <bits/stdc++.h> #define N 200000 #define ll long long #define s ...
- 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 922D】Robot Vacuum Cleaner
[链接] 我是链接,点我呀:) [题意] 让你把n个字符串重新排序,然后按顺序连接在一起 使得这个组成的字符串的"sh"子序列最多 [题解] /* * 假设A的情况好于B * 也就 ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Codeforces 583 DIV2 Robot's Task 贪心
原题链接:http://codeforces.com/problemset/problem/583/B 题意: 就..要打开一个电脑,必须至少先打开其他若干电脑,每次转向有个花费,让你设计一个序列,使 ...
- 489. Robot Room Cleaner扫地机器人
[抄题]: Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or block ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
随机推荐
- codechef Heavy-light Decompositions
Heavy-light Decompositions Problem Code: HLDOTSSubmit All submissions for this problem are available ...
- poj1459 最大流Dinic
比较简单. #include<stdio.h> #include<string.h> #include<queue> #define maxn 110 #defin ...
- python 解释器编码
- MaxCompute 费用暴涨之存储压缩率降低导致SQL输入量变大
现象:同样的SQL,每天处理的数据行数差不多,但是费用突然暴涨甚至会翻数倍. 分析: 我们先明确MaxCompute SQL后付费的计费公式:一条SQL执行的费用=扫描输入量 ️ SQL复杂度 ️ 0 ...
- 在 Deepin 15.9 安装 KiCad EDA 5.1 (2019-05-30)
在 Deepin 15.9 安装 KiCad EDA 5.1 Deepin (深度系统)是世界著名的 Linux 发行版. KiCad EDA 是世界最强大的开源 PCB 绘制软件. 尝试一:在深度商 ...
- 2018-8-10-WPF-轻量级-MVVM-框架入门-2.1.2
title author date CreateTime categories WPF 轻量级 MVVM 框架入门 2.1.2 lindexi 2018-08-10 19:16:51 +0800 20 ...
- SDUT-2088_数据结构实验之栈与队列十一:refresh的停车场
数据结构实验之栈与队列十一:refresh的停车场 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description refresh最近发 ...
- @codeforces - 1214F@ Employment
目录 @description@ @solution@ @accepted code@ @details@ @description@ 有 m 个城市围成一个圆环,编号为 1~m. 某公司有 n 个职 ...
- python环境测试MySQLdb、DBUtil、sqlobject性能
python环境测试MySQLdb.DBUtil.sqlobject性能 首先介绍下MySQLdb.DBUtil.sqlobject: (1)MySQLdb 是用于Python连接Mysql数据库的接 ...
- 第一次作业:C++ 函数重载
函数重载 函数重载是在C语言的学习中未涉及的新概念.我们在编程时经常会遇到这样一个问题:我们编写完一个函数准备准备调用时,一旦需要传入不同的数据类型的参数时,一个函数无法实现,我们又必须重写另一个或者 ...