BNUOJ 13268 Aladdin and the Optimal Invitation
来源:互联网 发布:王良正射鱼器淘宝 编辑:程序博客网 时间:2024/06/09 22:59
思路:x轴和y轴的情况是独立的,可以分开考虑,那么只要枚举位置,能维护最小值即可,这只要把公式拆开,预处理出两个前缀和即可,一个是w的前缀和,一个是w * x的前缀和
代码:
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 50005;typedef long long ll;int t, n, m, q;int x[N], y[N];ll sum[N][2], w[N];ll cal(int v, int n) { ll ans1 = sum[v][1] * v - sum[v][0]; ll ans2 = (sum[n][0] - sum[v][0]) - (sum[n][1] - sum[v][1]) * v; return ans1 + ans2;}int gao(int *x, int n) { memset(sum, 0, sizeof(sum)); for (int i = 0; i < q; i++) { sum[x[i]][0] += w[i] * x[i]; sum[x[i]][1] += w[i]; } for (int i = 1; i <= n; i++) { for (int j = 0; j < 2; j++) sum[i][j] += sum[i - 1][j]; } int ans = 1; for (int i = 1; i <= n; i++) { if (cal(i, n) < cal(ans, n)) ans = i; } return ans;}int main() { int cas = 0; scanf("%d", &t); while (t--) { scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < q; i++) scanf("%d%d%lld", &x[i], &y[i], &w[i]); printf("Case %d: %d %d\n", ++cas, gao(x, n), gao(y, m)); } return 0;}
1 0
- BNUOJ 13268 Aladdin and the Optimal Invitation
- lightoj 1349 Aladdin and the Optimal Invitation
- LightOJ 1349 - Aladdin and the Optimal Invitation(数学啊)
- lightoj 1349 - Aladdin and the Optimal Invitation 贪心 中位数
- lightoj 1349 - Aladdin and the Optimal Invitation 【中位数的运用】
- lightoj1349 - Aladdin and the Optimal Invitation【求中位数】
- Light OJ 1349 Aladdin and the Optimal Invitation
- lightoj 1349 - Aladdin and the Optimal Invitation (中位数的运用)
- Aladdin and the Flying Carpet
- Aladdin and the Flying Carpet
- Aladdin and the Flying Carpet
- LightOJ 1342 - Aladdin and the Magical Sticks
- lightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1341Aladdin and the Flying Carpet
- LightOJ 1341 - Aladdin and the Flying Carpet
- LightOJ1341 Aladdin and the Flying Carpet
- Aladdin and the Flying Carpet [整数分解]
- Object-C中的NSString
- cocos2d-x随机数的产生
- Android入门:隐式Intent
- BCB ServerSocket多线程示例
- CH3: 列表
- BNUOJ 13268 Aladdin and the Optimal Invitation
- effective c++ 笔记 (9-12)
- [Python学习] 专题一.函数的基础知识
- OpenGL中的原语组装和光栅化
- tar压缩与分割大文件
- lua语言环境搭建与测试
- [Python学习] 专题二.条件语句和循环语句的基础知识
- Linux内核云课堂作业第四章(上)
- [Python学习] 专题三.字符串的基础知识