2021牛客寒假算法基础集训营3

2021牛客寒假算法基础集训营3

I 序列的美观度

贪心/动态规划

#include <bits/stdc++.h>

#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
const int N = 8e7 + 10000;

int a[maxn];
int dp[maxn];

void solve() {
    int n;
    cin>>n;
    for (int i = 1; i <=n; ++i) {
        int x;
        cin>>x;
        dp[i]=max(dp[i],dp[i-1]);
        if (a[x]) dp[i]=max(dp[i],dp[a[x]]+1);
        a[x]=i;
    }
    cout<<dp[n];
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}

C 重力坠击

dfs/暴力枚举

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e2 + 10;
const int N = 1e7 + 10000;

struct p{
    int x,y,r;
};
p a[maxn],pos[maxn];
int n,k,r;
int ans=0;

int judge(int i,int j){
    return (a[i].x-pos[j].x)*(a[i].x-pos[j].x)+(a[i].y-pos[j].y)*(a[i].y-pos[j].y)<=(r+a[i].r)*(r+a[i].r);
}

int calc(){
    int cnt=0;
    for (int i = 1; i <=n; ++i) {
        for (int j = 1; j <=k; ++j) {
            if (judge(i,j)){
                cnt++;
                break;
            }
        }
    }
    return cnt;
}

void dfs(int dep){
    if (dep>k){
        ans=max(ans,calc());
        return;
    }
    for (int i = -7; i <=7; ++i) {
        for (int j = -7; j <=7; ++j) {
            pos[dep].x=i;
            pos[dep].y=j;
            dfs(dep+1);
        }
    }
}

void solve() {
    cin>>n>>k>>r;
    for (int i = 1; i <=n; ++i) {
        cin>>a[i].x>>a[i].y>>a[i].r;
    }
    dfs(1);
    cout<<ans;
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}



E 买礼物

数据结构/线段树

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 5e5 + 10;
const int N = 1e7 + 10000;

int a[maxn];
int last[maxn];
int nxt[maxn];
vector<int> vec[maxn*2];
struct Node{
    int l,r,val;
    int mid(){
        return (l+r)/2;
    }
}node[maxn<<2];

void pushup(int rt){
    node[rt].val=min(node[rt<<1].val,node[rt<<1|1].val);
}
void build(int rt,int l,int r){
    node[rt].l=l;
    node[rt].r=r;
    if (l==r){
        node[rt].val=nxt[l];
        return;
    }
    int m=node[rt].mid();
    build(rt<<1,l,m);
    build(rt<<1|1,m+1,r);
    pushup(rt);
}
void update(int rt,int l,int val){
    if (node[rt].r==node[rt].l){
        node[rt].val=val;
        return;
    }
    int m=node[rt].mid();
    if (l<=m) update(rt<<1,l,val);
    else update(rt<<1|1,l,val);
    pushup(rt);
}
int query(int rt,int l,int r){
    if (node[rt].l==l&&node[rt].r==r){
        return node[rt].val;
    }
    int m=node[rt].mid();
    if (m>=r) return query(rt<<1,l,r);
    else if (m<l) return query(rt<<1|1,l,r);
    return min(query(rt<<1,l,m),query(rt<<1|1,m+1,r));
}

void solve() {
    int n, q;
    cin >> n >> q;
    for (int i = 1; i <=n; ++i) cin >> a[i];
    for (int i = 1; i <=1000000; ++i) vec[i].push_back(0);
    for (int i = 1; i <=n; ++i) vec[a[i]].push_back(i);
    for (int i = 1; i <=1000000; ++i) vec[i].push_back(inf);
    for (int i = 1; i <=1000000; ++i) {
        for (int j = 1; j < vec[i].size()-1; ++j) {
            last[vec[i][j]]=vec[i][j-1];
            nxt[vec[i][j]]=vec[i][j+1];
        }
    }
    build(1,1,n);
    int type, x, l,r;
    while(q--){
        cin>>type;
        if (type==1){
            cin>>x;
            update(1,x,inf);
            update(1,last[x],nxt[x]);
            nxt[last[x]]=nxt[x];
            if (nxt[x]<inf) last[nxt[x]]=last[x];
        }else{
            cin>>l>>r;
            if (query(1,l,r)<=r) cout<<1;
            else cout<<0;
            cout<<"\n";
        }
    }
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}



B 内卷

优先队列/尺取法

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 1e7 + 10000;

struct node{
    int id,i,val;
    bool operator < (const node &b) const {
        return val>b.val;
    }
};
int a[maxn][10];
priority_queue<node>q;

void solve() {
    int n,k,ans,ma=0;
    cin>>n>>k;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 5; ++j)cin>>a[i][j];
            q.push({i,4,a[i][4]});
            ma=max(ma,a[i][4]);
    }
    ans=ma-q.top().val;
    while (1){
        node temp=q.top();
        q.pop();
        if (temp.i==0) break;
        q.push({temp.id, temp.i- 1, a[temp.id][temp.i - 1]});
        if(temp.i==1)k--;
        if (k<0)break;
        ma=max(ma,a[temp.id][temp.i-1]);
        ans=min(ans,ma-q.top().val);
    }
    cout<<ans<<"\n";
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
//    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}



