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;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值