一 vector降序排序
sort默认升序排序。若要将一个vector数组降序排序,只需:
sort (a.rbegin(), a.rend());二 贪心1
你和对手各有n名队员,每名队员有其能力值。
有n场比赛比赛,每场比赛有一定分数,且派出能力值高者获胜。若已知对手每场派出的队员,求可以获得的最高分数。
solution:
将每场比赛按得分从高到低排序。
对于一场比赛,若当前剩余队员中有能力值大于对方在这场比赛派出队员的,则在本场比赛派出剩余队员中能力值大于对方派出队员的且能力值最小的队员。否则派出剩余队员中能力值最小的队员。
code:
int n, p[N], a[N], b[N];
int ans = 0;
vector <pair <int, int> > t;
set <int> s;
void solve () {
cin >> n;
for (int i = 1; i <= n; i++) cin >> p[i];
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i];
for (int i = 1; i <= n; i++)
t.push_back({p[i], a[i]}), s.insert(b[i]);
sort (t.rbegin(), t.rend());
for (auto [x, y] : t) {
auto pos = s.lower_bound(y);
if (pos == s.end()) s.erase(s.end());
else s.erase(pos), ans += x;
}
cout << ans;
}求区间中某一给定子序列的数量
题目大意:
给定一个长度为 字符串 ,同时进行 次操作:
操作1: 表示将第 个字符改为 ( 只会为 I 或 O )。
操作2: 询问字符串 中有多少对三元组 满足:
I , O , I 并且 。
solution:
建一颗线段树,对于每个区间,维护其中子序列 I、O、IO、OI、IOI的数量。合并和修改显然。