PAT 甲级 A1044 (2019/02/20)

#include<cstdio>
const int MAXN = 100010;
int sum[MAXN];
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(){
    //freopen("input.txt","r",stdin);
    scanf("%d %d", &n, &S);
    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;
} 

原文地址:https://www.cnblogs.com/zjsaipplp/p/10425229.html

时间: 2024-11-09 04:16:01

PAT 甲级 A1044 (2019/02/20)的相关文章

PAT 甲级 A1010 (2019/02/20)

#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL Map[256]; // 0 ~ 9, a ~ z 与 0 ~ 35 的对应 LL Y = 1; // 定义LL型的Y LL inf = (1LL << 63) - Y; // long long 的最大值 2^63 - 1,注意括号 void init

PAT 甲级 A1037 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; //段错误,数组开辟的太小 int a[MAXN], b[MAXN]; int main(){ int n1, n2; scanf("%d", &n1); for(int i = 0; i < n1; i++){ scanf("%d", &a[i]); }

PAT 甲级 A1067 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int a[MAXN]; //存放各个数字当前所处的位置编号 int main(){ int n, num, ans = 0;//表示总计交换次数 scanf("%d", &n); int surplus = n - 1;//存放除0以外不在本位上的数的个数 for(int i = 0; i

PAT 甲级 A1085 (2019/02/20)

#include<cstdio> #include<algorithm> using namespace std; const int MAXN = 100010; int n, p, a[MAXN]; int binarySearch(int i, long long x){ if(a[n - 1] <= x) //如果最大的数比x小,则返回n return n; int left = i + 1; int right = n - 1; //在区间[i+1, n-1]内查找

PAT甲级【2019年3月考题】——A1158 TelefraudDetection【25】

Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone c

PAT甲级——A1044 Shopping in Mars

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 o

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime. Now given

PAT 甲级 1041 Be Unique (20 分)(简单,一遍过)

1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is designed in a unique way. The rule of winning is simple: one bets on a number chosen from [1]. The first one who bets on a unique number wins. For example

PAT 甲级 1108 Finding Average (20分)

1108 Finding Average (20分) The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A legal input is a real number in [−] and is a