1044. Shopping in Mars 解析
来源:互联网 发布:红蜘蛛教学软件 入侵 编辑:程序博客网 时间:2024/05/08 11:24
直接暴力弄会有两个case超时。
需要优化一下。
参考这个大神的思路的 http://blog.csdn.net/tiantangrenjian/article/details/19251273
#include <iostream>#include <vector>#include <climits>using namespace std;struct node {int i;int j;int sum;};vector <node> leq;vector <node> lov;int N, M;vector <int> link;vector <int> n2o;int main() {cin >> N >> M;int temp;node tempN;for (int i = 0; i < N; i++) {scanf("%d", &temp);link.push_back(temp);}int sum = 0;n2o.push_back(sum);for (int i = 0; i < N; i++) {sum += link[i];n2o.push_back(sum);}int min = INT_MAX;for (int i = 0; i < N; i++) {int left = i;int right = N;while (left < right) {int mid = (right + left) / 2;if (n2o[mid] - n2o[i] >= M) {right = mid;}elseleft = mid + 1;}tempN.j = right;tempN.i = i;tempN.sum = n2o[tempN.j] - n2o[tempN.i];tempN.i++;if (tempN.sum == M) {leq.push_back(tempN);}else if(tempN.sum > M) {if (tempN.sum < min) {lov.clear();lov.push_back(tempN);min = tempN.sum;}else if(tempN.sum == min){lov.push_back(tempN);}}}if (leq.size() != 0) {for (int i = 0; i < leq.size(); i++) {printf("%d-%d\n", leq[i].i , leq[i].j );}}else {for (int i = 0; i < lov.size(); i++) {printf("%d-%d\n", lov[i].i , lov[i].j );}}system("pause");return 0;}
0 0
- 1044. Shopping in Mars 解析
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- shell——trap捕捉信号(附信号表)
- 如何评价微软在数据中心使用FPGA代替传统CPU的做法?
- 关键字const
- l 移植u-boot之修改代码支持DM9000网卡-3.5
- 抽象类(汽车问题)
- 1044. Shopping in Mars 解析
- P1509 找啊找啊找GF(洛谷)
- 2.Web Service 服务端两种实现
- 数据库的分表与分区
- 关于python中给种数据类型中的“+”
- [构造函数]构造函数是如何产生的?
- Android之---如何写一个很屌的动画系列(3个)
- lambda expressions are not supported at this language level
- shell编程其实真的很简单(三)