2017年多校赛第二场 1003.Maximum Sequence(贪心)

来源:互联网 发布:水利水电造价软件 编辑:程序博客网 时间:2024/06/07 19:43

题解里面写的挺高大上的,但是当时过了那么多人,肯定是往着水题想,所以决定试一发贪心。当时队友用线段树来算,我现在补题用优先队列写,算是等效的吧。但是我wa了三四发,队友当时一发过的- -

wa点如下

1,队列忘记清空

2,一开始用pair,然后想到pair在优先队列里面第二个元素也会从大到小排列,我第二个元素存放的是当前这个值是属于第几个a_i的,那么在每次取出队首的元素的时候你还需要判断这个元素是否已经小于b_i了,小于则出队列。但是如果你有两个值相等的元素在队列里面,你竟然先判断了元素靠后那个,那么后面答案也会是错误的。所以自己开个结构体来重载运算符。。。

3,我忘记取模了。。。GG

代码如下:

#include<bits/stdc++.h>using namespace std;typedef long long ll;typedef pair<int, int> pii;const int mod = 1e9 + 7;struct node {int num, id;node(){} node(int x, int y) {num = x;id = y;}bool operator < (const node& t) const {if(num == t.num)return id > t.id;return num < t.num;}};priority_queue <node> q;int b[250005];int main() {int n, a;while(cin >> n) {while(!q.empty())q.pop();for(int i = 1; i <= n; i++) {scanf("%d", &a);a -= i;q.push(node(a, i));}for(int i = 0; i < n; i++)scanf("%d", &b[i]);sort(b, b + n);ll ans = 0;for(int i = 0; i < n; i++) {node tmp = q.top();while(tmp.id < b[i]) {q.pop();tmp = q.top();}ans = ans + tmp.num;ans %= mod;q.push(node(tmp.num - (n + i + 1), n + i + 1));}printf("%I64d\n", ans);}return 0;} 


阅读全文
0 0
原创粉丝点击