D. Maximum Diameter Graph

题意

给出每个点的最大度,构造直径尽可能长的树

思路

让度数大于$1$的点构成链,考虑是否能在链的两端加度为$1$的点

代码

#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl;
const int maxn = 1e3+5;
using namespace std;
typedef long long LL; int n; struct node{
int id,val;
}a[maxn]; bool cmp(node a,node b){
return a.val > b.val;
}
vector<pair<int,int> >vec; int main(){
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;i++){
scanf("%d",&a[i].val);
sum+=a[i].val;
a[i].id=i;
}
if(sum < (n-1)*2){puts("NO");return 0;}
sort(a+1,a+1+n,cmp);
int pos=n,cnt=0;
for(int i=1;i<=n;i++){
if(a[i].val > 1)cnt++;
else{pos=i-1;break;}
}
for(int i=2;i<=pos;i++){
vec.push_back({a[i].id,a[i-1].id});
a[i].val--;
a[i-1].val--;
}
int ans=pos-1;
if(pos != n){
vec.push_back({a[1].id,a[pos+1].id});
a[1].val--;
a[pos+1].val--;
ans++;
}
int flag=0;
for(int j=pos+1;j<=n;j++){
for(int i=pos;i>=1;i--){
if(a[j].val == 0)break;
if(a[i].val == 0)continue;
vec.push_back({a[j].id,a[i].id});
a[j].val--;
a[i].val--;
if(flag == 0 && i == pos){flag=1;ans++;}
}
}
printf("YES %d\n",ans);
printf("%d\n",vec.size());
for(int i=0;i<vec.size();i++){
printf("%d %d\n",vec[i].first,vec[i].second);
}
return 0;
}

E. Increasing Frequency

题意

让某段区间的数字加上某个值使最终等于$c$的数尽可能多

思路

从左往右枚举左边区间最优情况下的情况,考虑与当前左区间右边界相同的值变成c的情况,维护$ans$

代码

#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl;
const int maxn = 5e5+5;
using namespace std;
typedef long long LL; LL n,k,a[maxn];
LL pre[maxn],suf[maxn],expre[maxn],last[maxn]; int main(){
scanf("%I64d%I64d",&n,&k);
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
if(a[i] == k)pre[i]++,suf[i]++;
}
for(int i=1;i<=n;i++)pre[i]+=pre[i-1];
for(int i=n;i>=1;i--)suf[i]+=suf[i+1];
for(int i=1;i<=n;i++){
expre[i]=pre[i-1]+1;
int pos=last[a[i]];
if(pos)expre[i]=max(expre[i],expre[pos]+1);
last[a[i]]=i;
}
LL ans=0;
for(int i=1;i<=n+1;i++)ans=max(ans,expre[i-1]+suf[i]);
printf("%I64d\n",ans);
return 0;
}

G. Petya and Graph

题意

找到这样的子图满足图内的边只连接图内的点,求最大权值

思路

