PAT1044 Shopping in Mars

这篇具有很好参考价值的文章主要介绍了PAT1044 Shopping in Mars。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

个人学习记录,代码难免不尽人意。
做了这么多题难得本题不看答案一遍过,很是激动。

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:

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).
Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
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.
PAT1044 Shopping in Mars,PTA,算法,c++,pat
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<cstdio>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<cmath>
using namespace std;
int num[100010];
int main(){
   int n,m;
   scanf("%d%d",&n,&m);
   for(int i=0;i<n;i++){
   	scanf("%d",&num[i]);
   }
   int i=0,j=0;
   int temp=0;
   bool flag=false;
   int cost=1000000000;
   vector<pair<int,int> > v;
   int count=0;
   while(i<n||j<n){
//   	cout << i << " " <<j << endl;
   	if(temp<m){
   		if(j==n) break;
   		temp+=num[j];
   		j=min(n,j+1);
	   }
	else if(temp==m){
		flag=true;
		printf("%d-%d\n",i+1,j);
		if(j+1<n){
			temp=temp-num[i]+num[j];
			j++;
		}
		else{
		j=min(n,j+1);
		temp=temp-num[i];
		} 
		i=min(n,i+1);
		
	}
	else{
		if(temp<cost){
			cost=temp;
			v.clear();
			v.push_back(make_pair(i+1,j));
		}
		else if(temp==cost){
			v.push_back(make_pair(i+1,j));
		}
	        temp-=num[i];
			i=min(n,i+1);
			
	}
   }

   if(!flag&&!v.empty()){
   	for(int k=0;k<v.size();k++){
   		printf("%d-%d\n",v[k].first,v[k].second);
	   }
   	
   }
   return 0;
}

利用了《算法笔记》中讲述的“two points”思想,设置了i,j两个下标来从左到右遍历数组,其中记录num[i]到num[j]的和temp,判断temp和m的关系,分为三种情况:①如果temp小于m,则让j++,如果j已经等于边界n,则说明不可能再得到新结果了,直接break。②如果temp等于m,则此时i和j就是我们想要输出的结果,直接输出,然后让i和j都加一,相应的temp减去num[i]加上num[j]的值。③如果temp大于m,则让i++,相应的减去temp对应的值。文章来源地址https://www.toymoban.com/news/detail-642458.html

到了这里,关于PAT1044 Shopping in Mars的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT

    最近需要使用一下selenium,刚运行就报错了。。。 前提准备: 1.安装selenium 2.下载chrome对应版本的chromedriver 代码就是一个简单的demo: 运行报错: 网上说要把chromedriver放到环境变量,放进去还是报错!! 然后就直接看源码吧: 这个过程很繁琐,很枯燥,嫌废话连篇请直接翻到文

    2024年01月20日
    浏览(29)
  • PAT B1049

    题目 给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列 { 0.1, 0.2, 0.3, 0.4 },我们有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这 10 个片段。 给定正整数数列,求出全部片段包含的所有的数之和。

    2024年02月02日
    浏览(25)
  • PAT 甲级【1010 Radix】

    本题范围long型(35)^10 枚举radix范围上限pow(n/a0,1/m)上,考虑上限加1.范围较大。使用二分查找枚举 代码如下 本页面将简要介绍二分查找,由二分法衍生的三分法以及二分答案。 二分查找(英语:binary search),也称折半搜索(英语:half-interval search)、对数搜索(英语:logar

    2024年02月08日
    浏览(27)
  • pat乙级1002

    关键思路: 大数输入问题 1010010100 用int和long long都是远远不够的,因此用字符数组来输入,然后再转换成整数 数字与拼音的转换 本题用最基本的方法,使用switch开关语句实现转换 拼音数字间有 1 空格,但一行中最后一个拼音数字后没有空格。 此类问题一般使用以下代码实

    2023年04月14日
    浏览(30)
  • NAT与PAT

        NAT(Nct.work Adldrnns Trans l at.i on)又称为网络地址转换,用于实规私不网络和公不网络之问的互访。 公有网络地址(以下简称公网地址)是指在互联网上全球唯一的IP地址。2019年11月26日,是人类互联网时代值得纪念的一-天,全球近43亿个IPv4地址己正式耗尽。 私有网络地址(以下简

    2024年02月11日
    浏览(30)
  • pat乙题1002

    笔记: 1.字符数组char s[]的长度用sizeof(), strlen() 字符串string s长度s.size()和s.length()都是求的实际的占位,不算0的 2.switch: 用一个显化记录的字符串不就行了。 3.把数字sum粘贴到字符串数组s 上 改:用动态字符串存入。 4.既然用的char s[],想把字符串里的数字加和,当然要用-‘

    2024年02月16日
    浏览(25)
  • PAT 甲级考试【1003 Emergency】

    题目: As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead

    2024年02月08日
    浏览(30)
  • PAT A1032 Sharing

    1032 Sharing 分数 25 作者 CHEN, Yue 单位 浙江大学 To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, loading and being are stored as showed in Figure 1. Figure 1 You are supposed to find the starting

    2024年02月03日
    浏览(25)
  • 1028 List Sorting (PAT甲级)

    Excel can sort records according to any column. Now you are supposed to imitate this function. Input Specification: Each input file contains one test case. For each case, the first line contains two integers N (≤105) and C, where N is the number of records and C is the column that you are supposed to sort the records with. Then N lines follow, eac

    2024年02月15日
    浏览(29)
  • pat甲级 1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID\\\'s. Input Specification: Each inp

    2024年04月15日
    浏览(22)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包