立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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 10​5​​ 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 (≤10​5​​, the total number of coins) and M (≤10​3​​, 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


题意:输入n,value, 接下来输入n个数, 要求用其中的两个数相加等于value。 如果有多组,则输出最小的。

算法设计:有双指针法和桶排法两种方法。 双指针法的时间复杂度是O(nlogn)。 桶排法的时间复杂度为O(n), 算法逻辑请阅读代码体会。


代码一:双指针法

#include<bits/stdc++.h>
using namespace std;
int ary[100010];
int main() {ios::sync_with_stdio(false);int num, value; cin >> num >> value;for(int i = 0; i < num; i++) cin >> ary[i];sort(ary, ary+num);int sta = 0, fin = num-1;while(sta != fin) {if(ary[sta] + ary[fin] > value) fin--;else if(ary[sta] + ary[fin] < value) sta++;else { cout << ary[sta] << ' ' << ary[fin] << '\n'; return 0; }}cout << "No Solution\n";return 0;
}

代码二:桶排法

#include<bits/stdc++.h>
using namespace std;
int ary[1010];
int main() {ios::sync_with_stdio(false);int num, value, x; cin >> num >> value;for(int i = 0; i < num; i++) { cin >> x; ary[x]++; }for(int i = 0; i < 1005; i++) if(ary[i]) {ary[i]--;if(ary[value-i]) { cout << i << ' ' << value-i; return 0;}}cout << "No Solution";return 0;
}

耗时(桶排):


求赞哦~ (✪ω✪)

【最简解法】1048 Find Coins (25 分)_18行代码AC相关推荐

  1. 案例4-1.6 树种统计 (25 分)_18行代码AC

    立志用最少的代码做最高效的表达 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(≤ ...

  2. 【最详细解析】1070 结绳 (25分)_18行代码AC

    立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的 ...

  3. 【简便解法】1090 危险品装箱 (25分)_33行代码AC

    立志用最少的代码做最高效的表达 PAT乙级最优题解-->传送门 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题 ...

  4. 【简便解法】1035 插入与归并 (25分)_37行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优题解-->传送门 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插 ...

  5. 【高效解法】1065 单身狗 (25分)_27行代码AC

    立志用更少的代码做更高效的表达 "单身狗"是中文对于单身人士的一种爱称.本题请你从上万人的大型派对中找出落单的客人,以便给予特殊关爱. 输入格式: 输入第一行给出一个正整数 N(≤ ...

  6. 【附超时原因】1055 The World‘s Richest (25 分)_42行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Forbes magazine publishes every year its list of billionaires bas ...

  7. 【测试点三、四、五分析】1032 Sharing (25 分)_28行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 To store English words, one method is to use linked lists and sto ...

  8. 【解析】1013 Battle Over Cities (25 分)_31行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 It is vitally important to have all the cities connected by highw ...

  9. 【详细解析】1080 MOOC期终成绩 (25分)_45行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优题解-->传送门 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想 ...

最新文章

  1. matlab的帮助命令是英文的,4 Matlab 帮助系统
  2. 面试必备:30 个 Java 集合面试问题及答案
  3. 解决Sqlite中的中文路径问题
  4. ABAP 引用类型介绍
  5. 使用pm2启动Node和Vue项目教程
  6. [CodeJam 2019 Round 3] Rancake Pyramid(笛卡尔树)
  7. Python:知识目录
  8. Google真相:决策贡献及其艰苦抉择
  9. MyApps接口引擎,打破跨系统间的壁垒
  10. win10系统WiFi突然消失打不开,终极解决方法
  11. Ubuntu 安装微信,网页版无法登陆
  12. LR录制https协议报证书错误,导航已阻止
  13. 基于电力行业信息安全基线的量化管理系统研究与应用
  14. OPPO R7Plusm(全网通)root、刷入twrp recovery、卡刷刷入CM系统教程合集_ recovery.img文件下载 联想A7600-m线刷刷机教程 手机卡在双4G双百兆无法开
  15. java coap_分布式项目(三)CoAp client and server
  16. 裸金属服务器开通原理
  17. 28 篇论文、6 大主题带你一览 CVPR 2020 研究趋势
  18. HTML+CSS大作业——二次元漫画(8页) 漫画网页设计制作 简单静态HTML网页作品 我的漫画网页作业成品 学生漫画网站模板
  19. 计算机中汉字的顺序有什么排列,汉字演变过程的时间排序是什么?
  20. hacker入门专业术语

热门文章

  1. tomcat源码运行
  2. 如何获取Kafka的消费者详情——从Scala到Java的切换
  3. 如何有效控制 Go 线程数?
  4. 一个卑微的程序员友链
  5. 二叉树和栈的基本操作
  6. 【今晚七点半】:5G时代的云游戏还缺什么?
  7. 【线上分享】短视频出海 — 用户体验衡量关键指标与优化策略
  8. Apple 低延迟HLS分析
  9. WebSocket使用案例
  10. rabbitMQ 实战 高效部署分布式消息队列 读书笔记