HDU 3884(水过O(N ^ 2)的复杂度)
来源:互联网 发布:linux命令大全 编辑:程序博客网 时间:2024/05/16 06:48
题目连接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3884
题目大意:在花费限制为K的情况下,把最多的植物移动到同一个点,每一个植物移动的花费为移动的距离;
一看数据是10^4,如果暴力是n^2,肯定过不了
但是AC了,让我不禁烦死了一小会.......
当我们得知算法的复杂度为n方时,可以再深入分析,第一个n是必须完整遍历的,但是第二个n就是随机了,不一定完整遍历,所以可以试一试,不过真心不建议在比赛中试验这种题目,如果真的TLE,会浪费时间。
AC如下
LL cost = min(cost1 , cost2); if(k < cost) break;这个判断必须加,不加的TLE原因是在k不足以完成之后的mov时,跳出循环,避免不必要的查询
#include <bits/stdc++.h>using namespace std;typedef pair<int, int> P;typedef long long LL;#define INF 0x3f3f3f3f#define PI acos(-1)#define MAX_N 10005#define LOCALstruct node{ LL x, p;}A[MAX_N];bool mycmp(node c, node d){ return c.x < d.x;}//int cmp(const void *p1,const void *p2) //quickly sort//{// return ((struct node *)p1)->x-((struct node *)p2)->x;//}int main(){#ifdef LOCALfreopen("b:\\data.in.txt", "r", stdin);#endif LL n, K; while(~scanf("%I64d%I64d", &n, &K)) { for(int i = 0; i < n; i++) { scanf("%I64d%I64d", &A[i].x, &A[i].p); }// qsort(A, n, sizeof(A[0]), cmp); sort(A, A+n, mycmp); LL ans = -INF; for(int i = 0; i < n; i++) //枚举每个点 { LL nowsum = A[i].p; LL k = K; LL le, ri; le = i-1; ri = i+1; while(k > 0) { if(le < 0) le = i; if(ri >= n) ri = i; if(ri == i && le == i) break; //到达边界 LL cost1 = INF; LL cost2 = INF; if(le != i) cost1 = A[i].x - A[le].x; if(ri != i) cost2 = A[ri].x - A[i].x; LL cost = min(cost1 , cost2); if(k < cost) break; if(cost1 < cost2) //left is pre { nowsum += min(k/cost1, A[le].p); k -= cost1 * min(k/cost1, A[le].p); le--; } else if(cost1 == cost2) { nowsum += min(k/cost1, A[le].p + A[ri].p); k -= cost1 * min(k/cost1, A[le].p+ A[ri].p); le--; ri++; } else { nowsum += min(k/cost2, A[ri].p); k -= cost2 * min(k/cost2, A[ri].p); ri++; } } if(nowsum > ans) ans = nowsum; } printf("%I64d\n", ans); } return 0;}
0 0
- HDU 3884(水过O(N ^ 2)的复杂度)
- 关于O(n^2)复杂度的体验
- 将O(n^2)的空间化为O(n)的空间复杂度
- 数组中未出现的最小正整数(时间复杂度O(N),空间复杂度O(1))
- 时间复杂度O(n),空间复杂度O(1)的排序
- 求n很大时求,<= n的素数的个数(时间复杂度o(n ^ 2 / 3)模板
- <o(2n)最坏复杂度求数组的最大最小值
- 约瑟夫环O(n)复杂度的解法
- 使用O(n)的时间复杂度合并两个数组
- 关于中位数的时间复杂度为什么是O(n)
- 时间复杂度从O(n^3)到O(n^2)的优化
- 一、排序(时间复杂度 O(n^2))
- 最长有序子序列 时间复杂度O(n^2)
- 左旋字符串的一种野路子解法,时间复杂度O(2N)
- 复杂度为o(n)的计数排序
- 时间复杂度为O(n)的排序
- 关于时间复杂度O(N)的思考
- 建堆的时间复杂度O(N)
- 8天学通MongoDB——第五天 主从复制
- Android之Service学习总结
- 数据结构实验之图论八:欧拉回路
- Java json转Map,转bean,转List<bean>
- jzoj 4714. 【NOIP2016提高A组模拟8.19】公约数 数(luan)学(gao)
- HDU 3884(水过O(N ^ 2)的复杂度)
- n a^o7 !
- 解决java.lang.RuntimeException: Unable to start activity Activity无法打开的问题
- NandFlash初始化
- 【NOIP2016提高A组模拟8.19】(雅礼联考day2)树上路径
- 数据库增删改查基本语句
- 【NOIP2016提高A组模拟8.19】公约数
- 8天学通MongoDB——第六天 分片技术
- poj 1191 棋盘分割 dp-进阶