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)计算即可

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long ll;
 6 inline void read(int &x){
 7     x=0;char ch;bool flag = false;
 8     while(ch=getchar(),ch<‘!‘);if(ch == ‘-‘) ch=getchar(),flag = true;
 9     while(x=10*x+ch-‘0‘,ch=getchar(),ch>‘!‘);if(flag) x=-x;
10 }
11 inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
12 inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
13 const int maxn = 200010;
14 char s1[maxn],s2[maxn];
15 double a[256],b[256];
16 int main(){
17     int n;read(n);
18     scanf("%s%s",s1+1,s2+1);
19     double ans = 0;
20     for(int i=1;i<=n;++i){
21         b[s2[i]] += i;
22         ans += 1LL*(n-i+1)*(a[s2[i]] + b[s1[i]]);
23         a[s1[i]] += i;
24     }
25     printf("%.20lf\n",6.0*ans/n/(n+1)/(n<<1|1));
26     getchar();getchar();
27     return 0;
28 }

时间: 2024-08-08 19:57:07

CodeForces - 204C Little Elephant and Furik and Rubik的相关文章

Codeforces 220B - Little Elephant and Array 离线树状数组

This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will solve this problem in offline. For each x (0?≤?x?<?n) we should keep all the queries that end in x. Iterate that x from 0 to n?-?1. Also we need to kee

CodeForces 258B Little Elephant and Elections 数位DP

前面先用数位DP预处理,然后暴力计算组合方式即可. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include

CodeForces 221D Little Elephant and Array

Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 221D64-bit integer IO format: %I64d      Java class name: (Any) The Little Elephant loves playing with arrays. He has array a,

CodeForces 259A Little Elephant and Chess

Little Elephant and Chess Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 259A Description The Little Elephant loves chess very much. One day the Little Elephant and his friend decided t

codeforces 351B B. Jeff and Furik(概率)

题目链接: codeforces 351B 题目大意: 给出一个游戏,先手选择一对相邻的数交换位置,后手随机找一对数,然后掷硬币决定是否交换,如果不能交换,那么就重新找.问大致序列有序的采取最优策略的布数的期望. 题目分析: 定义dp[i]表示减少i个逆序对的步数的期望. dp[i]=1+1+dp[i?2]?0.5+dp[i?1+1]]?0.5 因为先手可以选择,所以它每次一定会采取操作减少一个逆序对. 我们得到dp[i] = 4 + dp[i-2],dp[0] = 0 , dp[1] = 1;

CodeForces - 258D Little Elephant and Broken Sorting

Discription The Little Elephant loves permutations of integers from 1 to n very much. But most of all he loves sorting them. To sort a permutation, the Little Elephant repeatedly swaps some elements. As a result, he must receive a permutation 1,?2,?3

CodeForces 258D Little Elephant and Broken Sorting(期望)

CF258D Little Elephant and Broken Sorting 题意 题意翻译 有一个\(1\sim n\)的排列,会进行\(m\)次操作,操作为交换\(a,b\).每次操作都有\(50\%\)的概率进行. 求进行\(m\)次操作以后的期望逆序对个数. \(n,m\le 1000\) 输入输出格式 输入格式: The first line contains two integers \(n\) and \(m\) \((1\leq n,m\leq 1000,n>1)\) -

CodeForces 204A Little Elephant and Interval 数位DP

#include <cstdio> #include <cstring> using namespace std; typedef __int64 LL; int a[33]; LL dp[33][10]; LL dfs(int x, int s, int e, int flag, int first) { if(x == -1) return s == e; if(!flag && dp[x][s]!= -1) return dp[x][s]; int end =

codeforces E. Little Elephant and Strings(广义后缀自动机,Parent树)

传送门在这里. 大意: 给一堆字符串,询问每个字符串有多少子串在所有字符串中出现K次以上. 解题思路: 这种子串问题一定要见后缀自动机Parent树Dfs序统计出现次数都是套路了吧. 这道题统计子串个数,那么可以发现,若一个节点所对应的子串出现了K次,那么其贡献就是len,不需要考虑重复. 因为即使出现重复也是在两个位置. 那么只需统计以每个点结束的子串就好了. 之前的Dfs序就很套路了. 只需再跑一遍字符串,更新答案就好了. 代码: 1 #include<cstdio> 2 #include