【PAT甲级】1048 Find Coins (25 分)(二分)
题意:
输入两个正整数N和M(N<=10000,M<=1000),然后输入N个正整数(<=500),输出两个数字和恰好等于M的两个数(小的数字尽可能小且输出在前),如果没有输出"No Solution"。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int a[];
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,k;
cin>>n>>k;
for(int i=;i<=n;++i)
cin>>a[i];
sort(a+,a++n);
int ans=;
for(int i=;i<=n;++i){
int l=,r=n;
while(l<=r){
int mid=(l+r)>>;
if(mid!=i){
if(a[i]+a[mid]<k)
l=mid+;
if(a[i]+a[mid]>k)
r=mid-;
if(a[i]+a[mid]==k){
ans=a[mid];
break;
}
}
if(mid==i){
if(a[i]+a[mid-]<k)
l=mid+;
if(a[i]+a[mid-]>k)
r=mid-;
if(a[i]+a[mid-]==k){
ans=a[mid-];
break;
}
}
}
if(ans){
cout<<a[i]<<" "<<ans;
return ;
}
}
cout<<"No Solution";
return ;
}
【PAT甲级】1048 Find Coins (25 分)(二分)的更多相关文章
- PAT 甲级 1048 Find Coins (25 分)(较简单,开个数组记录一下即可)
1048 Find Coins (25 分) Eva loves to collect coins from all over the universe, including some other ...
- PAT Advanced 1048 Find Coins (25 分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...
- PAT甲 1048. Find Coins (25) 2016-09-09 23:15 29人阅读 评论(0) 收藏
1048. Find Coins (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Eva loves t ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)
1146 Topological Order (25 分) This is a problem given in the Graduate Entrance Exam in 2018: Which ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- PAT 甲级 1063 Set Similarity (25 分) (新学,set的使用,printf 输出%,要%%)
1063 Set Similarity (25 分) Given two sets of integers, the similarity of the sets is defined to be ...
- PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)
1059 Prime Factors (25 分) Given any positive integer N, you are supposed to find all of its prime ...
- PAT 甲级 1051 Pop Sequence (25 分)(模拟栈,较简单)
1051 Pop Sequence (25 分) Given a stack which can keep M numbers at most. Push N numbers in the ord ...
- PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)
1037 Magic Coupon (25 分) The magic shop in Mars is offering some magic coupons. Each coupon has an ...
随机推荐
- Codeforces Round #567 (Div. 2) A.Chunga-Changa
原文链接:传送 #include"algorithm" #include"iostream" #include"cmath" using n ...
- springboot06(静态资源映射)
xxxxAutoConfiguration xxxxproperties 对静态资源的映射规则 webjars @ConfigurationProperties(prefix = "spri ...
- 集合的操作 contains(),containsAll() ,addAll(),removeAll(),
package seday11; import java.util.ArrayList;import java.util.Collection;import java.util.HashSet;/** ...
- VS Code 配置 C/C++ (Windwos)
下载VSCode https://code.visualstudio.com/Download 在扩展里安装C/C++插件 配置MinGW 需要再环境变量中的path中添加MinGW的bin目录 修改 ...
- python开发基础04-列表、元组、字典操作练习
练习1: # l1 = [11,22,33]# l2 = [22,33,44]# a. 获取内容相同的元素列表# b. 获取 l1 中有, l2 中没有的元素列表# c. 获取 l2 中有, l1 中 ...
- angular清空node_modules
安装全局包 npm install rimraf -g 执行清空命令 rimraf node_modules
- Bugku-CTF之sql注入2 (全都tm过滤了绝望吗?)
Day 38 sql注入2 200 http://123.206.87.240:8007/web2/ 全都tm过滤了绝望吗? 提示 !,!=,=,+,-,^,%
- 路飞-后台Django项目创建
后台:Django项目创建 环境 """ 为luffy项目创建一个虚拟环境 >: mkvirtualenv luffy """ &qu ...
- 【游戏体验】Haunted House(鬼屋历险记)
>>>点此处可试玩无敌版<<< 这款游戏可谓是经典,很多人的童年都有过它的陪伴,不妨重拾当年的快乐 个人测评 游戏性 9/10 音乐 7/10 剧情 4/10 总评 ...
- CSS学习(2)Id和Class选择器
id 选择器 id 选择器可以为标有特定 id 的 HTML 元素指定特定的样式. HTML元素以id属性来设置id选择器,CSS 中 id 选择器以 "#" 来定义. 以下的样式 ...