题意:

给你1-n的序列,然后有k次机会的操作,每一次你可以选择两个数交换。

求一个最大的逆序数。

思路:

感觉就是最后一个和第一个交换,然后往中间逼近,到最终的序列,用树状数组求一下逆序数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define MAX 100010
typedef __int64 LL;
#define mod 10000007 const int N=1e5+10; struct asd{
int v,pos;
};
asd q[N*4];
int c[N*4];
int n; int Sum(int x)
{
LL ans=0;
while(x>0)
{
ans+=c[x];
x-=x&(-x);
}
return ans;
} void update(int x,int t)
{
while(x<=n+5)
{
c[x]+=t;
x+=x&(-x);
}
}
int a[N+10]; int main()
{
int k;
scanf("%d%d",&n,&k);
for(int i=1;i<=n;i++)
a[i]=i;
for(int i=1;i+i<=n&&i<=k;i++)
swap(a[i],a[n-i+1]); LL ans=0;
for(int i=1;i<=n;i++)
{
update(a[i],1);
ans+=i-Sum(a[i]);
}
printf("%I64d\n",ans);
return 0;
}

还有很多简单的操作…对不会总结的人来说,看的智商有点做急。。。。

情人节快乐…汪!

Codeforces645B【树状数组求逆序数】的更多相关文章

  1. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  2. hdu 5147 Sequence II (树状数组 求逆序数)

    题目链接 Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. SGU180 Inversions(树状数组求逆序数)

    题目: 思路:先离散化数据然后树状数组搞一下求逆序数. 离散化的方法:https://blog.csdn.net/gokou_ruri/article/details/7723378 自己对用树状数组 ...

  4. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  5. HDU 1394 Minimum Inversion Number ( 树状数组求逆序数 )

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number                         ...

  6. HDU 1394 Minimum Inversion Number(线段树/树状数组求逆序数)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  7. hdu 1394 Minimum Inversion Number (裸树状数组 求逆序数 && 归并排序求逆序数)

    题目链接 题意: 给一个n个数的序列a1, a2, ..., an ,这些数的范围是0-n-1, 可以把前面m个数移动到后面去,形成新序列:a1, a2, ..., an-1, an (where m ...

  8. Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem (树状数组求逆序数 变形)

    题目链接 题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数.i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数. 我们可以用map预处理出  ...

  9. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

随机推荐

  1. 谜题 之 C语言

    本篇文章展示了14个C语言的迷题以及答案.代码应该是足够清楚的,并且我也相信有相当的一些样例可能是我们日常工作可能会见得到的.通过这些迷题,希望你能更了解C语言.假设你不看答案.不知道是否有把握回答各 ...

  2. hihoCoder 1234 fractal

    #1234 : Fractal 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 This is the logo of PKUACM 2016. More specific ...

  3. chrome.declarativeWebRequest

    chrome.declarativeWebRequest 清单文件 规则 条件与操作的求值 使用优先级覆盖规则 类型 HeaderFilter RequestMatcher CancelRequest ...

  4. System V IPC相关函数

    System V IPC 将一个已保存的路径名和一个整数标识符转换成一个key_t值,称为IPC键key_t:System V IPC(System V消息队列.System V信号量.System ...

  5. snip_进制转换代码段

    10进制转成16进制. /* 程序:10进制转16进制的C语言实现 描述: 关键: 获得余 获得整 整为零 则退出 */ #include<stdio.h> #define N 10 #d ...

  6. appium安装报错但运行成功

    npm install -g  appium ERR! fetch failed https://registry.npmjs.org/appium-uiauto/-/appium-uiauto-1. ...

  7. Android GUI系统学习1:Gralloc

    Gralloc模块是从Android Eclair(android 2.1)開始增加的一个HAL模块,Gralloc的含义为是Graphics Alloc(图形分配).他对上为libui提供服务,为其 ...

  8. log Configuration

    Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...

  9. CLI和CGI的区别

    CGI :“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上.以CGI方式运行时,web s ...

  10. mini_magick

    https://github.com/minimagick/minimagick class  https://www.rubydoc.info/github/minimagick/minimagic ...