分块

1、POJ 2104 K-th Number

参考:《挑战程序设计竞赛》P186

注意:

1、各种边界问题

2、块的大小要合适,比如书中的1000,块的大小为sqrt(n)会超时,因为在进行判断的时候复杂度是有差别的,分别为sqrt(n * logn和sqrt(n) * logn

3、二分的时候试着不用判断而是循环100次,竟然WA了。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 1e9;
const int maxn = 1e5 + 10;

int n, m, x, y, k, block, Left, Right;
int a[maxn], a_t[maxn], a_sorted[maxn];

int judge(int num);

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out3.txt", "w", stdout);
#endif // __AiR_H
    scanf("%d %d", &n, &m);
    block = 1000;
    int t = n / block;
    REP(i, n) { scanf("%d", &a[i]); a_t[i] = a[i]; a_sorted[i] = a[i]; }
    for (int i = 0; i < t * block; i += block) { sort(a + i, a + i + block); }
    sort(a_sorted, a_sorted + n);
    int low = -1, high = n - 1, mid;
    while (m--) {
        scanf("%d %d %d", &x, &y, &k);
        if ((x - 1) % block == 0) { Left = x - 1; }
        else { Left = ((x - 1) / block + 1) * block; }
        if (y % block == 0) { Right = y; }
        else { Right = (y / block) * block; }
        --x; --y;
        low = -1; high = n - 1;
        while (high - low > 1) {
            mid = low + (high - low) / 2;
            if (judge(a_sorted[mid])) { high = mid; }
            else { low = mid; }
        }
        printf("%d\n", a_sorted[high]);
    }
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
    return 0;
}

int judge(int num) {
    int cnt = 0;
    if (Left >= Right - 1) {
        for (int i = x; i <= y; ++i) {
            if (a_t[i] <= num) { ++cnt; }
        }
        if (cnt >= k) { return 1; }
        return 0;
    }
    for (int i = x; i < Left; ++i) {
        if (a_t[i] <= num) { ++cnt; }
    }
    for (int i = Left; i < Right; i += block) {
        cnt += upper_bound(a + i, a + i + block, num) - (a + i);
    }
    for (int i = Right; i <= y; ++i) {
        if (a_t[i] <= num) { ++cnt; }
    }
    if (cnt >= k) { return 1; }
    return 0;
}

2、SPOJ - ADALIST

借鉴了@tju-fishporridge大佬的代码,他是双端队列写的,比vector要慢好多。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <ctime>
#include <cassert>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define eps 1e-9

typedef long long ll;
typedef pair<int, int> pii;

const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
int N, Q, cmd, k, x, block;
int a[maxn];
vector<int> v[1000];

int main() {
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif // __AiR_H
    scanf("%d %d", &N, &Q);
    REP(i, N) { scanf("%d", &a[i]); }
    block = sqrt(N + Q); REP(i, N) { v[i / block].push_back(a[i]); }
    while (Q--) {
        scanf("%d %d", &cmd, &k); if (cmd == 1) { --k; }
        int cur = 0; while (k > (int)v[cur].size()) { k -= v[cur++].size(); }
        if (cmd == 1) { scanf("%d", &x); v[cur].insert(v[cur].begin() + k, x); }
        else if (cmd == 2) { v[cur].erase(v[cur].begin() + k - 1); }
        else { printf("%d\n", v[cur][k - 1]); }
    }
#ifdef __AiR_H
    printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
#endif // __AiR_H
    return 0;
}










内容概要:本文围绕无人机自主水下传感网络(UASNs)中自主水下航行器(AUV)的路径规划问题展开研究,提出采用遗传算法(Genetic Algorithm, GA)进行优化求解,并通过Matlab代码实现仿真验证。研究重点在于利用遗传算法的全局搜索能力,解决水下复杂环境中AUV的高效路径规划问题,提升数据采集效率与网络性能。文中详细阐述了问题建模、适应度函数设计、约束条件处理及算法实现流程,展示了GA在应对多目标、非线性、动态变化水下环境中的可行性与有效性。同时,文档还列举了大量相关科研方向与Matlab仿真实例,涵盖路径规划、电力系统、机器学习、通信优化等多个领域,体现出较强的技术综合性与科研指【UASNs、AUV】无人机自主水下传感网络中遗传算法的路径规划问题研究(Matlab代码实现)导价值。; 适合人群:具备一定Matlab编程基础,从事智能优化算法、路径规划、水下传感网络或相关领域研究的研究生、科研人员及工程技术人员,尤其适合正在开展无人机、AUV或智能优化应用研究的1-5年经验研究人员。; 使用场景及目标:①学习遗传算法在复杂路径规划问题中的建模与实现方法;②掌握Matlab在UASNs与AUV路径规划中的仿真技术;③借鉴多领域科研案例拓展研究思路,推动算法在实际水下探测、环境监测、军事侦察等场景的应用。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点关注遗传算法的编码方式、交叉变异策略与适应度函数设计;同时可参考文中列出的其他研究方向进行横向拓展,强化对智能优化算法在多学科交叉应用的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值