Codeforces645B【树状数组求逆序数】
2024-08-24 22:46:23
题意:
给你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;
}
还有很多简单的操作…对不会总结的人来说,看的智商有点做急。。。。
情人节快乐…汪!
最新文章
- ASP.NET MVC——Razor视图引擎
- ROS语音交互(三)科大讯飞语音在ROS平台下使用
- MATLAB画图
- spring mvc如何获取问号后的url参数
- 欢迎参加MVP主讲的Windows 10开发线上课程
- selenium简介
- Stimulsoft Reports报表工具
- 对Cost (%CPU) 粗略的理解
- 将数字n转换为字符串并保存到s中
- 》》css3--动画
- 【一天一道LeetCode】#371. Sum of Two Integers
- 嵌入页面的几种方法(转载自萤火虫小Q)
- UITableView的分割线长短的控制
- 【转载】springboot四 全局异常处理
- webstorm安装 利用host破解
- 理解TCP之Keepalive
- mysql添加事件
- ZOJ3202-Second-price Auction(根据输入输出判断是队列还是栈)
- Angular动态表单生成(五)
- hadoop日常维护之问题解决01
热门文章
- Java之基于Eclipse搭建SSH框架(下)
- Ubuntu下编译Android JNI实例全过程
- POJ - 1062 昂贵的聘礼(最短路Dijkstra)
- SpringBoot学习之快速入门创建
- ditaa - 把ascii图形转成图片
- android-測试so动态库(九)
- PCH in Xcode 6
- gRPC错误码 http状态码 provide your APIs in both gRPC and RESTful style at the same time
- this that 时间戳转日期 小程序 列表 与 加载
- Ruby map、each、select、inject、collect 、detect reference