Gym 100187M - Heaviside Function
来源:互联网 发布:上传视频的软件 编辑:程序博客网 时间:2024/05/28 03:03
Heaviside function is defined as the piecewiseconstant function whose value is zero for negative argument and one fornon-negative argument:
You are given the function f(x) = θ(s1x - a1) + θ(s2x - a2) + ... + θ(snx - an), where si = ± 1. Calculate its values for argument values x1, x2, ..., xm.
Input
The first line contains a single integer n (1 ≤ n ≤ 200000) —the number of the summands in the function.
Each of the next n lines contains two integers separated byspace — si and ai (si = ± 1, - 109 ≤ ai ≤ 109) — parameters of thei-th summand.
The next line contains a single integer m (1 ≤ m ≤ 200000) —the number of the argument values you should calculate the value of thefunction for.
The last line contains m integers x1, ..., xm ( - 109 ≤ xi ≤ 109) separated by spaces — the argument valuesthemselves.
Output
Output m lines. i-th line should contain the value of f(xi).
Sample test(s)
input
6
1 3
-1 2
1 9
-1 2
1 7
-1 2
8
0 12 2 8 4 -3 7 9
output
0
3
0
2
1
3
2
3
思路:
将s=1与s=-1的分开分别用a1,a2两个数组存储,分别从小到大排序;
x用结构存储,记录值和输入顺序,并按数值从小到大排序;
对于s=1,即对于a1数组,则统计x-a1[i]>=0的个数,我们可以枚举x,a1[i]从最大的开始,当a[i]足够小时,统计a1[i]的剩余个数。因为我们事先已经排序,所以直接加就可以;
对于s=-1的情况,同理;
代码:#define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int N = 200005;int n, s;int a1[N], a2[N];int l1, l2;int m;struct node{int value, id;}x[N];__int64 ans[N];bool cmp(node a, node b){return a.value<b.value;}bool cmp2(node a, node b){return a.id<b.id;}int main(){memset(ans, 0, sizeof(ans));scanf("%d", &n);int i, temp;l1 = l2 = 0;for (i = 0; i<n; i++){scanf("%d%d", &s, &temp);if (s == 1)a1[l1++] = temp;elsea2[l2++] = temp;}scanf("%d", &m);for (i = 0; i<m; i++){scanf("%d", &x[i].value);x[i].id = i;}sort(a1, a1 + l1);sort(a2, a2 + l2);sort(x, x + m, cmp);for (i = m - 1; i >= 0; i--){if (l1 == 0)break;while (x[i].value<a1[l1 - 1] && l1>0)l1--;if (l1 == 0)break;ans[x[i].id] += l1;}for (i = 0; i<m; i++){if (l2 == 0)break;while (x[i].value>-a2[l2 - 1] && l2>0)l2--;if (l2 == 0)break;ans[x[i].id] += l2;}sort(x, x + m, cmp2);for (i = 0; i<m; i++)printf("%I64d\n", ans[i]);return 0;}
- Gym 100187M - Heaviside Function
- Problem M. Heaviside Functionz
- Heaviside step function 阶跃函数
- 单位阶跃函数(Heaviside/unit step function)—— 化简分段函数
- 阶跃函数的导数为什么是冲击函数 The derivative of heaviside step function is delta function
- Gym 100513M - Variable Shadowing
- 396.Rotate Function(M)
- Gym 100703M It's complicated 水题
- Gym 101149M Ex Machina[线段树]
- GYM 101149 M.Ex Machina(构造)
- GYM 100827 D.Function(高斯消元)
- Codeforces Gym 100971M Decomposition into Good Strings DP+数据结构
- GYM 100090 M.Jumping along the Hummocks(SPFA)
- GYM 100488 M.Construct a Permutation(构造)
- GYM 100694 M.The Fifth Season(博弈论)
- Gym 101341M Last Man Standing 模拟|贪心
- Gym 101086 M ACPC Headquarters : AASTMT (Stairway to Heaven)
- [gym 101149 M. Ex Machina]构造+线段树
- C/C++ linux下光标定位和清屏函数(printf 实现)
- 黑马程序员——Java基础——Java语言组成
- 动态域名解析http版怎么用?
- [bfs]HDU2612 Find a way
- 读Spring的源代码二:DispatcherServlet处理web请求的过程
- Gym 100187M - Heaviside Function
- Thinkphp M 中的 where 失效问题
- HDU 5239 DOOM 线段树
- 线程
- 04-树8. Complete Binary Search Tree (30)
- HDU 2159 FATE(二维背包)
- Hadoop集群(第7期)_Eclipse开发环境设置
- EALayout 第三节 依赖多个控件布局
- RubyGem默认源安装太慢,修改国内淘宝源