PAT甲级1044,1047解题报告

云服务器安装ros教程 要在 CentOS 7 系统上安装 RouterOS,您需要将 RouterOS 安装为虚拟机或直接安装到物理硬盘上。这里提供的是将 RouterOS 安装到物理硬盘上的步骤,这将完全替换现有的 CentOS 7 系统。请确保您已经备份了所有重要数据。 阅读详情

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 (≤10​5​​), the total number of diamonds on the chain, and M (≤10​8​​), the amount that the customer has to pay. Then the next line contains N positive numbers D​1​​⋯D​N​​ (D​i​​≤10​3​​ 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 >Mwith (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

题目大意:就是给一个数组,一个目标值,求哪些连续区间和是等于这个目标值的,如果没有相等的,那就找最接近的。

解题思路:我是用树状数组和二分查找实现了。。后来查了一下不用树状数组也行的。算了,就当回顾了一下。

先讲树状数组,主要作用是对给定区间的求和与维护。利用一种像树一样的数据结构树状数组来实现。具体可以自行百度学习,这里简单讲就是把数组下标看成二进制。用一个数组记录一段和的值,比如c[6]记录的就是6-lowbit(6)到6的和,核心在于把数字看成二进制,这里的lowbit是返回最后一位在二进制表示中1的位置。比如6表示出110,然后lowbit就是2,然后就是记录第4个和第5个的和。然后就解决了求和的问题,然后在是二分查找,查找到一个比i大而且第一次比M大的索引位置,然后后退一个,如果正好是M。那么查找出所有是M位置的数就行了。如果不是那么就查找当前这个和就行了。目前不知道什么效率更高的算法了。然后我查了一下。。不用树状数组用普通的求和,就每次读入记录一下和也行。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
int TreeArr[100005];
int lowbit(int x) {
	return x&(-x);
}
int sum(int x, int *c) {
	int res = 0;
	while (x > 0) {
		res += c[x];
		x -= lowbit(x);
	}
	return res;
}
void update(int x, int data, int *c,int size) {
	while (x <= size) {
		c[x] += data;
		x += lowbit(x);
	}
}
int BinarySort(int l, int r, int g) {
	int left = l;
	int right = r;
	int mid;
	while (left < right) {
		mid = (left + right) / 2;
		if (g < sum(mid,TreeArr)) {
			right = mid;
		}
		else {
			left = mid + 1;
		}
	}
	return left;
}

int main()
{
	int N;
	int M;
	cin >> N >> M;
	memset(TreeArr, 0, sizeof(int)*N);
	for (int i = 1; i <= N; i++) {
		int cur;
		cin >> cur;
		update(i, cur, TreeArr, N);
	}
	int goal=99999999;
	
	for (int i = 1; i <= N; i++) {
		int j = BinarySort(1, N + 1, sum(i - 1, TreeArr)+M);
		if (sum(j - 1, TreeArr) - sum(i - 1, TreeArr) == M) {
			goal = M;
			break;
		}
		else if(j<=N&&sum(j,TreeArr)-sum(i-1,TreeArr)<goal){
			goal = sum(j, TreeArr) - sum(i - 1, TreeArr);
		}
	}
	for (int i = 1; i <= N; i++) {
		int j = BinarySort(1, N + 1, sum(i - 1, TreeArr) + goal);
		if (sum(j - 1, TreeArr) - sum(i - 1, TreeArr) == goal) {
			cout << i << "-" << j -1<< endl;
		}
	}
	return 0;
}

1047 Student List for Course (25 分)

Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the student name lists of all the courses.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤40,000), the total number of students, and K (≤2,500), the total number of courses. Then N lines follow, each contains a student's name (3 capital English letters plus a one-digit number), a positive number C (≤20) which is the number of courses that this student has registered, and then followed by C course numbers. For the sake of simplicity, the courses are numbered from 1 to K.

Output Specification:

For each test case, print the student name lists of all the courses in increasing order of the course numbers. For each course, first print in one line the course number and the number of registered students, separated by a space. Then output the students' names in alphabetical order. Each name occupies a line.

Sample Input:

10 5
ZOE1 2 4 5
ANN0 3 5 2 1
BOB5 5 3 4 2 1 5
JOE4 1 2
JAY9 4 1 2 5 4
FRA8 3 4 2 5
DON2 2 4 5
AMY7 1 5
KAT3 3 5 4 2
LOR6 4 2 4 1 5

Sample Output:

1 4
ANN0
BOB5
JAY9
LOR6
2 7
ANN0
BOB5
FRA8
JAY9
JOE4
KAT3
LOR6
3 1
BOB5
4 7
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1
5 9
AMY7
ANN0
BOB5
DON2
FRA8
JAY9
KAT3
LOR6
ZOE1

题目大意:和以前的选课类似,这次输出以课程为主线序的表。

解题思路:照常求解有最后一个样例是过不去的,其实有点ACM味道,就是卡printfscanf和cout,cin的时间差,所以全部改成scanf和printf就过了,因为前者耗时短。有人会问C++string怎么读入,其实很简单的,string有个c_str函数变成char,然后有个利用copy函数可以把char里的内容复制到string里面。所以完全都是可以改成printf和scanf的,因为平时练习碰到了,所以以后尽量还是少用cin和cout。

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
map<int, vector<string>> a;
bool cmp(string a, string b) { return a < b; }
int main()
{
	int n; int k;
	scanf("%d %d", &n, &k);
	for (int i = 0; i < n; i++) {
		string cur;
		cin >> cur;
		int cu;
		scanf("%d", &cu);
		for (int j = 0; j < cu; j++) {
			int tp;
			scanf("%d", &tp);
			a[tp].push_back(cur);
		}
	}
	for (int i = 0; i < k; i++) {
		printf("%d %d\n", i + 1, a[i + 1].size());
		sort(a[i + 1].begin(), a[i + 1].end(),cmp);
		for (int j = 0; j < a[i + 1].size(); j++) {
			printf("%s\n", a[i + 1][j].c_str());
		}
	}
	return 0;
}

 

数字电路逻辑与设计实验一 门电路逻辑功能及测试 (1)选二输入四异或门电路 74LS86,按图 1-8 接线,输入端 1、2、4、5 接逻辑开 关(S1~S4),输出端 A、B、Y 接电平显示发光二极管.用六反相器(非门)按图 1-11 接线,输入 80KHz 连续脉冲,用双踪示波器测输入, 输出相位差,计算每个门的平均传输延迟时间的 tpd 值。测试不同的输入组合:对门电路进行全面的测试,尝试不同的输入组合,包括各种可能的逻辑状态。检查门电路的输入:确认门电路的输入是否正确,并且输入信号的电平符合门电路的规格要求。确保输入信号的稳定性和准确性。 阅读详情

相关推荐

PMOS缓启动电路设计实战:用LTspice优化上电时序,把60A冲击降到15A

本文通过一个实战案例,深入解析了PMOS缓启动电路如何有效抑制上电冲击电流。文章指出,低ESR电容是导致冲击电流飙升的元凶,并详细阐述了利用LTspice仿真工具,通过优化栅极电阻和栅源电容等参数,将高达60A的冲击电流成功降至15A的设计与优化过程,为电源开关电路设计提供了可复用的方法论。

onion的博客 919

PAT甲级-1044 Shopping in Mars

PAT甲级-1044 Shopping in Mars C++

weixin_74092648的博客 892

手把手教你用若依WMS搭建企业级仓库管理系统(Vue+Spring Boot实战)

本文提供了一份基于若依框架(RuoYi)快速搭建企业级仓库管理系统(WMS)的实战指南。详细介绍了从环境准备、项目初始化到核心功能模块(如入库流程、库存管理看板)深度定制的全过程,并涵盖Lodop打印集成、移动端PWA适配及生产环境Docker部署等高级主题,帮助开发者高效构建稳定可用的WMS系统。

weixin_29015899的博客 577

PAT甲级1044

#include&amp;lt;iostream&amp;gt; #include&amp;lt;vector&amp;gt; using namespace std; struct Node{ int value; int key; Node *next; }; struct Res{ int a, b; int s; }; void ins(Node *t, int value, int key){ Node *...

宇宙-AnonyMous- 284

Electron多窗口开发避坑指南:Pinia状态同步的3种解决方案对比

本文深入探讨了在Electron多窗口应用中实现Pinia状态同步的三种核心方案。针对Electron多窗口架构下状态隔离的挑战,文章对比了基础事件通信、共享存储以及智能Pinia插件等方案的优劣与适用场景,并提供了生产级的实现指南与选型建议,帮助开发者构建健壮高效的桌面应用。

spice的博客 491

LangChain + PlayWright实战:5分钟搞定网页自动化测试(附完整代码)

本文详细介绍了如何利用LangChain与PlayWright构建智能网页自动化测试系统。通过环境配置、核心架构设计、实战Agent构建及性能优化等步骤,开发者可以快速实现高效的Web操作自动化,提升测试效率。文章还提供了完整代码示例和典型问题解决方案。

weixin_27791839的博客 251

PAT乙级—1044. 火星数字(20)-native

火星人是以13进制计数的:地球人的0被火星人称为tret。 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。 例如地球人的数字“2

Ken 4285

图的一些基本知识:图,邻居,度矩阵,邻接矩阵

目录前言图(graph)邻居(neighborhood)度矩阵(degree)邻接矩阵(adjacency)参考文献 前言       本文介绍了一些图论的基础知识,包括图的表示、邻接矩阵、度矩阵等(边不带权值的情况),本文中某些图片或者知识的参考/来源已列于本文最后。 图(graph)       图用G=(V, E)表示,V中元素为...

luzaijiaoxia0618的博客 6万+

PAT 1044火星数字的代码实现及易错点分析(C语言)

题目 火星人是以13进制计数的: 地球人的0被火星人称为tret。 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。 例如地球...

bawangtu的博客 3734

YOLO11改进|卷积篇|引入多样化分支块DBB

YOLO11中添加多样化分支块DBB

A1983Z的博客 1460

Python爬虫实战:基于Scrapy与Playwright的搜狐新闻高效爬取

在当今信息爆炸的时代,网络爬虫技术已成为数据获取和分析的重要工具。作为国内领先的新闻门户网站,搜狐新闻包含了大量有价值的新闻数据,这些数据对于舆情分析、市场研究等领域具有重要意义。本文将详细介绍如何使用Python最新的爬虫技术栈(Scrapy框架结合Playwright浏览器自动化工具)来高效爬取搜狐新闻数据。pythontitle = scrapy.Field() # 新闻标题url = scrapy.Field() # 新闻链接。

2201_76125261的博客 1561

pat 1044

开始做这题的时候超时比较严重,有很就是第二个嵌套循环不从0开始,而是根据上一次的循环中找到一个地方开始,为什么呢?因为内层嵌套每一次结束循环有两个原因:1.找到了刚好满足题目条件的距离,得到k;2.没有找到满足题目条件的距离,但是找到了刚好大于给定距离的k;那么下一次嵌套循环从k开始,因为k前面的元素一定不会满足下一次的嵌套循环的题目要满足的条件。所以代码虽然有两个for循环,但实际上时间复杂度为

从这里开始...... 1354

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

PAT A1044

#include &lt;iostream&gt; #include &lt;map&gt; using namespace std; int sum[100010]={0}; map&lt;int,int&gt; ans; int binarys(int left,int right,int x){ while(left&lt;right){ int mid=(left+right)/2;...

weixin_37565706的博客 277

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

PAT-B 1044. 火星数字

题目内容: 火星人是以13进制计数的: 地球人的0被火星人称为tret。 地球人数字1到12的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。 火星人将进位以后的12个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer

板蓝根的博客 1604

PAT 1044. Shopping in Mars

题目:http://pat.zju.edu.cn/contests/pat-a-practise/1044 题解: 在一个长度为n的序列中找长度为m的连续子序列,如果不存在,则找大于m的最小连续子序列长度。 最后输出所有符合条件的连续子序列的两个端点。 枚举加二分搜索。 代码: #include #include #include #include #include #include

Ted's 游乐园 1011

PAT乙级1044. 火星数字(C语言)

PAT乙级真题题解目录 /* * (读取字符要注意回车的处理 可回顾一下 1018. 锤子剪刀布) * 1. 创建二维字符数组的 长度 * 2. 含高位的火星文,如果低位是0,不输出 */ #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;ctype.h&gt; int main() { ...

对方北方 1109

PAT 1044

题目 给定一串数字,和一个目标值,要求求出一组子序列和等于目标值的一对儿下标吗,如果没有相等的,找到一个大于目标值的最小值。 思路一 暴力 会超时 直接遍历每个个可能性 中间可以提前跳出的,就是条件多一些,,有很多if。。整体代码写的不好。 有两个点会超时,有一个点答案错误,应该也和这个方法有关#include<cstdio> #include<vector> using...

qq_44910084的博客 215

PAT 甲级 1047  Student List for Course

1047 Student List for Course (25 point(s)) Zhejiang University has 40,000 students and provides 2,500 courses. Now given the registered course list of each student, you are supposed to output the stu...

漫浸天空的雨色的备忘录~ 840

个人练习-PAT甲级-1044 Shopping in Mars

题目链接https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 很久没有练习,为了考试开始临时抱佛脚。。 思维有点钝化,起初看到就是简单的遍历区间,但用脚趾头想都知道会超时。看了柳神思路大概是用二分法。但已经不记得二分法怎么写了,于是先抄了一遍,对着抄的代码再做分析。抄的时候感慨柳神写得真是漂亮,自己简直毫无改进余地。。 完整代码: #include<iostream> #include<v

Rstln的博客 188

PAT甲级真题1044 火星购物

在火星购物是一个完全不同的体验。 火星人用被链子连在一起的钻石付款,每颗钻石都有一定的价值。 付款时,只能在链条的某一位置上切割一次,然后一些钻石就会一个接着一个的从链条上脱落。 一旦钻石脱离链条,就不能在穿回去。 例如,如果我们有一个钻石链条,上面有 8 颗钻石,价值分别为 3,2,1,5,4,6,8,7元,而我们需要支付的价格为 15 元。 我们有三种选择: 在 4 和 6 之间剪断链条,然后取走前 55 颗钻石,价值为 3+2+1+5+4=15。 在 5 之前或 6 之后剪断链条,然后取走第 4∼6

qq_43738331的博客 372

1044 Shopping in Mars (25 分)-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

神笔码农.的博客 741

PAT甲级-二分查找类型-1044 Shopping in Mars解题思路

1044 Shopping in Mars (25 分) 思路 lower_bound当查找不出值,会返回一个超出范围的下标 用scanf、printf避免超时 代码 #include<bits/stdc++.h> using namespace std; int main() { int n,m; cin>>n>>m; int num,sum[100005]; sum[0]=0; for(int i = 1;i<=n;

weixin_43999137的博客 392

pat1044. Shopping in Mars

1044. Shopping in Mars (25) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Shopping in Mars is quite a different exp

Greefseed的专栏 1011

【PAT(甲级)】1047 Student List for Course(超时问题)

给出学生选课信息,按照课程后面跟学生姓名的方式输出。字符串与数字对应的问题,用map。后面存储的记得用vector而不是set,set太耗费时间了。因为要求名字按照字典顺序排列,所以输出的时候要用sort对vector排序一遍。...

weixin_55202895的博客 303

pat甲级1044

pat甲级1044记录一下pat刷题历程注意点 记录一下pat刷题历程 注意点 刚开始写的时候每次都从头开始求和,然后理所当然地超时了,然后翻了一下算法笔记,借鉴了书上把从第1个结点到第n个结点的距离储存到sum数组中的方法 2.在没有刚好相加等于给定值的情况下,要输出相加最接近给定值的解,所以一定要遍历两遍 ###代码(渣渣代码) #include #include using names...

weixin_43927586的博客 427

PAT甲级1044(在火星购物)

1044 Shopping in Mars (25 point(s))   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,...

云胡的博客 318

PAT甲级刷题记录——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 ...

一只奋斗中的本科生 448
上一篇: PAT甲级1041,1042解题报告
下一篇: 树状数组总结
Tate_Brown
博客等级 码龄9年 39粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址