首页 > 其他分享 >PAT_A1044 Shopping in Mars

PAT_A1044 Shopping in Mars

时间:2023-10-22 11:55:21浏览次数:33  
标签:A1044 15 chain pay Shopping Mars values diamonds PAT

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 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13

Sample Output 1:

1-5
4-6
7-8
11-11

Sample Input 2:

5 13
2 4 5 7 9

Sample Output 2:

2-4
4-5
#include<bits/stdc++.h>
using namespace std;
const int N = 100000+5;
int n,m, e[N], s[N], p=N;

int main(){
	scanf("%d%d", &n, &m);
	for(int i = 1; i <= n; i++){
		scanf("%d", e+i);
		s[i] = s[i-1] + e[i];
	}
	for(int i = 1; i <= n; i++){
		int j = lower_bound(s, s+n+1, m+s[i-1])-s;
		if(s[j] - s[i-1] == m){
			p = m;
			break;
		}else if(j <= n && s[j] - s[i-1] < p)
			p = s[j] - s[i-1];
	}
	for(int i = 1; i <= n; i++){
		int j = lower_bound(s, s+n+1, p+s[i-1])-s;
		if(s[j] - s[i-1] == p) printf("%d-%d\n",i, j);
	}
	return 0;
}

总结
1、 #二分 s[i]表示从e[1]e[i]的和,s为递增序列,可以进行二分。遍历左端点i,二分查找右端点j,寻找j使得s[j] >= m+s[i-1]。第一遍遍历i确定支付金额p,第二遍遍历输出结果。

标签:A1044,15,chain,pay,Shopping,Mars,values,diamonds,PAT
From: https://www.cnblogs.com/Afinis/p/17780226.html

相关文章

  • 多路径multipath共享磁盘配置
    1. 配置共享磁盘1.1. 主机关机的情况下,添加4块硬盘,每块磁盘设置如下  1.2. 另外一台主机添加上面已经存在的磁盘,同样设置 1.3. 修改两台虚拟机的配置文件(.vmx)disk.locking="FALSE"disk.EnableUUID="TRUE"scsi1:1.SharedBus="Virtual"......
  • 内核文档翻译(chatgpt) —— Pathname lookup (路径名查找)
    原文:https://www.kernel.org/doc/html/latest/filesystems/path-lookup.html内核中文件系统相关的文档汇总:FilesystemsintheLinuxkernelThiswrite-upisbasedonthreearticlespublishedatlwn.net:PathnamelookupinLinuxRCU-walk:fasterpathnamelookupinLi......
  • c: Visitor Pattern
     /***@filevalidator.h*@authoryourname([email protected])*@brief观察者模式VisitorPattern来源:C现代编程集成开发环境、设计模式、极限编程、测试驱动开发、重构、持续集成日.花井志生著,杨文轩译,人民邮电出版社*@version0.1*@date2023-10-21......
  • 动态加载目录进classpath
    参考文档:https://www.codelast.com/%E5%8E%9F%E5%88%9B-java%E5%8A%A8%E6%80%81%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E7%9B%AE%E5%BD%95%E5%88%B0classpath%E4%B8%AD/ publicstaticloadFoldertoClasspath(){FileprogramRootDir=newFile("./");URL......
  • PAT_A 1010 Radix
    Givenapairofpositiveintegers,forexample,6and110,canthisequation6=110betrue?Theansweris yes,if6isadecimalnumberand110isabinarynumber.Nowforanypairofpositiveintegers N1​ and N2​,yourtaskistofindtheradixofon......
  • 【愚公系列】2023年10月 二十三种设计模式(十九)-观察者模式(Observer Pattern)
    ......
  • 利用 CSS 的 clip-path 属性快速画三角形、气泡框
    clip-path 结合polygon函数,可以快速切出一个三角形、气泡框。a.三角形有三个顶点,因此 polygon 需要传三个参数,每个参数是顶点的x和y轴位置百分比:#triangle-1{-webkit-clip-path:polygon(50%0,100%100%,0100%);clip-path:polygon(50%0,100%100%,......
  • xxl-job执行java任务报错: unable to find valid certification path to requested tar
    1、错误:xxl-job调用https接口显示证书验证失败[错误信息:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofindvalidcertificationpathtorequestedtarget]2023-10-2015......
  • PAT_A 1085 Perfect Sequence
    Givenasequenceofpositiveintegersandanotherpositiveinteger p.Thesequenceissaidtobea perfectsequence if M≤m×p where M and m arethemaximumandminimumnumbersinthesequence,respectively.Nowgivenasequenceandaparameter p,y......
  • python sys.path介绍
    pythonsys.path介绍介绍当我们导入模块时,python解释器会通过sys.path中的环境变量搜索。sys.path是一个列表,里面包含已添加到环境变量中的路径。使用sys.path.append({路径})可以往里面添加自定义的环境变量。使用当我们想要导入某个文件中的文件失败时,可以将其文件夹路......