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 最大密度子图

题意

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

题解

codeforces contest1082的更多相关文章

  1. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  2. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  3. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  4. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  5. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  6. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  7. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  8. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  9. CodeForces - 148D Bag of mice

    http://codeforces.com/problemset/problem/148/D 题目大意: 原来袋子里有w只白鼠和b只黑鼠 龙和王妃轮流从袋子里抓老鼠.谁先抓到白色老鼠谁就赢. 王妃每次 ...

随机推荐

  1. hadoop记录-hadoop集群日常运维命令

    hadoop集群日常运维命令 #1.namenode hadoop namenode -format #格式化,慎用 su hdfs hadoop-daemon.sh start namenode h ...

  2. Telegraf+InfluxDB+Grafana搭建服务器监控平台

    Telegraf+InfluxDB+Grafana搭建服务器监控平台 tags:网站 个人网站:https://wanghualong.cn/ 效果展示 本站服务器状态监控:https://statu ...

  3. DirectX11--实现一个3D魔方(1)

    前言 可以说,魔方跟我的人生也有一定的联系. 在高中的学校接触到了魔方社,那时候的我虽然也能够还原魔方,可看到大神们总是可以非常快地还原,为此我也走上了学习高级公式CFOP的坑.当初学习的网站是在魔方 ...

  4. JS“盒子模型”

    列举几个常用的属性 client系列 clientWidth - 盒子真实内容的宽度[content+padding左右],不包括边线和滚动条 clientHeight - 盒子真实内容的高度[con ...

  5. 利用LI标签仿照a中Link进行页面跳转?

    点击LI时仿照A标签进行页面跳转html: <ul> <li link="/school/schooldetail/success_detail?case_id=<! ...

  6. C#中 将图片保存到Sql server 中

    private void Form1_Load(object sender, EventArgs e) { #region 保存数据库 string url = @"C:\Users\Adm ...

  7. python爬虫得到unicode编码处理方式

    在用python做爬虫的时候经常会与到结果中包含unicode编码,需要将结果转化为中文,处理方式如下 str.encode('utf-8').decode('unicode_escape')

  8. MSM8909的触摸屏驱动导致的熄屏后重新亮屏速度慢的原因!【转】

    转自:https://blog.csdn.net/kk20000/article/details/83041081 使用的汇顶的触摸驱动的时候会重新亮屏速度慢3秒,而在使用另外一个敦泰触摸驱动的时候没 ...

  9. 题解-HAOI2018全套

    去冬令营转了一圈发现自己比别人差根源在于刷题少,见过的套路少(>ω<) 于是闲来无事把历年省选题做了一些 链接放的都是洛谷的,bz偷懒放的也是链接 AM.T1 奇怪的背包 Problem ...

  10. 微软将把Windows Defender防火墙传递给 Linux 子系统

    前不久,微软以 Azure Sphere OS 的形式发布了自己的 Linux 版本.而在最新的开发中,该公司又决定将其 Windows Defender 防火墙的传递给 Linux 子系统(WSL) ...