1044. Shopping in Mars (25)
来源:互联网 发布:华为交换机端口命名 编辑:程序博客网 时间:2024/05/21 03:25
题目链接:http://www.patest.cn/contests/pat-a-practise/1044
题目:
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:
1. Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
2. Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
3. Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).
Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.
If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (<=105), the total number of diamonds on the chain, and M (<=108), the amount that the customer has to pay. Then the next line contains N positive numbers D1 ... DN (Di<=103 for all i=1, ..., N) which are the values of the diamonds. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print "i-j" in a line for each pair of i <= j such that Di + ... + Dj = M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i.
If there is no solution, output "i-j" for pairs of i <= j such that Di + ... + Dj > M with (Di + ... + Dj - M) minimized. Again all the solutions must be printed in increasing order of i.
It is guaranteed that the total value of diamonds is sufficient to pay the given amount.
Sample Input 1:16 153 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13Sample Output 1:
1-54-67-811-11Sample Input 2:
5 132 4 5 7 9Sample Output 2:
2-44-5
分析:
关键时刻,用cout超时,用printf正确。提醒提醒啊。
AC代码:
#include<stdio.h>#include<algorithm>#include<vector>#include<iostream>using namespace std;struct A{ int s; int e;}buf[100002];int input[100002];int main(){ //freopen("F://Temp/input.txt", "r", stdin); int n, m; int min = 0; scanf("%d%d", &n, &m); for (int i = 0; i < n; i++){ scanf("%d", &input[i]); if (min < m)min += input[i]; } int sum = 0; int idx = 0; for (int i = 0, j = 0; j <= n;){ if (sum < m){ sum += input[j]; j++; } else { if (sum == min){ buf[idx].s = i; buf[idx].e = j-1; idx++; } else if (sum < min){ min = sum; idx = 0; buf[idx].s = i; buf[idx].e = j - 1; idx++; } sum -= input[i]; i++; } } for (int i = 0; i < idx; i++){ //cout << buf[i].s+1 << "-" << buf[i].e + 1 << endl; printf("%d-%d\n", buf[i].s + 1, buf[i].e + 1); } return 0;}
截图:
用cout的话会有超时的情况:
用printf则没有问题:
——Apie陈小旭
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- 1044. Shopping in Mars (25)
- char* 和char[]的区别
- vs2015引用lib库
- 可视化自编码器训练结果
- ES5中数组的常用方法
- 欢迎使用CSDN-markdown编辑器
- 1044. Shopping in Mars (25)
- ubuntu和win7双系统引导,出现“no such device ”错误
- 稀疏自编码器符号一览表
- mic接口和line-in的区别
- C++中智能指针的设计和使用
- java spring mvc之文件上传
- Processing 教程(4) - text文本显示
- poj 2406 Power Strings 【kmp(len/(len-p[len])的灵活运用)】
- 树莓派折腾日记1-raspian安装,基本设置