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. SQL Server中排名函数row_number,rank,dense_rank,ntile详解

    SQL Server中排名函数row_number,rank,dense_rank,ntile详解 从SQL SERVER2005开始,SQL SERVER新增了四个排名函数,分别如下:1.row_n ...

  2. java eclipse使用不同jdk版本

    因为开发需要,两个工程分别需要使用jdk1.6(elipse indigo)和jdk1.8(eclipse neon).因为两个eclipse对于jdk版本的要求不同,若只在环境变量中配置jdk版本, ...

  3. JAVA使用并行流(ParallelStream)时要注意的一些问题

    https://blog.csdn.net/xuxiaoyinliu/article/details/73040808

  4. FloatingActionButton 完全解析

    鸿洋大神说的很明白这里就直接引用一下: FloatingActionButton 完全解析[Design Support Library(2)]

  5. 再说WCF Data Contract KnownTypeAttribute

    WCF 中的序列化是用DataContractSerializer,所有被[DataContract]和[DataMemeber]标记的类和属性会被DataContractSerializer序列化. ...

  6. phpstudy nginx下curl请求本地其他项目

    curl 请求的时候 如果用post请求,传递参数为 数组的时候 header 头 会被设置为  multipart/form-data  如果是字符串 形式 header 头会被设置为applica ...

  7. EasyPlayerPro windows播放器本地配置文件配置方法介绍

    需求背景 应EasyPlayerPro某客户需求,在EasyPlayerPro启动时,自动播放指定的url源, 不需要每次都去手动填写, 且实现自动播放,不需要手动的单击播放按钮: 为响应该需求,特增 ...

  8. python列表(list)常用方法

    #!/usr/bin/env python # -*- coding:utf-8 -*- a = [1, 2, 3, 4, 5] # 索引 print(a[0], a[1], a[2], a[3], ...

  9. Java for LeetCode 133 Clone Graph

    Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJ's ...

  10. 有关svg的一些理解

    SVG 是使用XML来描述二维图形和绘图程序的语言. SVG指可伸缩的矢量图形(Scalable Vector Graphics) SVG使用XML格式定义图形 SVG图形在放大或改变尺寸的情况下,图 ...