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. 导入数据时出现“SqlDateTime 溢出

    错误出现:导入数据时出现“SqlDateTime 溢出.必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM之间.” 出现这种问题多半是因为你插入或者更 ...

  2. HttpUrlConnection使用详解--转AAAAA

    http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpConnection.html HttpUr ...

  3. 2day:Python基础

    基础知识: 1.python文件的后缀名:.py 2 .Windows Python的执行方式:Python 解释器路径  py文件路径 例:C:\python3\python.exe d:\1.py ...

  4. MyBatis的核心API

    MyBatis核心Api 上次简单的写了一个MyBatis的简介以及编写了一个MyBatis的入门程序,但是在入门程序中出现多很多比较陌生的词,比如SqlSessionFactoryBuilder.S ...

  5. Python3升级3.6强力Django+杀手级xadmin打造在线教育平台✍✍✍

    Python3升级3.6强力Django+杀手级xadmin打造在线教育平台 教程 Xadmin安装方法: settings.py 的配置: users App 下的 adminx.py 的配置:

  6. USACO 2009 Open Treasure Cave /// DFS||并查集 oj26215

    题目大意: 输入 p,n,t :p为地点数 判断 t 能否回到源点1 接下来n行 每行输入 a b c: a能到达b和c Sample Input 13 6 76 7 82 3 410 11 128 ...

  7. 03-css选择器

    <!doctype html><!--声明文档类型 网页文档--> <html><!--根目录标签 父级--> <head><!--网 ...

  8. linux 服务 启动 关闭 列表

    ##查看服务在每个级别的运行状态 chkconfig --list httpd           0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:启用  6:关闭 bluetooth ...

  9. 2-sat——输出方案poj3683

    一篇讲的详细的博客 https://blog.csdn.net/Hawo11/article/details/74908233 缩点后为什么要建立反图? 如果是按原图处理,选择一个点之后要把所有其后续 ...

  10. mysql 一次性插入的数据量过大报错max_allowed_packet解决方法

    查询: show VARIABLES like ‘%max_allowed_packet%‘; 记录下数字(默认是一个7位) 执行语句: ; 重启服务 再查询 该数字 ,如果没变,则修改mysql的m ...