PAT甲级——A1048 Find Coins
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she could only use exactly two coins to pay the exact amount. Since she has as many as 1 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find two coins to pay for it.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (≤, the total number of coins) and M (≤, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers no more than 500. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print in one line the two face values V1 and V2 (separated by a space) such that V1+V2=M and V1≤V2. If such a solution is not unique, output the one with the smallest V1. If there is no solution, output No Solution
instead.
Sample Input 1:
8 15
1 2 8 7 2 4 11 15
Sample Output 1:
4 11
Sample Input 2:
7 14
1 8 7 2 4 11 15
Sample Output 2:
No Solution
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
using namespace std;
int N, M;
//放法一,使用窗口函数
int main()
{
cin >> N >> M;
vector<int>nums(N);
vector<pair<int, int>>res;
for (int i = ; i < N; ++i)
cin >> nums[i];
sort(nums.begin(), nums.end(), [](int a, int b) {return a < b; });
int l = , r = N - , m;
while (l < r)//找到中间的数刚好与M/2最接近
{
m = (l + r) / ;
if (nums[m] <= M / && nums[m + ] > M / )
break;
if (nums[m] > M / )
r = m - ;
else
l = m + ;
}
r = m + ;
l = m;
while(l>= && r<N)//然后使用窗口函数,进行左右移动
{
if (nums[l] + nums[r] == M)
{
res.push_back(make_pair(nums[l], nums[r]));
r++;
}
else if (nums[l] + nums[r] < M)
r++;
else
l--;
}
sort(res.begin(), res.end(), [](pair<int, int> a, pair<int, int> b) {return a.first < b.first; });
if (res.size() == )
cout << "No Solution" << endl;
else
cout << res[].first << " " << res[].second << endl;
return ;
} //方法二,使用数找数原理
int main()
{
cin >> N >> M;
int nums[], t;//根据题目要新建的数组大小
memset(nums, , sizeof(int) * );
for (int i = ; i < N; ++i)
{
cin >> t;
nums[t]++;//统计个数
}
for (int i = ; i < ; ++i)
{
if (nums[i] > )//个数大于0
{
nums[i]--;//使用掉一个数字
if (M > i && nums[M - i] > )
{
cout << i << " " << M - i << endl;//由最小值开始遍历,故为最优答案
return ;
}
nums[i]++;//没有使用,还回去
}
}
cout << "No Solution" << endl;
return ;
}
PAT甲级——A1048 Find Coins的更多相关文章
- PAT 甲级 1048 Find Coins
https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级题分类汇编——杂项
本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
随机推荐
- MR/hive/shark/sparkSQL
shark完全兼容hive,完全兼容MR,它把它们替代.类SQL查询,性能比hive高很多 sparkSQL比shark更快.shark严重依赖hive,hive慢,无法优化. SparkSQL和sh ...
- spring 中常用的设计模式
一. Spring 中常见的设计模式 工厂模式 : BeanFactory 装饰器模式: BeanWrapper 代理模式: AopProxy 单例模式: ApplicationContext 委派模 ...
- day 67 Django基础三之视图函数
Django基础三之视图函数 本节目录 一 Django的视图函数view 二 CBV和FBV 三 使用Mixin 四 给视图加装饰器 五 Request对象 六 Response对象 一 Dja ...
- windows安装vscode,配置golang环境
出现的问题: 进行如下命令进行目录切换:cd %GOPATH%\src\github.com\golang我这里的GOPATH是在D:\GoPath,大家这里一定要注意些如果src目录下面没有gith ...
- openstack各组件介绍
Nova:计算服务,通过虚拟化技术,实现虚拟机的创建,管理,删除,是openstack中最核心的服务. Neutron:网络服务,为虚拟机提供网络连接服务,就像物理机的交换机和路由器一样 Glance ...
- uploadify上传附件 点击保存无效 切F12就可以正常保存
感谢 这篇文章 https://blog.csdn.net/koala25/article/details/70230046 uploadify上传附件 点击保存无效 切F12就可以正常保存了, ...
- 第十三篇:一点一滴学ibatis(二)映射文件
首先给出一个常见的映射文件局部模板 <?xml version="1.0" encoding="utf-8" ?><!DOCTYPE s ...
- 收藏的链接-Qt
Qt编写的开源帖子集合(懒人专用) - QTCN开发网 - Powered by phpwind http://www.qtcn.org/bbs/read-htm-tid-85501.html?tds ...
- seienium基础(测试脚本中的等待方法)
测试脚本中的等待方法 一.加等待时间的目的 等待是为了使脚本执行更加稳定 二.常用的休眠方式 第一种 sleep(): 设置固定休眠时间.python 的 time 包提供了休眠方法 sleep() ...
- Java后端WebSocket的Tomcat实现(转)
文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSocket协议被提出,它实现了浏览器与服务器的全双工通信,扩展了浏览器与服务端 ...