算法笔记 上机训练实战指南 第4章 入门篇(2) --算法初步 4.6two pointers 学习笔记
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤mp,则称这个数列是完美数列。
现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列。
输入格式:
输入第一行给出两个正整数 N 和 p,其中 N(≤)是输入的正整数的个数,p(≤)是给定的参数。第二行给出 N 个正整数,每个数不超过 1。
输出格式:
在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
输入样例:
10 8
2 3 20 4 5 1 6 7 8 9
输出样例:
8
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 100010; int main(){ int n,p; int a[maxn]; scanf("%d%d",&n,&p); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); int i=0,j=0,count=1; while(i<n && j<n){ while(j<n && a[j]<= (long long)a[i]*p){ count = max(count,j-i+1); j++; } i++; } printf("%d",count); return 0; }
Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequences is defined to be the median of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.
Given two increasing sequences of integers, you are asked to find their median.
Input Specification:
Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (≤) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed that all the integers are in the range of long int.
Output Specification:
For each test case you should output the median of the two given sequences in a line.
Sample Input:
4 11 12 13 14
5 9 10 15 16 17
Sample Output:
13
#include<cstdio> #include<algorithm> using namespace std; const int maxn = 1000010; int a[maxn],b[maxn],c[maxn]; int merge(int A[],int B[],int C[],int n,int m){ int i =0,j=0,index = 0; while(i < n && j<m){ if(A[i]<=B[j]){ C[index++] = A[i++]; }else{ C[index++] = B[j++]; } } while(i < n) C[index++] = A[i++]; while(j < m) C[index++] = B[j++]; return index; } int main(){ int n1,n2; scanf("%d",&n1); for(int i=0;i<n1;i++){ scanf("%d",&a[i]); } scanf("%d",&n2); for(int i=0;i<n2;i++){ scanf("%d",&b[i]); } int len = merge(a,b,c,n1,n2); sort(c,c+len); printf("%d\n",c[(n1+n2-1)/2]); return 0; }
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<cstdio> #include<algorithm> using namespace std; const int maxn = 100010; int a[maxn]; void twoPointers(int n,int m){ int i=0,j=n-1; while(i<j){ if(a[i]+a[j]==m) break; else if(a[i] + a[j] > m){ j--; }else{ i++; } } if(i <j){ printf("%d %d\n",a[i],a[j]); }else{ printf("No Solution\n"); } } int main(){ int n,m; scanf("%d%d",&n,&m); for(int i=0;i<n;i++){ scanf("%d",&a[i]); } sort(a,a+n); twoPointers(n,m); return 0; }