标签:
http://acm.hdu.edu.cn/showproblem.php?pid=4908
题意:
求一个[1,N]的全排列中以M为中位数的子序列的个数
思路:
隔了好久填个坑...当初理解错题意了
一开始直接写了个二重循环来扫直接TLE了...
搜了下才想到该怎么用一重解决...
代码: 93MS 1524K
#include <cstdio> #include <cstring> using namespace std; #define N 40001 int a[N], cnt[N << 1]; int main() { int n, m, p; while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] == m) { p = i; } } memset(cnt, 0, sizeof(cnt)); int ans = 0; for (int i = p, t = 0; i >= 0; i--) { if (a[i] > m) { t++; } else if (a[i] < m) { t--; } cnt[N + t]++; } for (int i = p, t = 0; i < n; i++) { if (a[i] > m) { t--; } else if (a[i] < m) { t++; } ans += cnt[N + t]; } printf("%d\n", ans); } return 0; }
标签:
原文地址:http://www.cnblogs.com/rsola/p/4269070.html