\(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. ksync

    #include <linux/init.h> #include <linux/module.h> #include <linux/types.h> #includ ...

  2. STM32移植ROS--发布超声波信息

    前言:之前ROS跟单片机的底层通讯主要是通过串口自定的协议来做,比如官网提供的arduino串口驱动一样,需要ROS往下发一个指令,单片机再回传一个指令,要写一大堆的协议,这样很麻烦,效率也比较低, ...

  3. Python保存json文件并格式化

    使用json.dump()的时候设置一下indent参数的值就好了.比如json.dump(json_dict, f, indent=4), ensure_ascii=False,写入中文

  4. idea中的后缀补全

    IDEA有个很牛逼的功能,那就是后缀补全(Postfix Completion),这个功能可以通过后缀来使用代码补全进行模板式地补全语句,如遍历循环语句(for.foreach).使用 String. ...

  5. 2019.10 搜索引擎最新排名,Elasticsearch遥遥领先

    大数据的搜索平台已经成为了众多企业的标配,Elasticsearch.Splunk(商业上市公司).Solr(Apache开源项目)是其中最为优秀和流行的选择.在2019.10 最新搜索引擎排名中,E ...

  6. conda opencv cv2.imshow无法使用

    error: -------src-dir-------/opencv-2.4.10/modules/highgui/src/window.cpp:501: error: (-2) The funct ...

  7. Vue基础框架

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <!-- 设置语言为 ...

  8. Kafka学习笔记之Kafka High Availability(上)

    0x00 摘要 Kafka在0.8以前的版本中,并不提供High Availablity机制,一旦一个或多个Broker宕机,则宕机期间其上所有Partition都无法继续提供服务.若该Broker永 ...

  9. SQL Server中,如何查看每个数据库的Owner是哪个SQL Server账户,也就是谁创建的

    有时候我们作为SQL Server的DBA,会需要查找每个数据库的Owner是哪个SQL Server账户,也就是谁创建的. 我们可以使用系统存储过程"sys.sp_helpdb" ...

  10. STP生成树理解

    1.STP的功能 a. 防止二层环路    b .实现网络冗余备份 2.STP的选择机制 目的:  确定阻塞的端口 STP 交换机的角色: 根交换机,非根交换机 STP的选票:     BPDU Ro ...