1044. Shopping in Mars (25)
来源:互联网 发布:阿里云虚拟主机代理 编辑:程序博客网 时间:2024/04/28 18:52
1044. Shopping in Mars (25)
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
分析:
(1)考察搜索,直接暴力搜索肯定有测试点过不去,有人用二分,有人用局部优化
(2)我学习的方法就是做了局部优化,若找到在J=P位置等于M的值,则下一轮J从P+1开始搜索。
(3)C输入输出也可以提高效率。
(4)需要保证程序中的jump>=0;
#include <cstdio>#define N 100001#define M 100000001int main (){int n,m,i,j;int dc[N],op[N][2];scanf("%d%d",&n,&m);for (i=0;i<n;i++){scanf("%d",&dc[i]);}int cnt,tmp,sum=M,jump=0;for (i=0;i<n;i++){tmp=0;for (j=i+jump;j<n;j++){tmp+=dc[j];if (tmp>=m){if (tmp<sum){cnt=0;sum=tmp;op[cnt][0]=i;op[cnt][1]=j;cnt++;}else if (tmp==sum){op[cnt][0]=i;op[cnt][1]=j;cnt++;}if (tmp==m&&j-i>1){jump=j-(i+1);}else jump=0;break;}}}//printf("%d",op[0][0]);for (i=0;i<cnt;i++){printf("%d-%d\n",op[i][0]+1,op[i][1]+1);}//printf("1");return 0;}
- 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)
- mysql or条件可以使用索引而避免全表
- eclipse中如何把源码打进jar包(创建有源码的jar包,或者有源码如何在eclipse中查看)
- 用gcc编译成可执行程序细节过程
- IOS中多线程的同步方法
- iOS NSString 和NSData 转换
- 1044. Shopping in Mars (25)
- android开发环境搭建
- unity3D物体的旋转和缩放
- block及arc中注意事项
- UVA - 12335 Lexicographic Order (第k大排列)
- oracle
- 键盘控制div移动
- [笔记] Android WebView使用方法
- structs2中interceptor(拦截器)的种类