A-codeforces558A

来源:互联网 发布:免费速读训练软件 编辑:程序博客网 时间:2024/05/19 19:14

题目链接:Codeforces558A


水题,第一次运行超时,用结构体快排。

不知道为什么不能一起算,要正负分开算,不然超时。




#define _CRT_SECURE_NO_DEPRECATE#include<stdio.h>#include<algorithm>#include<cstring>using namespace std;struct node{int x, a;}nor[105], nol[105];bool cmpr(const node &a, const node &b){return a.x < b.x;}bool cmpl(const node &a, const node &b){return a.x > b.x;}int main(){int n;while (scanf("%d", &n) != EOF){int ans = 0;int aa, b;int r = 0, l = 0;for (int i = 0; i < n; i++){scanf("%d %d", &aa, &b);if (aa > 0){nor[r].x = aa;nor[r].a = b;r++;}else{nol[l].x = aa;nol[l].a = b;l++;}}sort(nor, nor + r, cmpr);sort(nol, nol + l, cmpl);if (l == 0)ans = nor[0].a;else if (r == 0)ans = nol[0].a;else {int pl = 0, pr = 0;if (l > r){while (1){ans += nol[pl++].a;if (pr == r)break;ans += nor[pr++].a;if (pl == l)break;}}else{while (1){ans += nor[pr++].a;if (pl == l)break;ans += nol[pl++].a;if (pr == r)break;}}}printf("%d\n", ans);}return 0;}


0 0
原创粉丝点击