前缀和 Codeforces509E Pretty Song
来源:互联网 发布:飞飞cms安装ck播放器 编辑:程序博客网 时间:2024/05/16 07:33
传送门:点击打开链接
题意:定义一个函数f(l,r),结果是区间[l,r]中IEAOUY的个数除以(r-l+1),求所有区间的函数f之和
思路:求所有的这种字符对答案的贡献。我们可以发现,如果上述的这种字符,所在的位置,从左边数是3,从右边数是4
那么答案就等于
1+1/2+1/3+1/4
1/2+1/3+1/4+1/5
1/3+1/4+1/5+1/6
那么问题变成了,如何快速求出这个平行四边形里面的数字之和。
我们可以把这个平行四边形的上面和左下角补充完整,变成了一个左下角是直角的三角形。
我们很容易用前缀和记录这个三角形里的数字之和,那么我们可以利用这个来得到这个平行四边形里的数字之和。
直接拿大的三角形减去最上面的那个,就能得到下面这个梯形的数字之和了,然后再减去左下角那个空白三角形就可以了。
左下角空白三角形也是从1开始的,我们也能利用前缀和直接减去,然后就做完了。
#include<map>#include<set>#include<cmath>#include<ctime>#include<stack>#include<queue>#include<cstdio>#include<cctype>#include<string>#include<vector>#include<cstring>#include<iomanip>#include<iostream>#include<algorithm>#include<functional>#define fuck(x) cout<<"["<<x<<"]"#define FIN freopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w+",stdout)using namespace std;typedef long long LL;typedef pair<int, int>PII;const int MX = 5e5 + 5;const int mod = 1e9 + 7;const int INF = 0x3f3f3f3f;char s[MX];double L[MX];void presolve() { double last = 0; L[0] = 0; for(int i = 1; i < MX; i++) { last += 1.0 / i; L[i] = L[i - 1] + last; }}double solve(int h, int w) { return L[h + w - 1] - L[w - 1] - L[h - 1];}bool yes(char x) { if(x == 'I' || x == 'E' || x == 'A' || x == 'O' || x == 'U' || x == 'Y') return true; return false;}int main() { presolve(); //FIN; while(~scanf("%s", s + 1)) { int n = strlen(s + 1); double ans = 0; for(int i = 1; i <= n; i++) { if(yes(s[i])) ans += solve(i, n - i + 1); } printf("%.8f\n", ans); } return 0;}
0 0
- 前缀和 Codeforces509E Pretty Song
- CF509E:Pretty Song(规律,前缀和)
- CF #289 E. Pretty Song
- Codeforces 509E. Pretty Song
- Pretty Song - CodeForces 509 E 想法题
- 【水题】Codeforces Round #289 509E Pretty Song
- 数学题 暴力字符串 Codeforces div2 E. Pretty Song
- 12-30 Wannafly每日一题 Pretty Song
- codeforces 509E Pretty Song [递推]【杂类】
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E.Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules) E. Pretty Song
- Codeforces Round #289 (Div. 2, ACM ICPC Rules)E. Pretty Song(数学,想法题,好题)
- 前缀和
- 前缀和
- 自定义多状态View
- eclipse 自动生成get/set方法
- oracle添加拼接函数
- JDK1.8(8.0)环境变量配置(Android Studio)
- 类似淘宝头条,走马灯上下滚动
- 前缀和 Codeforces509E Pretty Song
- 快速掌握Lua 5.3 —— 编译,运行以及错误
- beaglebone black hardware version
- android 电话状态的监听(来电和去电)
- 分享使用MFCC和LPC进行语音识别
- UICollectionView瀑布流的实现,带例子
- 2865: 结构体--日期计算
- Eclipse 连接MySql数据库总结
- 【寒假记录】素材收集整理