codeforces contest1082

news/2024/7/17 11:36:21 标签: 数据结构与算法

C 维护前缀和

题意

每一个id给一个权值序列,从每个id选出数量相同的权值,对他们进行求和,使得他们的和最大

题解

注意负数对结果没有贡献,直接跳过。

当时写的比较挫,连排序都写错了!cf的编译器比较的严谨,虽然本地的编译器过了样例,但实际上是有问题的。结果就是交上去疯狂RE。

题解就直接看代码,比较的直观

代码

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+10;
int n, m, k;
int a[maxn];
struct Node{
    int id, val;
    bool operator < (const Node &b) const{
        if(id!=b.id) return id<b.id;
        else return val>b.val;
    }
}node[maxn];

int main(){
    ios::sync_with_stdio(false);
    cin>>n>>m;
    int u, v;
    for(int i=1; i<=n; i++){
        cin>>node[i].id>>node[i].val;
    }
    sort(node+1, node+1+n);
    int pre=-1;
    int c;
    int sum = 0;
    int ans = -1e9-10;
    for(int i=1; i<=n; i++){
        if(node[i].id!=pre){
            pre = node[i].id, c=0, sum = 0;
        }
        c++;
        sum +=node[i].val;
        if(sum>0) {
            a[c]+=sum;
        }
        ans = max(ans, a[c]);
    }
    if(ans == -1e9-10) cout<<0<<endl;
    else  cout<<ans<<endl;

    return 0;
}

D 构造题

题意

给n个点,每个点限制一个度数\(a_i\),你要构造一个无向图,使得每个点的度数不超过限制,并且最长链最长。

题解

度数大于等于2的点先构成一个最长链,然后度数为1的先加在两头(否则会使答案错误),之后的点在中间插就行了。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)

const int inf = 0x3f3f3f3f;
const int maxn = 500+10;

int readint()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int n, m;
int a[maxn];
vector<int> lone;
vector<int> two;

int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    int tot = 0;
    int one = 0;
    int sum = 0;
    int u, v;
    for(int i=1; i<=n; i++) {
        cin>>a[i];
        if(a[i]>1) tot += 1, two.push_back(i);
        else one+=1, lone.push_back(i);
        sum += a[i];
    }
    if(sum<2*n-2){
        cout<<"NO"<<endl;
        return 0;
    }
    {
        if(one>=2)
            cout<<"YES "<<tot+1<<endl<<n-1<<endl;
        else cout<<"YES "<<tot-1+one<<endl<<n-1<<endl;
        for(int i=0; i<two.size(); i++){
            if(i+1<two.size()){
                u = two[i], v = two[i+1];
                a[u]--, a[v]--;
                cout<<u<<" "<<v<<endl;
            }
        }
        int idx = 0;
        if(lone.size()>=2){
            u=lone[0], v=lone[1];
            cout<<u<<" "<<two[0]<<endl;
            a[two[0]]--;
            cout<<v<<" "<<two[two.size()-1]<<endl;
            a[two[two.size()-1]]--;
            idx=2;
        }
        for(int i=0; i<two.size(); i++){
            int &deg = a[two[i]];
            if(idx >= int(lone.size())) break;
            for(int j=idx; j<min(int(lone.size()), deg+idx); j++){
                cout<<lone[j]<<" "<<two[i]<<endl;
            }
            idx+=deg;
            deg = 0;
        }
    }
    return 0;
}

E Increasing Frequency--思维+贪心

题意

给一个长度为n的序列,并且确定一个数c。\(你可以任选一个区间[l, r], 对该区间+k,k可以为负数\),使得最后的n个数中,等于c的数字的个数最多。问最多有多少个这样的数?

题解

Let $ cnt(l,r,x) $ be a number of occurrences of number x in subsegment$ [l,r] $.

The given task is equivalent to choosing \([l,r]\) and value d, such that $ans=cnt(1,l−1,c)+cnt(l,r,d)+cnt(r+1,n,c) $is maximum possible. But with some transformations \(ans=cnt(1,n,c)+(cnt(l,r,d)−cnt(l,r,c))\), so we need to maximize \(cnt(l,r,d)−cnt(l,r,c)\).

