\(Gym101002E:K-Inversions\)

题意描述:

  • 题目连接链接

  • 给定一个长度为\(N\)只包含\(AB\)的字符串,某个\(A\)的位置为\(j\),某个\(B\)的位置为\(i\),求\(j-k=k\)的数对有多少个,输出\(k=1,2,...,n-1\)的情况。

数据范围:

  • \(1\leq n\leq 10^6\)。

思路:

  • \(FFT\)。
  • 假设\(A\)的位置为\(x\),\(B\)的位置为\(y\),则题目需要\(x-y=k\)。
  • 如果说我们将\(A,B\)的位置看作是多项式的幂,就可以在\(nlogn\)的时间内求出所有幂为\(x+y\)的系数。
  • 所以这里需要转换一下,令\(y=(n-y-1)\),那么\(x-y=x-n+y+1=k\),即\(x+y=n+k-1\)。
  • 所以将\(B\)的位置反转,套用\(fft\)求解。
#include<bits/stdc++.h>
using namespace std;
const int maxn = 6e6 + 10;
const double PI = acos(-1.0);
char s[maxn];
int limit, l, r[maxn];
struct Complex
{
double x, y;
Complex(double xx = 0, double yy = 0){
x = xx, y = yy;
}
Complex operator + (const Complex b) const{
return Complex(x+b.x, y+b.y);
}
Complex operator - (const Complex b) const{
return Complex(x-b.x, y-b.y);
}
Complex operator * (const Complex b) const{
return Complex(x*b.x-y*b.y, x*b.y+y*b.x);
}
}a[maxn], b[maxn]; void fft(Complex c[], int type)
{
for(int i = 0; i < limit; i++)
if(i < r[i]) swap(c[i], c[r[i]]);
for(int mid = 1; mid < limit; mid <<= 1)
{
Complex wn(cos(PI/mid), type*sin(PI/mid));
for(int R = mid<<1, j = 0; j < limit; j+= R)
{
Complex w(1, 0);
for(int k = 0; k < mid; k++, w = w*wn)
{
Complex x = c[j+k], y = w*c[j+mid+k];
c[j+k] = x+y;
c[j+mid+k] = x - y;
}
}
}
} int main()
{
scanf("%s", s);
int n = strlen(s);
for(int i = 0; i < n; i++)
{
if(s[i] == 'A') a[i].x = 1;
else b[n-i-1].x = 1;
} limit = 1;
while(limit <= n+n) limit <<= 1, l++;
for(int i = 0; i < limit; i++)
r[i] = (r[i>>1]>>1)|((i&1)<<(l-1));
fft(a, 1), fft(b, 1);
for(int i = 0; i <= limit; i++)
a[i] = a[i]*b[i];
fft(a, -1);
for(int i = n; i < n+n-1; i++)
printf("%d\n", (int)(a[i].x/limit+0.5)); return 0;
}

Gym101002E:K-Inversions的更多相关文章

  1. django模型操作

    Django-Model操作数据库(增删改查.连表结构) 一.数据库操作 1.创建model表        

  2. Inversions After Shuffle

    Inversions After Shuffle time limit per test 1 second memory limit per test 256 megabytes input stan ...

  3. 《算法导论》Problem 2-4 Inversions

    在Merge Sort的基础上改改就好了. public class Inversions { public static int inversions(int [] A,int p, int r) ...

  4. [Swift]LeetCode775. 全局倒置与局部倒置 | Global and Local Inversions

    We have some permutation Aof [0, 1, ..., N - 1], where N is the length of A. The number of (global) ...

  5. HDU 6318 - Swaps and Inversions - [离散化+树状数组求逆序数][杭电2018多校赛2]

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=6318 Problem Description Long long ago, there was an ...

  6. HDU 6318 Swaps and Inversions 思路很巧妙!!!(转换为树状数组或者归并求解逆序数)

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  7. PAT 1009. Triple Inversions (35) 数状数组

    Given a list of N integers A1, A2, A3,...AN, there's a famous problem to count the number of inversi ...

  8. HDU 多校对抗赛第二场 1010 Swaps and Inversions

    Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. Codeforces 513G1 513G2 Inversions problem [概率dp]

    转自九野:http://blog.csdn.net/qq574857122/article/details/43643135 题目链接:点击打开链接 题意: 给定n ,k 下面n个数表示有一个n的排列 ...

  10. Coursera Algorithms week3 归并排序 练习测验: Counting inversions

    题目原文: An inversion in an array a[] is a pair of entries a[i] and a[j] such that i<j but a[i]>a ...

随机推荐

  1. multer 文件后缀名

    我的代码是这样写的. var storage = multer.diskStorage({ destination: function (req, file, cb) { cb(null, 'uplo ...

  2. idea 项目在一般模式下可以正常启动,在debug模式下无法启动,像是卡住了的感觉

    项目一般模式下可以启动,debug模式下就是启动不了,后经过排查发现打的有断点,断点取消在重启立马就可以啦. Method breakpoints may dramatically slow down ...

  3. K8S集群集成harbor(1.9.3)服务并配置HTTPS

    一.简介 简介请参考:https://www.cnblogs.com/panwenbin-logs/p/10218099.html 二.安装Harbor主机环境及安装要求 主机环境: OS: Cent ...

  4. 利用 Symbol Type Viewer 工具实现将 pdb 文件 转换为 c\c++ 头文件

    利用 Symbol Type Viewer 工具实现将 pdb 文件 转换为 c\c++ 头文件 一.得到符号 二.将符号转换为 .h 文件 三.得到 c\c++ 头文件,之后编程时直接导入这个文件即 ...

  5. Docker 镜像-管理-导入-导出

    目录 Docker 镜像基本概念 Docker 镜像加速 Docker 镜像 常用命令 Docker 镜像的创建和导出导入 Docker 镜像基本概念 我们使用的容器都是基于镜像的,镜像是由多层组成的 ...

  6. ASP.NET Core快速入门(第2章:配置管理)--学习笔记

    课程链接:http://video.jessetalk.cn/course/explore 良心课程,大家一起来学习哈! 任务9:配置介绍 命令行配置 Json文件配置 从配置文件文本到c#对象实例的 ...

  7. 用openresty(Lua)写一个获取YouTube直播状态的接口

    文章原发布于:https://www.chenxublog.com/2019/08/29/openresty-get-youtube-live-api.html 之前在QQ机器人上面加了个虚拟主播开播 ...

  8. pymysql 的简单使用

    一.环境 Windows  7 x64     python 3.7.1    pymysql 0.9.3  mysql5.6.43 二.pymysql的简单使用 1.准备数据库demo_temp c ...

  9. SSO单点登录和CAS

    一.单点登录流程 =====客户端====== 1.拦截客户端的请求判断是否有局部的session 2.1如果有局部的session,放行请求. 2.2如果没有局部session 2.2.1请求中有携 ...

  10. oracle 主键生成策略-sequence序列+trigger触发器

    oracle中设置表的主键字段为自增序列(实例)1.首先创建一个表(如日志表) //删除库表中存在的日志表drop table S_LOG_INFO cascade constraints;//新建日 ...