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 ...
随机推荐
- win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址
win10 1709正式版iso镜像下载|windows10 1709秋季创意者更新官方下载地址 发布时间:2017-10-18 14:27发布者:系统城-xtcjh浏览数:74458 win10 1 ...
- Django之信息聚合
feeds.py #coding:utf-8 __author__ = 'similarface' from django.contrib.syndication.views import Feed ...
- hashCode与equals的作用与区别及应当注意的细节
最近去面试了几家公司,被问到hashCode的作用,虽然回答出来了,但是自己还是对hashCode和equals的作用一知半解的,所以决定把它们研究一下. 以前写程序一直没有注意hashCode的作用 ...
- PHP进阶知识
关于PHP程序员技术职业生涯规划:http://rango.swoole.com/ Micro Service Framework For PHP:https://github.com/pinguo/ ...
- Android-解决Fail to post notification on channel "null"的方法
原文:https://blog.csdn.net/weixin_40604111/article/details/78674563 在sdk版本为25或25之前想在notification中添加一个点 ...
- python 基础 2.6 for 循环 和if循环 中break
python中最基本的语法格式大概就是缩进了.python中常用的循环:for循环,if循环.一个小游戏说明for,if ,break的用法. 猜数字游戏: 1.系统生成一个20以内的随机数 2.玩家 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 斜率优化
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MB Description 农夫John准备扩大他的农场,他正在考虑N ...
- vue 向后台提交数据
新建template 表单 <template> <div class="add-group content"> <table class=" ...
- 各种jar包下方法的使用
commons-codec-1.6.jar: DigestUtils.md5Hex(String str); httpclient-4.2.2.jar: HttpClient client=new D ...
- 【题解】P2602[JZOI2010]数字计数
[题解][P2602ZJOI2010]数字计数 乍看此题,感觉直接从数字的位上面动手,感觉应该很容易. 但是仔细看数据范围,发现如果不利用计数原理,肯定会超时,考虑数码出现的特征: \(A000\)到 ...