CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik
个人感觉是很好的一道题
这道题乍一看我们无从下手,那我们就先想想怎么打暴力
暴力还不简单?枚举所有字串,再枚举所有位置,算出所有答案不就行了
我们自然不能无脑暴力,但是暴力可以给我们启发
我们知道所有对答案做出贡献的字符一定是相同的(废话)
所以我们可以O(n^2)首先枚举两个字符串中相同的字符然后再考虑如何贡献
然后计算出所有的方案下的值,再除以n*(n+1)*(2*n+1)/6 [不知道式子怎么来的去面壁]
我们发现:如果这两个相同的字符要做出贡献,那么它们一定在字串相同的位置
换句话说,这两个字符左边被选择的字符一定要同样多(同样的,此时右侧的字符一定也一样多)
比如:(下标均从1开始)
s1 : abbaca
s2 : baabac
这里面如果s1[1],s2[2]两个相同的字符'a'要做出贡献,那么左边必须有0 ,1 , 2 , 3 ...
我们发现左边一个字符都不能有
那如果s1[3],s2[4]做出贡献呢?左边只能有2个字符:因为其中下标的最小值是3,3-1=2
所以我们发现每次左边最多的字符数量只与最靠左的字符有关
相应的,每次右边最多的字符的数量只与最靠右的字符有关。
那,贡献呢??
我们可以枚举最靠右的字符,再去计算和那些在他左侧的字符对答案做出贡献
假如我们现在确定了s1[4],考虑让其做出贡献。
在 <= 4 的情况中
s2[2] 做出贡献 : (6-4)*(2)
s2[3] 做出贡献 : (6-4)*(3)
那么我们发现,每个贡献只与另一个字符串中 <= pos 的字符的下标之和有关
所以我们线性扫一遍,过程累加下标和,直接O(n)计算即可
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
x=;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=*x+ch-'',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = ;
char s1[maxn],s2[maxn];
double a[],b[];
int main(){
int n;read(n);
scanf("%s%s",s1+,s2+);
double ans = ;
for(int i=;i<=n;++i){
b[s2[i]] += i;
ans += 1LL*(n-i+)*(a[s2[i]] + b[s1[i]]);
a[s1[i]] += i;
}
printf("%.20lf\n",6.0*ans/n/(n+)/(n<<|));
getchar();getchar();
return ;
}
CodeForces - 204C Little Elephant and Furik and Rubik的更多相关文章
- Codeforces D. Little Elephant and Interval(思维找规律数位dp)
题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...
- CodeForces 259A Little Elephant and Chess
Little Elephant and Chess Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d &am ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- Codeforces 204A Little Elephant and Interval
http://codeforces.com/problemset/problem/204/A 题意:给定一个[L,R]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...
- Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp
Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...
- Codeforces 258C Little Elephant and LCM
Little Elephant and LCM #include<bits/stdc++.h> #define LL long long #define fi first #define ...
- [Codeforces 204E] Little Elephant and Strings
[题目链接] https://codeforces.com/contest/204/problem/E [算法] 首先构建广义后缀自动机 对于自动机上的每个节点 , 维护一棵平衡树存储所有它所匹配的字 ...
- CodeForces - 258D Little Elephant and Broken Sorting
Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of al ...
- CodeForces - 205B - Little Elephant and Sorting
先上题目: Little Elephant and Sorting time limit per test 1 second memory limit per test 256 megabytes i ...
随机推荐
- apache common包 CollectionUtils 使用 详解
集合判断: 例1: 判断集合是否为空: CollectionUtils.isEmpty(null): true CollectionUtils.isEmpty(new ArrayList()): t ...
- 程序猿的量化交易之路(32)--Cointrade之Portfolio组合(19)
转载须注明出处:http://blog.csdn.net/minimicall?viewmode=contents,http://cloudtrade.top/ Portfolio:组合,代表的是多个 ...
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For e ...
- hibernate3中session.get()与session.load()两个方法的区别?
1.发送sql的时机不同:load采用延迟加载技术,只有当真正去使用所查处的对象时,才会发送sql:get采用的是立即检索技术,当执行到get方法是就会立即发送sql. 2.返回的对象不同:load返 ...
- linux下ejabberd框架搭建
ejabberd为erlang的IM的开源框架,一直想找个时间研究研究: 1.下载Ejabberd安装包 wget http://www.process-one.net/downloads/ejabb ...
- difference between VM, Docker and Vagrant区别
VM: VirtualBox, VMware Docker Vagrant using which you can create VMs or container. It interacts wit ...
- 进程间通信(IPC)+进程加锁解锁
[0]README 0.1) source code and text description are from orange's implemention of a os: 0.2) for com ...
- 【BZOJ3319】黑白树 并查集
[BZOJ3319]黑白树 Description 给定一棵树,边的颜色为黑或白,初始时全部为白色.维护两个操作:1.查询u到根路径上的第一条黑色边的标号.2.将u到v 路径上的所有边的颜色设为 ...
- Hadoop实战-Flume之自定义Sink(十九)
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...
- 【题解】Cats Transport (斜率优化+单调队列)
[题解]Cats Transport (斜率优化+单调队列) # When Who Problem Lang Verdict Time Memory 55331572 Jun/09/2019 19:1 ...