pat 1044三个点超时

#include<iostream>

using std::cin;
using std::cout;
using std::endl;
int a[100000][100000];
int main(){
    freopen("1.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    //cin >> n >> m;
    //int (*a)[100000] = new int[n][100000];
    int *begin = new int[n];
    int *end = new int[n];
    int i, j,min = 32767,value,pt = 0;
    for (i = 0; i < n; i++){
        scanf("%d", &value);
            a[i][i] = value;
        for (j = 0; j <=i; j++){
            if (i!=j&&i)
                a[j][i] = value + a[j][i - 1];
            if (a[j][i] >=m&&a[j][i] - m < min){
                min = a[j][i] - m;
                begin[0] = j;
                end[0] = i;
                pt = 1;
            }
            else if (a[j][i]>=m && a[j][i] - m == min){
                begin[pt] = j;
                end[pt] = i;
                pt++;
            }
        }
    }
    for (i = 0; i < pt; i++)
        printf("%d-%d\n", begin[i] + 1, end[i] + 1);
    return 0;
}
#include<iostream>

using std::cin;
using std::cout;
using std::endl;

int main(){
    freopen("1.in", "r", stdin);
    int n, m;
    scanf("%d%d", &n, &m);
    //cin >> n >> m;
    int *a= new int[n];
    int *begin = new int[n];
    int *end = new int[n];
    int pt = 0;
    int i, j,min = 37267;
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);
        //cin >> a[i];
    int sum = 0;
    for (i = 0; i < n; i++){
        sum = 0;
        for (j = i; j < n; j++)
        {
            sum += a[j];
            if (sum == m){
                printf("%d-%d\n", i + 1, j + 1);
                //cout << i+1 << "-" << j+1 << endl;
                min = 0;
                break;
            }
            else if (sum - m>0 && sum - m < min){
                begin[0] = i;
                end[0] = j;
                min = sum - m;
                pt = 1;
            }
            else if (sum - m>0 && sum - m == min){
                begin[pt] = i;
                end[pt] = j;
                pt++;
            }
        }
    }
    if (min){
        for (i = 0; i < pt; i++)
            printf("%d-%d\n", begin[i] + 1, end[i] + 1);
            //cout << begin[i] + 1 << "-" << end[i] + 1 << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值