最大权闭合子图板题,不知道是什么可以看这个博客,bzoj上甚至有原题(这题还不爆int

代码

#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const LL inf = 1e18;
const LL maxn = 1e5+5; struct Dinic {
static const LL maxn = 1e5+5;
static const LL maxm = 4e5+5; struct Edge {
LL u, v, next, flow, cap;
} edge[maxm*2]; LL head[maxn], level[maxn], cur[maxn], eg; void addedge(LL u, LL v, LL cap) {
edge[eg]={u,v,head[u],0,cap},head[u]=eg++;
edge[eg]={v,u,head[v],0, 0},head[v]=eg++;
}
void init() {
eg = 0;
memset(head, -1, sizeof head);
}
bool makeLevel(LL s, LL t, LL n) {
for(int i = 0; i < n; i++) level[i] = 0, cur[i] = head[i];
queue<LL> q; q.push(s);
level[s] = 1;
while(!q.empty()) {
int u = q.front();
q.pop();
for(LL i = head[u]; ~i; i = edge[i].next) {
Edge &e = edge[i];
if(e.flow < e.cap && level[e.v] == 0) {
level[e.v] = level[u] + 1;
if(e.v == t) return 1;
q.push(e.v);
}
}
}
return 0;
}
LL findpath(LL s, LL t, LL limit = inf) {
if(s == t || limit == 0) return limit;
for(LL i = cur[s]; ~i; i = edge[i].next) {
cur[edge[i].u] = i;
Edge &e = edge[i], &rev = edge[i^1];
if(e.flow < e.cap && level[e.v] == level[s] + 1) {
int flow = findpath(e.v, t, min(limit, e.cap - e.flow));
if(flow > 0) {
e.flow += flow;
rev.flow -= flow;
return flow;
}
}
}
return 0;
}
LL max_flow(int s, int t, int n) {
LL ans = 0;
while(makeLevel(s, t, n)) {
LL flow;
while((flow = findpath(s, t)) > 0) ans += flow;
}
return ans;
}
} di; LL n,m,st,en;
LL a[maxn]; int main() {
di.init();
scanf("%I64d%I64d",&n,&m);
st=0,en=n+m+1;
for(int i=1;i<=n;i++){
scanf("%I64d",&a[i]);
di.addedge(i,en,a[i]);
}
LL sum=0;
for(int i=1;i<=m;i++){
LL x,y,z;
scanf("%I64d%I64d%I64d",&x,&y,&z);
di.addedge(st,i+n,z);
di.addedge(i+n,x,inf);
di.addedge(i+n,y,inf);
sum+=z;
}
printf("%I64d\n",sum-di.max_flow(st,en,en+1));
return 0;
}

  

Educational Codeforces Round 55 (Rated for Div. 2)的更多相关文章

  1. Educational Codeforces Round 55 (Rated for Div. 2) C. Multi-Subject Competition 【vector 预处理优化】

    传送门:http://codeforces.com/contest/1082/problem/C C. Multi-Subject Competition time limit per test 2 ...

  2. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  3. Educational Codeforces Round 55 (Rated for Div. 2) B. Vova and Trophies 【贪心 】

    传送门:http://codeforces.com/contest/1082/problem/B B. Vova and Trophies time limit per test 2 seconds ...

  4. Codeforces 1082 C. Multi-Subject Competition-有点意思 (Educational Codeforces Round 55 (Rated for Div. 2))

    C. Multi-Subject Competition time limit per test 2 seconds memory limit per test 256 megabytes input ...

  5. Codeforces 1082 A. Vasya and Book-题意 (Educational Codeforces Round 55 (Rated for Div. 2))

    A. Vasya and Book time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  6. Educational Codeforces Round 55 (Rated for Div. 2):E. Increasing Frequency

    E. Increasing Frequency 题目链接:https://codeforces.com/contest/1082/problem/E 题意: 给出n个数以及一个c,现在可以对一个区间上 ...

  7. Educational Codeforces Round 55 (Rated for Div. 2):D. Maximum Diameter Graph

    D. Maximum Diameter Graph 题目链接:https://codeforces.com/contest/1082/problem/D 题意: 给出n个点的最大入度数,要求添加边构成 ...

  8. Educational Codeforces Round 55 (Rated for Div. 2):C. Multi-Subject Competition

    C. Multi-Subject Competition 题目链接:https://codeforces.com/contest/1082/problem/C 题意: 给出n个信息,每个信息包含专业编 ...

  9. Educational Codeforces Round 55 (Rated for Div. 2)E

    题:https://codeforces.com/contest/1082/problem/E 题意:给出n个数和一个数c,只能操作一次将[L,R]之间的数+任意数,问最后该序列中能存在最多多少个c ...

随机推荐

  1. poj 3090 Visible Lattice Points(离线打表)

    这是好久之前做过的题,算是在考察欧拉函数的定义吧. 先把欧拉函数讲好:其实欧拉函数还是有很多解读的.emmm,最基础同时最重要的算是,¢(n)表示范围(1, n-1)中与n互质的数的个数 好了,我把规 ...

  2. 函数rand,randn,randi

    1,rand 生成均匀分布的伪随机数.分布在(0~1)之间主要语法:rand(m,n)生成m行n列的均匀分布的伪随机数rand(m,n,‘double’)生成指定精度的均匀分布的伪随机数,参数还可以是 ...

  3. 可视化工具Grafana:简介及安装

    随着业务的越发复杂,对软件系统的要求越来越高,这意味着我们需要随时掌控系统的运行情况.因此,对系统的实时监控以及可视化展示,就成了基础架构的必须能力. 这篇博客,介绍下开源的可视化套件grafana的 ...

  4. Struts2的核心——拦截器

    虽然以前已经学了很多的拦截器,但是在这里还是想重头梳理一下所有有关拦截器的知识,尤其是struts2中的拦截器 1:拦截器是什么? java里的拦截器是动态拦截Action调用的对象.它提供了一种机制 ...

  5. Nowcoder217D msc的背包 背包、生成函数、组合

    传送门 发现这是一个背包问题,而\(k\)又很大,考虑生成函数方式解决这个问题. 对于体积为\(1\)的物品的生成函数为\(\frac{1}{1 - x}\),体积为\(2\)的物品的生成函数为\(\ ...

  6. Bootstrap开发框架视频整理

    最近到客户处进行实地培训,整理了很多培训的材料,现将它们录制相关主题的视频,作为我的Bootstrap开发框架的知识补充,希望给感兴趣的朋友进行了解.培训内容主要包括基础框架部分.MVC框架部分.Bo ...

  7. Ubuntu安装mysql之后,编译找不到头文件

    解决Ubuntu安装mysql之后找不到mysql.h问题   安装: sudo apt-get install libmysqlclient-dev   编译: gcc test.c -o test ...

  8. linux安装tomcat部署web项目

    我用的是如下图的两个软件,连接linux服务器. 其中WinSCp是传输文件用的,SecureCRT是用来输入命令的. 1.复制tomcat到指定目录(可复制到你想要的目录下),命令如下: cp /路 ...

  9. RPC----Hadoop核心协议

    什么是RPC RPC设计的目的 RPC的作用 远程过程调用(RPC)是一个协议,程序可以使用这个协议请求网络中另一台计算机上某程序的服务而不需要知道网络细节. 必备知识: 网络七层模型 网络四层模型 ...

  10. 通过CONN_MAX_AGE优化Django的数据库连接

    上周对我们用Django+Django-rest-framework提供的一套接口进行了压力测试.压测的过程中,收到DBA通知——数据库连接数过多,希望我们优化下程序.具体症状就是,如果设置mysql ...