PAT Advanced 1044. Shopping in Mars (25)
来源:互联网 发布:搞笑一家人知乎 编辑:程序博客网 时间:2024/06/14 20:43
InputSpecification:
Eachinput file contains one test case. For each case, the first line contains 2numbers: N (<=105), the total number of diamonds on the chain,and M (<=108), the amount that the customer has to pay. Then thenext line contains N positive numbers D1 ... DN (Di<=103 forall i=1, ..., N) which are the values of the diamonds. All the numbers in aline are separated by a space.
OutputSpecification:
For eachtest case, print "i-j" in a line for each pair of i <= j such thatDi + ... + Dj = M. Note that if there are morethan one solution, all the solutions must be printed in increasing order of i.
If thereis 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 isguaranteed that the total value of diamonds is sufficient to pay the givenamount.
Sample Input 1
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
input: n=16
m=15
D[1…n]
ShoppinginMars(n,m,D):
暴力法:
fori =0 to n
sum=0;
forj=I to n
sum+=D[j]
if(sum(I,j)>m)
break;
ifsum(I,j)==m
print(I,j)
时间复杂度为O(n^2),显然会超时
再仔细想想的话,还有信息可以利用:正对每个i,一定都顶多只有一个对应的临界的j,恰好使得Di + … + Dj >= M, 我们于是遍历每个i,当找到这个i对应的j以后,我们要找下一个i+1对应的j的时候,应该从j+1开始找。
时间复杂度为O(n)
最后的思路就是:
要搞2次,每次搞的时候记录比n大的最小的值biggerValue,第二次搜这个值biggerValue
下面就是愉快的撸代码时间了
开撸,开撸。
写起来还是有一点麻烦的,码力下降(^^其实本身就没有码力)
附上我的代码,思路是正确的,结果超时了,很绝望。
import java.util.Scanner; /** *Created by SSW on 2017/11/20. */public class A_ShoppinginMars_1044 { public static void main(String[] args){ int n,m; Scanner scanner=new Scanner(System.in); n=scanner.nextInt(); m=scanner.nextInt(); int[] D=new int[n+1]; for(int i=1;i<=n;i++){ D[i]=scanner.nextInt(); } D[0]=0; int sum=0; int lastPosition=1; int biggerValue=Integer.MAX_VALUE; boolean flag=true; for(int i=1;i<D.length;i++){ sum-=D[i-1]; for(int j=lastPosition;j<D.length;j++){ sum+=D[j]; if(sum>m){ lastPosition=j; if(sum<biggerValue) biggerValue=sum; sum-=D[j]; break; } if(sum==m){ flag=false; System.out.println(i+"-"+j); lastPosition=j+1;// System.out.println(lastPosition); break; } } } if(flag){ lastPosition=1; sum=0; for(int i=1;i<D.length;i++){ sum-=D[i-1]; for(int j=lastPosition;j<D.length;j++){ sum+=D[j]; if(sum>biggerValue){ lastPosition=j; sum-=D[j]; break; } if(sum==biggerValue){ System.out.println(i+"-"+j); lastPosition=j+1; break; } } } } }}
- PAT Advanced 1044. Shopping in Mars (25)
- 【C++】PAT(advanced level)1044. Shopping in Mars (25)
- 【PAT】【Advanced Level】1044. Shopping in Mars (25)
- PAT (Advanced Level) Practise 1044 Shopping in Mars (25)
- PAT (Advanced Level) Practise 1044 Shopping in Mars (25)
- 1044. Shopping in Mars (25)-PAT
- PAT A 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars (25)
- 【PAT】1044. Shopping in Mars (25)
- [pat]1044. Shopping in Mars (25)
- PAT 1044. Shopping in Mars (25)
- 【PAT甲级】1044. Shopping in Mars (25)
- PAT(A) - 1044. Shopping in Mars (25)
- PAT(A) - 1044. Shopping in Mars (25)
- PAT甲级.1044. Shopping in Mars (25)
- js对象转换
- 神经网络中激活函数的真正意义
- OpenCV实践之路——矩形图片的对角线分割
- opencv was not found in the pkg-config search path
- Java序列化与反序列化
- PAT Advanced 1044. Shopping in Mars (25)
- 分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
- C和指针之字符串编程练习1
- express(2)
- 广搜模板
- 微信小程序循环:Wx:for
- 321. Create Maximum Number
- HTTP 错误 500.21
- 1047. 编程团体赛(20)