Median on Segments (Permutations Edition)

E1. Median on Segments (Permutations Edition)

参考:CF1005E1 Median on Segments (Permutations Edition) 思维

中位数为m的条件为,在那一段中,小于 m 的数的个数为 x 个,大于 m 的数有 y 个,要满足条件x==y||x==y-1

因为不可能每一次都去统计有多少个大于的多少个小于的,所以我们要预处理一下,借此来降低复杂度

map<int,int> ma;
int cnt=0;
for(int i=pos;i>=1;--i)
{
    if(p[i]<m) cnt--;
    else if(p[i]>m) cnt++;
    ma[cnt]++;
}

代码:

// Created by CAD on 2020/1/11.
#include <bits/stdc++.h>
using namespace std;

const int maxn=2e5+5;
int p[maxn];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,m;cin>>n>>m;
    int pos;
    for(int i=1;i<=n;++i)
    {
        cin>>p[i];
        if(p[i]==m) pos=i;
    }
    map<int,int> ma;
    int cnt=0;
    for(int i=pos;i>=1;--i)
    {
        if(p[i]<m) cnt--;
        else if(p[i]>m) cnt++;
        ma[cnt]++;
    }
    int ans=0;  cnt=0;
    for(int i=pos;i<=n;++i)
    {
        if(p[i]<m) cnt++;
        else if(p[i]>m) cnt--;
        ans+=ma[cnt]+ma[1+cnt];
    }
    cout<<ans<<endl;
    return 0;
}

原文地址:https://www.cnblogs.com/CADCADCAD/p/12180394.html

时间: 2024-10-31 20:21:47

Median on Segments (Permutations Edition)的相关文章

E1. Median on Segments (Permutations Edition)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 16.0px Menlo; color: #008400; background-color: #ffffff } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; background-color: #ffffff; min-height: 14.0px } n个数字 不重复 给你一个m 然后问你有多少个区间的中位数是m 奇数

Codeforces #496 E1. Median on Segments (Permutations Edition)

http://codeforces.com/contest/1005/problem/E1 题目 https://blog.csdn.net/haipai1998/article/details/80985281  原博客 对样例1: 5 42 4 5 3 1 m=4,所以下标pos=2: 从pos往右遇到比m大的就cnt++,遇到小的就cnt--: 刚开始cnt=0;  mp[cnt]++ 于是从pos开始到 n:   mp[0]=1,   mp[1]=1,   mp[0]=2 ,   mp[

Codeforces 1005E1&amp;2 Median on Segments (General Case &amp; Permutations Edition)

E1 想到的O(n)做法,因为m只会出现一次,所以subarray里必须包括m.可以想像合法的subarray是m左边一个连续区间+m+m右边一个连续区间组成.然后把左区间预处理,枚举右区间就行了.(根据性质:一个subarray的median是m,那说明有0个数净比m大,或有1个数净比m大)[净大指的是2个比m小,1个比m大,算-1个比m净大] 1 #include<iostream> 2 #include<map> 3 using namespace std; 4 5 long

Codeforces Round #496 (Div. 3) ABCDE1

1 //B. Delete from the Left 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <set> 7 #include <map> 8 #include <vector> 9 using namespace std; 10 const int inf=0x3f3

Temporary Segments: What Happens When a Sort Occurs (文档 ID 102339.1)

APPLIES TO: Oracle Database - Enterprise Edition - Version 8.1.7.4 to 11.2.0.1 [Release 8.1.7 to 11.2]Information in this document applies to any platform.***Checked for relevance on 03-Nov-2014*** PURPOSE This article describes what happens when a s

[树状数组][权值线段树] Codeforces 1093E Intersection of Permutations

题目描述 You are given two permutations aa and bb , both consisting of nn elements. Permutation of nn elements is such a integer sequence that each value from 11 to nn appears exactly once in it. You are asked to perform two types of queries with them: 1

Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations: [ [1,1,2], [1,2,1], [2,1,1] ] 分析: 全组合的思想,保证start和end之间交换的时候中间没有与end相同的数字 class Solution

LeetCode OJ 4. Median of Two Sorted Arrays

There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). Example 1: nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2: nums1 = [1,

leetcode笔记:Find Median from Data Stream

一. 题目描述 Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value. Examples: [2,3,4] , the median is 3 [2,3], the median is (2 + 3) / 2 = 2.5 De