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 V​1​​ and V​2​​ (separated by a space) such that V​1​​+V​2​​=M and V​1​​≤V​2​​. If such a solution is not unique, output the one with the smallest V​1​​. 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的更多相关文章

  1. PAT 甲级 1048 Find Coins

    https://pintia.cn/problem-sets/994805342720868352/problems/994805432256675840 Eva loves to collect c ...

  2. PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)

    1048 Find Coins (25 分)   Eva loves to collect coins from all over the universe, including some other ...

  3. PAT甲级题解(慢慢刷中)

    博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...

  4. 【转载】【PAT】PAT甲级题型分类整理

    最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...

  5. PAT甲级题分类汇编——杂项

    本文为PAT甲级分类汇编系列文章. 集合.散列.数学.算法,这几类的题目都比较少,放到一起讲. 题号 标题 分数 大意 类型 1063 Set Similarity 25 集合相似度 集合 1067 ...

  6. PAT甲级题分类汇编——序言

    今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...

  7. PAT甲级题解分类byZlc

    专题一  字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...

  8. PAT甲级1131. Subway Map

    PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...

  9. PAT甲级1127. ZigZagging on a Tree

    PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...

随机推荐

  1. 资源-Java:Java资源列表

    ylbtech-资源-Java:Java资源列表 1. 开发软件返回顶部 1.Eclipse https://www.eclipse.org/ 2.IntelliJ IDEA https://www. ...

  2. 云-腾讯云-实时音视频:实时音视频(TRTC)

    ylbtech-云-腾讯云-实时音视频:实时音视频(TRTC) 支持跨终端.全平台之间互通,从零开始快速搭建实时音视频通信平台 1.返回顶部 1. 腾讯实时音视频(Tencent Real-Time ...

  3. 20.multi_case05

    #coding:utf-8 # 通过gather方法 import asyncio async def a(t): print('-->', t) await asyncio.sleep(0.5 ...

  4. java_IO流(输出流)

    ** * io流: * 输入流:硬盘输入到内存 字节/字符输入流 * 输出流:内存输出到硬盘 字节/字符输入流 * 字节流:一切数据都是字节存储(二进制) * 字节输出流(OutputStream): ...

  5. Spark中使用Java编程的常用方法

    原文引自:http://blog.sina.com.cn/s/blog_628cc2b70102w9up.html 一.初始化SparkContext System.setProperty(" ...

  6. JS流程控制语句 反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足。

    反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足. while语句结构: while(判断条件) { 循环语句 } 使用w ...

  7. git sync tags with remote

    git 同步遠程標籤 在 .git/config的 [remote "origin"] 下加了 fetch = +refs/tags/*:refs/tags/* 最後就變成 [re ...

  8. python csv write 乱码

    参考 : https://www.zhihu.com/question/34201726 1.使用utf_8_sig with open('d:/file.csv', 'w', encoding='u ...

  9. lumen框架使用Elasticsearch详解

    该博文是集合几个博客踩坑得来的,百度热搜前几篇都是缺胳膊少腿的,所以结合几篇博客实现了一遍. 一.lumen使用Elasticsearch 首先需要搭建好的elasticsearch环境: http: ...

  10. 左神算法进阶班5_4设计可以变更的缓存结构(LRU)

    [题目] 设计一种缓存结构,该结构在构造时确定大小,假设大小为K,并有两个功能: set(key, value):将记录(key, value)插入该结构. get(key):返回key对应的valu ...