\(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. SQLi_Labs通关文档

    SQLi_Labs通关文档 先列举一下sql基础语句 show databases; //查看数据库 use xxx; //使用某个数据库 show tables; //查看该数据库的数据表 desc ...

  2. Java 常用正则表达式搜集ing

    MAC地址: ^[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{2}+:[a-fA-F0-9]{ ...

  3. CSP-S 2019 题解

    D1T1-格雷码 题中给出了构造格雷码的方法. $solve(n,k)$表示求出$2^n$意义下排名为$k$的格雷码, 只要比较一下考虑最高位的0/1取值就好了. 部分分提示了要开$unsigned\ ...

  4. JavaScript DOM 常用操作

    1.理解DOM: DOM(Document Object Model ,文档对象模型)一种独立于语言,用于操作xml,html文档的应用编程接口. 怎么说,我从两个角度理解: 对于JavaScript ...

  5. CodeForce 222C Reducing Fractions

    To confuse the opponents, the Galactic Empire represents fractions in an unusual format. The fractio ...

  6. 休谟:《人性论》一书中提出的要重视"是"与"应该"的区别

    "价值"最初是经济学的范畴,指的是经济价值.商品价值.价值作的为一个哲学概念,首先大概是由18 世纪的英国哲学家休谟(David H ume,1711-1776)提出的.他于173 ...

  7. Wine添加路径PATH办法

    使用wine运行某些程序时,可能会提示某些DLL找不到,需要手动把这些DLL的路径添加进去.添加方法是:wine regedit打开注册表工具:添加一个键HKEY_CURRENT_USER/Envir ...

  8. phpmyadmin 在服务起上检测到错误,请查看窗口底部

    使用phpmyadmin一直提示这个警告,看着难受: 解决: 修改文件:/etc/phpmyadmin/config.inc.php 在最后添加这一句, $cfg['SendErrorReports' ...

  9. Yapi接口管理平台 本地部署 windows环境 -

    YApi 是高效.易用.功能强大的 api 管理平台,旨在为开发.产品.测试人员提供更优雅的接口管理服务.可以帮助开发者轻松创建.发布.维护 API,YApi 还为用户提供了优秀的交互体验,开发人员只 ...

  10. 【转】聊一聊-JAVA 泛型中的通配符 T,E,K,V,?

    原文:https://juejin.im/post/5d5789d26fb9a06ad0056bd9 前言 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型 ...