1046 Plane Spotting

1046 Plane Spotting

sicily algorithm


title

题目链接:

给出一个长度为N的非负整数序列(所有数不超过200),还有两个整数M和K,求前M个最优的长度不小于K的连续子序列。
连续子序列的优先级如何比较
平均值大的序列优于平均值小的
长度大的序列优于长度小的
结束位置靠前的序列优于结束位置靠后的

code

#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;

struct nodes
{
    /* data */
    float averNum;
    int len;
    int lastIdx;
};

// note double or float has no ==
const float inf = 1e-9;
const int N = 305;

float myAbs(const float a, const float b) {
    float diff = a - b;
    if(diff < 0) {
        diff = 0 - diff;
    }

    return diff;
}

int myMin(const int a, const int b) {
    return a < b ? a : b;
}

bool cmp(const nodes& a, const nodes& b) {
    if(a.averNum - b.averNum > inf) {
        return true;
    } else if(myAbs(a.averNum, b.averNum) < inf) {
        if(a.len > b.len) {
            return true;
        } else if(a.len == b.len) {
            if(a.lastIdx < b.lastIdx) {
                return true;
            }
        }
    }

    return false;
}

int main() {
    int T;
    cin >> T;
    for(int t = 0; t < T; t++) {
        int n, m, k;
        cin >> n >> m >> k;
        int nums[N];
        for(int i = 0; i < n; i++) {
            cin >> nums[i];
        }

        // 
        std::vector<nodes> planes;
        for(int i = 0; i < n; i++) {
            int sum = 0;
            for(int j = i; j < n; j++) {
                sum += nums[j];

                int len = j - i + 1;
                // min size
                if(len >= k) {

                    float averNum = sum / (len + 0.);

                    nodes pn;
                    pn.len = len;
                    pn.averNum = averNum;
                    pn.lastIdx = j + 1;
                    planes.push_back(pn);
                }
            }
        }

        int size = planes.size();
        sort(planes.begin(), planes.end(), cmp);

        size = myMin(size, m);
        for(int i = 0; i < size; i++) {
            if(i == 0) {
                std::cout << "Result for run " << t + 1 << ":" << std::endl;
            }

            std::cout << planes[i].lastIdx - planes[i].len + 1 << "-"                                                    << planes[i].lastIdx << std::endl;
        }
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值