代码的精髓就是求解maximize\(cnt(l, r, d)-cnt(l, r, c)\)

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)

const int inf = 0x3f3f3f3f;
const int maxn = 5e5+10;

int readint()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int cnt[maxn];
int n, c;
int mi[maxn];

int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>c;
    int temp;
    int ans = 0;
    for(int i=1; i<=n; i++){
        cin>>temp;
        mi[temp] = min(mi[temp], cnt[temp]-cnt[c]);
        cnt[temp]++;
        ans = max(ans, cnt[temp]-cnt[c]-mi[temp]);
    }
    cout<<ans+cnt[c]<<endl;

    return 0;
}

F trie+dp

G 最大密度子图

题意

给定一个无向图,给定边权和点权,选择若干的点,使得他们的边权和-点权和最大

题解

转载于:https://www.cnblogs.com/babydragon/p/10042557.html


http://www.niftyadmin.cn/n/989482.html

相关文章

[编程题]城市修建

[编程题]城市修建 有一个城市需要修建&#xff0c;给你N个民居的坐标X,Y&#xff0c;问把这么多民居全都包进城市的话&#xff0c;城市所需最小面积是多少&#xff08;注意&#xff0c;城市为平行于坐标轴的正方形&#xff09; 输入描述: 第一行为N&#xff0c;表示民居数目&a…

EnterpriseServerBase的AOP--EsbAOP实现

EsbAOP是EnterpriseServerBase类库中的轻量级AOP框架&#xff0c;它实现了AOP的主要思想&#xff0d;&#xff0d;对方法调用进行截获&#xff0c;并加入自定义的预处理、后处理。 EsbAOP与其它很多开源的AOP实现有些不同&#xff0c;其不同之处主要在于EsbAOP并没有严格的实现…

用laravel搭一个微信公众号后台

我使用的是laravel5.2, 早期版本可能不适合下面的方法。 在routes.php写下接收微信服务器post请求的路径: Route::post(wechatmp, WechatControllerresponseMsg);在App\Http\Middleware\VerifyCsrfToken里&#xff0c;将该请求路径去除CSRF TOKEN的保护&#xff0c;官网说明&a…

JSON数据从MongoDB迁移到MaxCompute最佳实践

摘要&#xff1a; 本文为您介绍如何利用DataWorks数据集成直接从MongoDB提取JSON字段到MaxCompute。 数据及账号准备首先您需要将数据上传至您的MongoDB数据库。本例中使用阿里云的云数据库 MongoDB 版&#xff0c;网络类型为VPC&#xff08;需申请公网地址&#xff0c;否则无法…

天才们为什么独身一世?

蜀道之难&#xff0c;难于上青天。如果有人问&#xff1a;有什么比蜀道还难&#xff1f;老孙会说&#xff1a;思维。从一种思维到另一种思维之间的“蜀道”&#xff0c;更加艰难&#xff01;男人和女人之间&#xff0c;就有那么一条蜀道。畅销书《男人来自火星&#xff0c;女人…

深入理解MYSQL undo redo

undo log保证事务的原子性&#xff08;回滚&#xff09; A、BeginB、记录A1到undo log中C、修改记录A3D、记录B1到undo log中E、修改记录B2F、写入undo log到磁盘中G、写入数据到磁盘中H、Commit 复制代码A-E步骤都是在内存中完成 A-F之间如果出现问题&#xff0c;由于undo log…

[编程题]圈地运动

[编程题]圈地运动 圈地运动&#xff0c;就是用很多木棍摆在地上组成一个面积大于0的多边形&#xff5e; 小明喜欢圈地运动&#xff0c;于是他需要去小红店里面买一些木棍&#xff0c;期望圈出一块地来。小红想挑战一下小明&#xff0c;所以给小明设置了一些障碍。障碍分别是&a…

从现在开始要学习Python了

Python是个好东东&#xff0c;即没有C和C烦人的指针问题&#xff0c;功能似乎也比PHP要强不少&#xff0c;再重要的是它是纯粹的面向对象语言&#xff0c;语句清晰&#xff0c;容易理解&#xff0c;这对于学习来说有着很大的优势&#xff0c;学习语言的初期都是看别人代码的&am…