【Best Coder】#36 C tree

来源:互联网 发布:java防止xss攻击代码 编辑:程序博客网 时间:2024/05/17 01:42

好吧,我承认是抄别人的代码的。

但感觉这道题目确实得好好学习学习。

#define _CRT_SECURE_NO_WARNINGS#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define MAX 50005struct node{int v;int id;}h[MAX],q[MAX];int co[MAX];int res[MAX];bool cmp(node a, node b){return a.v > b.v;}int main(){int n, qq;while (~scanf("%d%d", &n, &qq)){memset(co, 0, sizeof(co));for (int i = 0; i < n; i++){scanf("%d", &h[i].v);h[i].id = i;}for (int i = 0; i < qq; i++){scanf("%d", &q[i].v);q[i].id = i;}sort(h, h + n, cmp);sort(q, q + qq, cmp);int pos = 0;int ans = 0;for (int i = 0; i < qq; i++){while (pos<n&&h[pos].v>q[i].v){if (co[h[pos].id - 1])ans--;if (co[h[pos].id + 1])ans--;ans++;co[h[pos].id] = 1;pos++;}res[q[i].id] = ans;}for (int i = 0; i < qq; i++)printf("%d\n", res[i]);}}

0 0