当前位置:网站首页>(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
(前缀和/思维)Codeforces Round #806 (Div. 4)F. Yet Another Problem About Pairs Satisfying an Inequality
2022-07-26 22:49:00 【ZaneBobo】

一.题意
给你一个数组,要求你找出数组中满足 ai<i<aj<j 的数对个数。
二.思路
首先限定 数对中的每一个数一定满足ai<i,那么只需要建立一个i<aj的关系即可。
我们利用前缀和的思想 sum[i]代表 i之前包括i 所有满足ai<i的数的个数,那么sum[a[j]-1]就是所有满足i<a[j]的且满足ai<i的个数。然后枚举的时候再加上一个限定条件 a[j]<j 就行啦。
三.代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 2e5+10;
int a[N];
map<int,int> sum;
typedef long long LL;
void solve()
{
int n;
cin>>n;
sum.clear();
for(int i=1;i<=n;i++)
{
cin>>a[i];
sum[i]+=sum[i-1]+(a[i]<i);//限定 a[i]<i
}
LL res=0;
for(int i=1;i<=n;i++)
{
if(i>a[i])res+=sum[a[i]-1];
//if条件限定a[j]<j sum[a[i]-1]则是限定了i<a[j]这样全部符合条件的对数
}
cout<<res<<endl;
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
}边栏推荐
- 6.30联发科笔试
- 7.8 锐捷网络笔试
- 2022zui new Tiktok 24-hour round robin live broadcast monitoring (I) live broadcast room start-up monitoring
- TCP's three handshakes and four waves (brief introduction)
- 2022zui新抖音24小时循环值守直播监控(一)直播间开播监控
- C语言——数组、字符串处理函数、strlen、strcpy和strncpy、strcat和strncat、strcmp和strncmp
- 7.8 Ruijie online written examination
- C语言——第一个程序、打印、变量和常量
- Solution: various error reporting and pit stepping and pit avoiding records encountered in the alchemist cultivation plan pytoch+deeplearning (III)
- Es specify user name and password when instantiating resthighlevelclient
猜你喜欢

Introduction to STM32 lesson 1

C语言——while语句、dowhile语句、for循环和循环结构、break语句和continue语句

Static comprehensive experiment (comprehensive exercise of static route, loopback interface, default route, empty interface, floating static)

The gradient descent method and Newton method are used to calculate the open radical

C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!

动态路由ofps协议配置

OSPF basic configuration application (comprehensive experiment: interference election default routing area summary authentication -- interface authentication)

Dynamic routing rip protocol experiment

2022最新抖音直播监控(二)直播间流媒体下载

2022zui新抖音24小时循环值守直播监控(一)直播间开播监控
随机推荐
OSPF static experiment
Text to image paper intensive reading rat-gan: recursive affine transformation for text to image synthesis
FID index reproduction step on the pit to avoid the pit text generation image FID quantitative experiment whole process reproduction (FR é Chet inception distance) quantitative evaluation experiment s
静态路由缺省路由vlan实验
最新C语言入门与进阶 -史上最全最详细的C语言教程!! 第一节-总览C语言概括
OSPF protocol overview and basic concepts
OSPF的重发布及路由策略
C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!
C语言——数据类型、基本数据类型的取值范围
高度塌陷解决方法
机械硬盘选购指南——从选购经历谈起
测开基础 日常刷题 (持续更新ing...)
2022最新抖音直播监控(二)直播间流媒体下载
ACM模式输入输出练习
Codeforces Round #810 (Div. 2), problem: (B) Party
7.13 蔚来提前批笔试
js求最大值?
[详解C语言]一文带你玩转选择(分支)结构
2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
Es specify user name and password when instantiating resthighlevelclient