选择客栈
作者:互联网
P1311 [NOIP2011 提高组] 选择客栈 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
- 核心:遍历每个客栈,认为这个客栈是客栈对中后一个,现在需要找到它前面有多少个满足条件的,不断更新当前最新的小于等于预算的客栈对应的位置,同时更新那个位置之前所有颜色对应的一个计数器nums,累加时对应颜色计数器的数量就是当前遍历到的客栈对答案的贡献(只考虑与之前的组合)
- pre时最新的预算小于等于p的客栈的位置,color是各点对应的颜色,nums是pre前的各颜色的一个统计
#include <bits/stdc++.h>
using namespace std;
#define N 1e5
#define INF 2e9
#define MAX 200005
// https://www.luogu.com.cn/problem/P1311
// 5 2 3
// 0 5
// 1 3
// 0 2
// 1 4
// 1 5
int n, k, p;
int ans, pre;
int color[MAX], nums[MAX];
int main()
{
cin >> n >> k >> p;
for (int i = 1, pric; i <= n; i++)
{
scanf("%d%d", color + i, &pric);
if (pric <= p)
{
for (int j = i; j > pre; j--)
nums[color[j]]++;
pre = i;
ans += nums[color[i]] - 1;
}
else
ans += nums[color[i]];
}
printf("%d", ans);
}
标签:pre,nums,int,客栈,选择,color,ans 来源: https://www.cnblogs.com/Wang-Xianyi/p/16650660.html