A 模数的世界

#include <bits/stdc++.h>

//#pragma GCC optimize(2)
#define int long long
using namespace std;
//const int mod = 998244353;
typedef long long LL;
const int inf = 1e18;
const int mod = 1e9 + 7;
const int maxn = 1e5 + 10;
const int N = 1e7 + 10000;


int exgcd(int a, int b, int &x, int &y) {
    int ans = a;
    if (b == 0) {
        x = 1;
        y = 0;
    } else {
        ans = exgcd(b, a % b, y, x);
        y -= (a / b) * x;
    }
    return ans;
}


void solve() {
    int a, b, p, ra, rb, k1, k2, x, y;
    int is;
    cin >> a >> b >> p;
    if (a == b && b == 0) {
        cout << "0 " << p << " " << p;
    } else {
        if (a == 0 || b == 0) {
            ra = (p - a) * (p - 1);
            rb = (p - b) * (p - 1);
        } else {
            is = 0;
            if (a < b)swap(a, b), is = 1;
            k1 = (p - a), k2 = (p - b);
            exgcd(p, k2, x, y);
            if (x < 0) x = (x % k2 + k2) % k2;
            ra = ((k2 + 1 - k1) * x * p + k1) * (p - 1);
            rb = k2 * (p - 1);
            if (is)swap(ra, rb);
        }
        cout << p - 1 << " " << ra << " " << rb;
    }
    cout << "\n";
}

signed main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--) {
        solve();
    }
    return 0;
}



在使用Python来安装geopandas包时,由于geopandas依赖于几个其他的Python库(如GDAL, Fiona, Pyproj, Shapely等),因此安装过程可能需要一些额外的步骤。以下是一个基本的安装指南,适用于大多数用户: 使用pip安装 确保Python和pip已安装: 首先,确保你的计算机上已安装了Python和pip。pip是Python的包管理工具,用于安装和管理Python包。 安装依赖库: 由于geopandas依赖于GDAL, Fiona, Pyproj, Shapely等库,你可能需要先安装这些库。通常,你可以通过pip直接安装这些库,但有时候可能需要从其他源下载预编译的二进制包(wheel文件),特别是GDAL和Fiona,因为它们可能包含一些系统级的依赖。 bash pip install GDAL Fiona Pyproj Shapely 注意:在某些系统上,直接使用pip安装GDAL和Fiona可能会遇到问题,因为它们需要编译一些C/C++代码。如果遇到问题,你可以考虑使用conda(一个Python包、依赖和环境管理器)来安装这些库,或者从Unofficial Windows Binaries for Python Extension Packages这样的网站下载预编译的wheel文件。 安装geopandas: 在安装了所有依赖库之后,你可以使用pip来安装geopandas。 bash pip install geopandas 使用conda安装 如果你正在使用conda作为你的Python包管理器,那么安装geopandas和它的依赖可能会更简单一些。 创建一个新的conda环境(可选,但推荐): bash conda create -n geoenv python=3.x anaconda conda activate geoenv 其中3.x是你希望使用的Python版本。 安装geopandas: 使用conda-forge频道来安装geopandas,因为它提供了许多地理空间相关的包。 bash conda install -c conda-forge geopandas 这条命令会自动安装geopandas及其所有依赖。 注意事项 如果你在安装过程中遇到任何问题,比如编译错误或依赖问题,请检查你的Python版本和pip/conda的版本是否是最新的,或者尝试在不同的环境中安装。 某些库(如GDAL)可能需要额外的系统级依赖,如地理空间库(如PROJ和GEOS)。这些依赖可能需要单独安装,具体取决于你的操作系统。 如果你在Windows上遇到问题,并且pip安装失败,尝试从Unofficial Windows Binaries for Python Extension Packages网站下载相应的wheel文件,并使用pip进行安装。 脚本示例 虽然你的问题主要是关于如何安装geopandas,但如果你想要一个Python脚本来重命名文件夹下的文件,在原始名字前面加上字符串"geopandas",以下是一个简单的示例: python import os # 指定文件夹路径 folder_path = 'path/to/your/folder' # 遍历文件夹中的文件 for filename in os.listdir(folder_path): # 构造原始文件路径 old_file_path = os.path.join(folder_path, filename) # 构造新文件名 new_filename = 'geopandas_' + filename # 构造新文件路径 new_file_path = os.path.join(folder_path, new_filename) # 重命名文件 os.rename(old_file_path, new_file_path) print(f'Renamed "{filename}" to "{new_filename}"') 请确保将'path/to/your/folder'替换为你想要重命名文件的实际文件夹路径。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值