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 ...
随机推荐
- Android Camera API2中采用CameraMetadata用于从APP到HAL的参数交互
前沿: 在全新的Camera API2架构下,常常会有人疑问再也看不到熟悉的SetParameter/Paramters等相关的身影,取而代之的是一种全新的CameraMetadata结构的出现,他不 ...
- python中strip()函数的理解
1.strip()函数 函数原型 声明:s为字符串.rm为要删除的字符序列 s.strip(rm) :删除s字符串中开头.结尾处.位于 rm删除序列的字符 s.lstrip(rm) :删除s字符串中开 ...
- 根据URL发起HTTP请求,我的HTTPHelper。
完整的demo using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...
- 重新编译Nginx指导手册【修复静态编译Openssl的Nginx漏洞 】(转)
1. 概述 当前爆出了Openssl漏洞,会泄露隐私信息,涉及的机器较多,环境迥异,导致修复方案都有所不同.不少服务器使用的Nginx,是静态编译opensssl,直接将openssl编译到ng ...
- [转]XMPP基本概念--节(stanza)
本文介绍在XMPP通信中最核心的三个XML节(stanza).这些节(stanza)有自己的作用和目标,通过组织不同的节(stanza),就能达到我们各种各样的通信目的. 首先我们来看一段XMPP流. ...
- linux 上操作常用的命苦与出错的地方
帮助信息 ./configure -help|grep mysql 出错提示安装libxml2 tar -zxvf libxml2xxxx.tar cd libxml2xxx ./configure ...
- asm 与 cglib(整理的)
参考博客地址 http://www.oseye.net/user/kevin/blog/304#top http://www.blogjava.net/vanadies10/archive/2011/ ...
- 漫反射和Lambert模型
粗糙的物体表面向各个方向等强度地反射光,这种等同地向各个方向散射的现象称为光的漫反射(diffuse reflection).产生光的漫反射现象的物体表面称为理想漫反射体,也称为朗伯(Lambert) ...
- 如何进行Web服务的性能测试?
随着浏览器功能的不断完善,用户量不断的攀升,涉及到web服务的功能在不断的增加,对于我们测试来说,我们不仅要保证服务端功能的正确性,也要验证服务端程序的性能是否符合要求.那么性能测试都要做些什么呢?我 ...
- 计算机网络--DNS
1.域名系统DNS(domain name system)是因特网使用的命名系统,用来把便于人们时用的机器名字转换为IP地址.因特网的域名系统DNS被设计成一个联机分布式数据库系统,并采用客户服务器方 ...