zoukankan      html  css  js  c++  java
  • [luoguP1627] 中位数(模拟?)

    传送门

    水题,怎么评到这个难度的?

    #include <cstdio>
    #include <iostream>
    #define N 200001
    
    int n, b, p, ans = 1;
    int c[N], d[N], a[N];
    int *num1 = c + 100000, *num2 = d + 100000;
    
    inline int read()
    {
    	int x = 0, f = 1;
    	char ch = getchar();
    	for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
    	for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
    	return x * f;
    }
    
    int main()
    {
    	int i, x;
    	n = read();
    	b = read();
    	for(i = 1; i <= n; i++)
    	{
    		a[i] = read();
    		if(a[i] == b) p = i;
    	}
    	x = 0;
    	//x是比b大的数比比b小的数多多少个 
    	for(i = p - 1; i >= 1; i--)
    	{
    		if(a[i] > b) x++;
    		if(a[i] < b) x--;
    		if(!((p - i) & 1) && !x) ans++;
    		num1[x]++;
    	}
    	x = 0;
    	//x是比b大的数比比b小的数少多少个 
    	for(i = p + 1; i <= n; i++)
    	{
    		if(a[i] < b) x++;
    		if(a[i] > b) x--;
    		if(!((i - p) & 1) && !x) ans++;
    		num2[x]++;
    	}
    	for(i = -n; i <= n; i++) ans += num1[i] * num2[i];
    	printf("%d
    ", ans);
    	return 0;
    }
    

      

  • 相关阅读:
    Python 写文件
    Python 读文件
    Python 打开文件(File Open)
    Python 异常处理(Try...Except)
    Python PIP包管理器
    Python 正则表达式(RegEx)
    Python JSON
    Python 模块
    Python 迭代器(Iterator)
    Python 继承
  • 原文地址:https://www.cnblogs.com/zhenghaotian/p/7360088.html
Copyright © 2011-2022 走看看