*4.5 PAT B1030/A1085
B1030
给定一个正整数数列,和正整数 p,设这个数列中的最大值是 M,最小值是 m,如果 M≤m**p,则称这个数列是完美数列。
现在给定参数 p 和一些正整数,请你从中选择尽可能多的数构成一个完美数列。
输入格式:
输入第一行给出两个正整数 N 和 p,其中 N(≤105)是输入的正整数的个数,p(≤109)是给定的参数。第二行给出 N 个正整数,每个数不超过 109。
输出格式:
在一行中输出最多可以选择多少个数可以用它们组成一个完美数列。
输入样例:
10 8
2 3 20 4 5 1 6 7 8 9
输出样例:
8
A1085
Given a sequence of positive integers and another positive integer p. The sequence is said to be a perfect sequence if M≤m×p where M and m are the maximum and minimum numbers in the sequence, respectively.
Now given a sequence and a parameter p, you are supposed to find from the sequence as many numbers as possible to form a perfect subsequence.
Input Specification:
Each input file contains one test case. For each case, the first line contains two positive integers N and p, where N (≤105) is the number of integers in the sequence, and p (≤109) is the parameter. In the second line there are N positive integers, each is no greater than 109.
Output Specification:
For each test case, print in one line the maximum number of integers that can be chosen to form a perfect subsequence.
Sample Input:
10 8
2 3 20 4 5 1 6 7 8 9
Sample Output:
8
//二分查找法
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, p, a[100010];
int binarySearch(int i, long long x) {
if (a[n - 1] <= x)
return n;
int l = i + 1, r = n - 1, mid;
while (l < r) {
mid = (l + r) / 2;
if (a[mid] <= x) {
l = mid + 1;
}
else {
r = mid;
}
}
return 1;
}
int main() {
scanf("%d%d", &n, &p);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int ans = 1;
for (int i = 0; i < n; i++) {
int j = binarySearch(i, (long long)a[i] * p);
ans = max(ans, j - i);
}
printf("%d\n", ans);
return 0;
}
//upper_bound法
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, p, a[100010];
int main() {
scanf("%d%d", &n, &p);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int ans = 1;
for (int i = 0; i < n; i++) {
int j = upper_bound(a + i + 1, a + n, (long long)a[i] * p) - a;
ans = max(ans, j - 1);
}
printf("%d\n", ans);
return 0;
}
? *4.5 PAT A1010
Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is yes
, if 6 is a decimal number and 110 is a binary number.
Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.
Input Specification:
Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1
and N2
each has no more than 10 digits. A digit is less than its radix and is chosen from the set { 0-9, a
-z
} where 0-9 represent the decimal numbers 0-9, and a
-z
represent the decimal numbers 10-35. The last number radix
is the radix of N1
if tag
is 1, or of N2
if tag
is 2.
Output Specification:
For each test case, print in one line the radix of the other number so that the equation N1
= N2
is true. If the equation is impossible, print Impossible
. If the solution is not unique, output the smallest possible radix.
Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
LL Map[256];
LL inf = (1ll << 63) - 1;
void init() {
for (char c = '0'; c <= '9'; c++) {
Map[c] = c - '0';
}
for (char c = 'a'; c <= 'z'; c++) {
Map[c] = c - 'a' + 10;
}
}
LL convertNum10(char a[], LL radix, LL t) {
LL ans = 0;
int len = strlen(a);
for (int i = 0; i < len; i++) {
ans = ans * radix + Map[a[i]];
if (ans<0 || ans>t)
return -1;
}
return ans;
}
int cmp(char n2[], LL radix, LL t) {
int len = strlen(n2);
LL num = convertNum10(n2, radix, t);
if (num < 0)
return 1;
if (t > num)
return -1;
else if (t == num)
return 0;
else
return 1;
}
LL binarySearch(char n2[], LL left, LL right, LL t) {
LL mid;
while (left <= right) {
mid = (left + right) / 2;
int flag = cmp(n2, mid, t);
if (flag == 0)
return mid;
else if (flag == -1)
left = right + 1;
else
right = mid - 1;
}
return -1;
}
int findLargestDigit(char n2[]) {
int ans = -1, len = strlen(n2);
for (int i = 0; i < len; i++) {
if (Map[n2[i]] > ans) {
ans = Map[n2[i]];
}
}
return ans + 1;
}
char n1[20], n2[20], temp[20];
int tag, radix;
int main() {
init();
scanf("%s %s %d %d", n1, n2, &tag, &radix);
if (tag == 2) {
strcpy(temp, n1);
strcpy(n1, n2);
strcpy(n2, temp);
}
LL t = convertNum10(n1, radix, inf);
LL low = findLargestDigit(n2);
LL high = max(low, t) + 1;
LL ans = binarySearch(n2, low, high, t);
if (ans == -1)
printf("Impossible\n");
else
printf("%lld\n", ans);
return 0;
}
? *4.5 PAT A1044
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diamonds are taken off the chain one by one. Once a diamond is off the chain, it cannot be taken back. For example, if we have a chain of 8 diamonds with values M$3, 2, 1, 5, 4, 6, 8, 7, and we must pay M$15. We may have 3 options:
- Cut the chain between 4 and 6, and take off the diamonds from the position 1 to 5 (with values 3+2+1+5+4=15).
- Cut before 5 or after 6, and take off the diamonds from the position 4 to 6 (with values 5+4+6=15).
- Cut before 8, and take off the diamonds from the position 7 to 8 (with values 8+7=15).
Now given the chain of diamond values and the amount that a customer has to pay, you are supposed to list all the paying options for the customer.
If it is impossible to pay the exact amount, you must suggest solutions with minimum lost.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 numbers: N (≤105), the total number of diamonds on the chain, and M (≤108), the amount that the customer has to pay. Then the next line contains N positive numbers D1⋯D**N (D**i≤103 for all i=1,⋯,N) which are the values of the diamonds. All the numbers in a line are separated by a space.
Output Specification:
For each test case, print i-j
in a line for each pair of i
≤ j
such that Di
+ … + Dj
= M. Note that if there are more than one solution, all the solutions must be printed in increasing order of i
.
If there is no solution, output i-j
for pairs of i
≤ j
such that Di
+ … + Dj
>M with (Di
+ … + Dj
−M) minimized. Again all the solutions must be printed in increasing order of i
.
It is guaranteed that the total value of diamonds is sufficient to pay the given amount.
Sample Input 1:
16 15
3 2 1 5 4 6 8 7 16 10 15 11 9 12 14 13
Sample Output 1:
1-5
4-6
7-8
11-11
Sample Input 2:
5 13
2 4 5 7 9
Sample Output 2:
2-4
4-5
//#include<stdio.h>
//
//int main() {
// int chain[100010];
// int n, m;
// scanf("%d %d", &n, &m);
// for (int i = 0; i < n; i++) {
// scanf("%d", &chain[i]);
// }
// for (int i = 0; i < n; i++) {
// printf("%d ", chain[i]);
// }
// int i = 0, j;
// while (i < n) {
// int sum = chain[i];
// j = i + 1;
// while (j < n && sum <= m) {
// sum += chain[j];
// j++;
// }
// if (sum == m) {
// printf("%d-%d\n", i + 1, j - 1);
// }
// else {
// printf("%d-%d\n", i + 1, j);
// }
// i++;
// }
// return 0;
//}
//算法笔记
#include<stdio.h>
int sum[100010];
int n, S, nearS = 100000010;
int upper_bound(int L, int R, int x) {
int left = L, right = R, mid;
while (left < right) {
mid = (left + right) / 2;
if (sum[mid] > x) {
right = mid;
}
else {
left = mid + 1;
}
}
return left;
}
int main() {
scanf("%d%d", &n, &S);
sum[0] = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &sum[i]);
sum[i] += sum[i - 1];
}
for (int i = 1; i <= n; i++) {
int j = upper_bound(i, n + 1, sum[i - 1] + S);
if (sum[j - 1] - sum[i - 1] == S) {
nearS = S;
break;
}
else if (j >= n && sum[j] - sum[i - 1] < nearS) {
nearS = sum[j] - sum[i - 1];
}
}
for (int i = 1; i <= n; i++) {
int j = upper_bound(i, n + 1, sum[i - 1] + nearS);
if (sum[j - 1] - sum[i - 1] == nearS) {
printf("%d-%d\n", i, j - 1);
}
}
return 0;
}