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的更多相关文章

  1. Codeforces D. Little Elephant and Interval(思维找规律数位dp)

    题目描述: Little Elephant and Interval time limit per test 2 seconds memory limit per test 256 megabytes ...

  2. CodeForces 259A Little Elephant and Chess

     Little Elephant and Chess Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d &am ...

  3. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  4. Codeforces 204A Little Elephant and Interval

    http://codeforces.com/problemset/problem/204/A 题意:给定一个[L,R]区间,求这个区间里面首位和末尾相同的数字有多少个 思路:考虑这个问题满足区间加减, ...

  5. Codeforces 258D Little Elephant and Broken Sorting (看题解) 概率dp

    Little Elephant and Broken Sorting 怎么感觉这个状态好难想到啊.. dp[ i ][ j ]表示第 i 个数字比第 j 个数字大的概率.转移好像比较显然. #incl ...

  6. Codeforces 258C Little Elephant and LCM

    Little Elephant and LCM #include<bits/stdc++.h> #define LL long long #define fi first #define ...

  7. [Codeforces 204E] Little Elephant and Strings

    [题目链接] https://codeforces.com/contest/204/problem/E [算法] 首先构建广义后缀自动机 对于自动机上的每个节点 , 维护一棵平衡树存储所有它所匹配的字 ...

  8. 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 ...

  9. CodeForces - 205B - Little Elephant and Sorting

    先上题目: Little Elephant and Sorting time limit per test 1 second memory limit per test 256 megabytes i ...

随机推荐

  1. java sqlite配置和自定义函数

    资源 jetty Jetty Downloads地址 sqlite sqlite JDBC Driver 地址:bitbucket代码托管 和 Github代码托管 jetty配置sqlite 在je ...

  2. ie6中 object doesn’t support this property or method

    可能是由于方法或json中有注释,/**/或//删掉注释就可以了

  3. Android 属性动画框架 ObjectAnimator、ValueAnimator ,这一篇就够了

    前言 我们都知道 Android 自带了 Roate Scale Translate Alpha 多种框架动画,我们可以通过她们实现丰富的动画效果,但是这些宽家动画却有一个致命的弱点,它们只是改变了 ...

  4. 【Java】事件驱动模型和观察者模式

    你有一件事情,做这件事情的过程包含了许多职责单一的子过程.这样的情况及其常见.当这些子过程有如下特点时,我们应该考虑设计一种合适的框架,让框架来完成一些业务无关的事情,从而使得各个子过程的开发可以专注 ...

  5. (个人开源)ffpanel --ffmpeg的GUI,让ffmpeg离开黑黑的命令行

    程序及源代码下载地址 :https://github.com/langsim/ffpanel

  6. python opener代理

    链接:http://www.jb51.net/article/46495.htm https://www.cnblogs.com/cunyusup/p/7341829.html

  7. GOLANG 1.9 语言规范

    GOLANG 1.9 语言规范 - CSDN博客 https://blog.csdn.net/libing_thinking/article/details/77671607

  8. Javascript模块化编程-require.js[3]

    很多情况下,JS都是放到一个或者多个文件里,只要加载这些文件就可以了. 但是对于一些小型项目而言,这种写法是没有任何问题的. 但是对于某些大型网站,JS的量是很大的,如果还采用这种方式,网站时常在加载 ...

  9. vue+vuex构建单页应用

    基本 构建工具: webpack 语言: ES6 分号:行首分号规则(行尾不加分好, [ , ( , / , + , - 开头时在行首加分号) 配套设施: webpack 全家桶, vue 全家桶 项 ...

  10. 流畅python学习笔记第十八章:使用asyncio包处理并发(二)

    前面介绍了asyncio的用法.下面我们来看下如何用协程的方式来实现之前的旋转指针的方法 @asyncio.coroutine def spin(msg): write,flush=sys.stdou ...