当前位置:网站首页>(前缀和/思维)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();
}
}边栏推荐
- OSPF静态大实验
- Dynamic routing ofps protocol configuration
- [MySQL] MySQL startup and shutdown commands and some error reports to solve problems
- Text to image paper intensive reading ssa-gan: text to image generation with semantic spatial aware Gan
- Es specify user name and password when instantiating resthighlevelclient
- JS 99 multiplication table
- Simple application of rip V2 (V2 configuration, announcement, manual summary, ripv2 authentication, silent interface, accelerating convergence)
- [FPGA tutorial case 29] the second DDS direct digital frequency synthesizer based on FPGA - Verilog development
- Autojs learning - realize the display of date and lunar time
- [FPGA tutorial case 30] DDS direct digital frequency synthesizer based on FPGA -- frequency accuracy analysis with MATLAB
猜你喜欢

TIM输出比较——PWM

2022 latest live broadcast monitoring 24-hour monitoring (III) analysis of barrage in live broadcast room
npm报错, Error: EPERM: operation not permitted, mkdir

7.16 written examination of Duoyi network
![[详解C语言]一文带你玩转选择(分支)结构](/img/ca/7ee9f62a2478785c97684c7a0cc749.png)
[详解C语言]一文带你玩转选择(分支)结构

微信小程序:用户微信登录流程(附:流程图+源码)

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

Tcp的三次握手与四次挥手

C语言实现小游戏【三子棋】注释详细 逻辑清晰 快来看看吧!!
![[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction](/img/bf/ce4bc33d2a0fc7fe57105e20fbafcf.png)
[FPGA tutorial case 28] one of DDS direct digital frequency synthesizers based on FPGA -- principle introduction
随机推荐
第四讲—讲解GPIO_Write函数以及相关例程
ospf协议概述以及基础概念
[explain C language in detail] takes you to play with loop structure (for_while_do while)
JS 99 multiplication table
Realize data interaction between two apps through fileprovider
[MySQL] MySQL startup and shutdown commands and some error reports to solve problems
The gradient descent method and Newton method are used to calculate the open radical
Three methods that can effectively fuse text and image information -- feature stitching, cross modal attention, conditional batch normalization
ACM mode input and output exercise
2022最新抖音直播监控(二)直播间流媒体下载
7.16 多益网络笔试
Experiment of total connection and star topology of mGRE
npm报错, Error: EPERM: operation not permitted, mkdir
静态路由缺省路由vlan实验
微信小程序:用户微信登录流程(附:流程图+源码)
js求最大值?
OSPF的重发布及路由策略
Solution to high collapse
6.28 Dahua written examination
7.8 Ruijie online written examination