NOI.AC 20181103 题解
CF 1037B Reach Median
班上 n个同学(n 是奇数)排成一排站队,为了美观,需要大家高度的中位数是 x。
你可以让同学们在脚下垫木板或者稍微蹲一点来达成这个目标。对任意一位同学的身高减少或者增加1的代价都是1。问
你最少花费多少代价可以让最后大家高度的中位数是x。
solution:
基于一个贪心,现将序列排序。设当前的中位数为MID,位置是pos
那么如果MID比x大,那么将MID=x计算代价,我们的中位数要左移然后就考虑将pos左侧所有小于x的数全部赋值为x计算代价
如果MID比x小,那么将MID=x计算代价,我们中位数要右移就考虑将pos右侧所有大于x的数全部赋值为x计算代价
然后输出答案即可。
Code:
# include <bits/stdc++.h>
# define int long long
using namespace std;
const int MAXN=2e5+;
int a[MAXN],n,x;
inline int read()
{
int X=,w=; char c=;
while (!(c>=''&&c<='')) w|=c=='-',c=getchar();
while (c>=''&&c<='') X=(X<<)+(X<<)+(c^),c=getchar();
return w?-X:X;
}
inline void write(int x)
{
if (x<) { x=-x;putchar('-');}
if (x>) write(x/);
putchar(''+x%);
}
inline void writeln(int x){write(x);putchar('\n');} signed main()
{
n=read(); x=read();
for (int i=;i<=n;i++) a[i]=read();
sort(a+,a++n);
int pos=(n+)/,ans=;
if (x<a[pos]) {
for (int i=;i<=pos;i++)
if (a[i]>x) ans+=a[i]-x;
} else {
for (int i=pos;i<=n;i++)
if (a[i]<x) ans+=x-a[i];
}
writeln(ans);
return ;
}
CF 1037E Reach Median
题目描述
There are nn persons who initially don't know each other. On each morning, two of them, who were not friends before, become friends.
We want to plan a trip for every evening of mm days. On each trip, you have to select a group of people that will go on the trip. For every person, one of the following should hold:
- Either this person does not go on the trip,
- Or at least kk of his friends also go on the trip.
Note that the friendship is not transitive. That is, if aa and bb are friends and bb and cc are friends, it does not necessarily imply that aa and cc are friends.
For each day, find the maximum number of people that can go on the trip on that day.
输入输出格式
输入格式:
The first line contains three integers nn , mm , and kk ( 2 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^52≤n≤2⋅105,1≤m≤2⋅105 , 1 \le k < n1≤k<n ) — the number of people, the number of days and the number of friends each person on the trip should have in the group.
The ii -th ( 1 \leq i \leq m1≤i≤m ) of the next mm lines contains two integers xx and yy ( 1\leq x, y\leq n1≤x,y≤n , x\ne yx≠y ), meaning that persons xx and yy become friends on the morning of day ii . It is guaranteed that xx and yy were not friends before.
输出格式:
Print exactly mm lines, where the ii -th of them ( 1\leq i\leq m1≤i≤m ) contains the maximum number of people that can go on the trip on the evening of the day ii .
输入输出样例
说明
In the first example,
- 1,2,31,2,3 can go on day 33 and 44 .
In the second example,
- 2,4,52,4,5 can go on day 44 and 55 .
- 1,2,4,51,2,4,5 can go on day 66 and 77 .
- 1,2,3,4,51,2,3,4,5 can go on day 88 .
In the third example,
- 1,2,51,2,5 can go on day 55 .
- 1,2,3,51,2,3,5 can go on day 66 and 77 .
一共有n个人,他们开始互不认识,而每天早上不认识的两个人会变成朋友。一共有m天,每天晚上有的人要去旅行,去旅行的人必须满足ta有至少k个朋友也去旅行
求每天去旅行的最大人数
输入格式:
第一行3个整数,表示n,m,kn,m,k
往下m行,每行两个整数x,yx,y,表示这天早上xx和yy会变成朋友
输出格式:
共mm行,每行一个整数,表示每天晚上最多能去旅游的人数
solution:
考虑离线做首先维护一个du,即图上节点的入度如果入度小于k那么就删除这个节点然后维护删去这个点以后发生的事情(比如其他边被删去,其他点又被删去)
这样维护就是一边bfs就行(dfs也行)
具体来说就是先建好图吧最终的状态O(n)统计一遍然后,依次删边,删边的时候注意将du[u]--,du[v]--,如果du[u]或者du[v]等于0那么把这个点删去然后维护全局的答案
然后由于这个点删去了,那么和这个点有关系的所有边都得删去了,然后又造成一部分点的 du 减少于是又得维护这些点的状态删去或者不删。。这就要bfs了。。。
注意删过的点和边不用重复删,就是弄两个数组看看对应的点和边还在不在就行了。
Code:
# include <bits/stdc++.h>
# define int long long
using namespace std;
const int MAXN=2e5+;
struct Edge{ int u,v;};
struct A{ int node,id;};
vector<A>E[MAXN];
vector<Edge>R;
int ret,n,m,k,du[MAXN];
bool ok[MAXN],able[MAXN];
inline int read()
{
int X=,w=; char c=;
while (!(c>=''&&c<='')) w|=c=='-',c=getchar();
while (c>=''&&c<='') X=(X<<)+(X<<)+(c^),c=getchar();
return w?-X:X;
}
void del(int u)
{
if (ok[u]==false) return;
ret--; ok[u]=false; du[u]=;
queue<int>q; q.push(u);
while (!q.empty()) {
int u=q.front();q.pop();
for (int i=;i<E[u].size();i++) {
int v=E[u][i].node;
if (ok[v]==false||able[E[u][i].id]==false) continue;
du[v]--;
able[E[u][i].id]=false;
if (du[v]<k) ok[v]=false,ret--,q.push(v);
}
}
}
void erase(int id)
{
if (able[id]==false) return;
int x=R[id].u,y=R[id].v;
able[id]=false;
du[x]--;if (du[x]<k&&ok[x]) del(x);
du[y]--;if (du[y]<k&&ok[y]) del(y);
}
signed main()
{
n=read();m=read();k=read();
for (int i=;i<=m;i++) {
int u=read(),v=read();
E[u].push_back((A){v,i-});
E[v].push_back((A){u,i-});
du[u]++; du[v]++;
R.push_back((Edge){u,v});
}
ret=n;
memset(ok,true,sizeof(ok));
memset(able,true,sizeof(able));
for (int i=;i<=n;i++) if (du[i]<k) del(i);
stack<int>Ans;
for (int w=R.size()-;w>=;w--) {
Ans.push(ret);
erase(w);
}
while (!Ans.empty()) printf("%lld\n",Ans.top()),Ans.pop();
return ;
}
运气大战
你的班上nn个同学要去参加一项集体比赛。每个人有实力值和运气值。每个人的实力值是确定的,但是运气值是飘忽不定的。一个人的发挥是他的实力值wiwi 和运气值的乘积,即wi⋅rciwi⋅rci。班级的发挥是所有人发挥之和。每个人有一个初始运气值riri,但是每次比赛的时候,每个人的运气值是所有人运气值的一个排列,并且要满足,排列之后ii的运气值不是riri。即满足,ii的运气值是rcirci,{ci}{ci}是1−n1−n的排列,且满足ci≠ici≠i。
现在有QQ场比赛,每场比赛前会交换两人的初始运气值。问你每场比赛班级可以获得的最大发挥是多少。
输入格式
第一行两个整数 nn 和 QQ。
第二行nn个整数w1,...,wnw1,...,wn。
第三行nn个整数r1,...,rnr1,...,rn。
接下来QQ行,每行两个整数ai,biai,bi表示每次要交换运气值的两个人的编号。
输出格式
共QQ行,每行一个答案,表示这场比赛班级的最大发挥。
样例1
输入
5 5
25 1 16 26 19
11 27 4 8 20
1 5
1 5
2 5
1 2
5 4
输出
1527
1543
1543
1536
1536
样例2
输入
输出
数据范围
20% n≤10,q≤10n≤10,q≤10
另30% n≤1000,q≤100n≤1000,q≤100
另20% n≤30000,q≤500n≤30000,q≤500
100% 2≤n≤30000,1≤q≤10000,1≤wi,ri≤1e6,1≤ai≠bi≤n
本题时限:5s
由于排序不等式,我们尽量想顺序放。两边都排序。
由于 n 个不能配的干扰,又不能完全顺序放。
有个结论,最后匹配出第 i 个人的运气值是第 j 个的话,|i-j|\le2∣i−j∣≤2。这个结论从最小化逆序对的个数来看,自己把附近几个线连起来画一画证明一下。
这样就可以用 dp[i]表示到 i 为止所有配好的最优答案。计算的时候需要用到前三轮的答案然后讨论一下。这个是 O(nq)的,可以过70%。
用线段树记录区间答案。区间记录这样的信息:把这个区间前0-2个和后0-2个元素去掉的答案,用3x3的矩阵维护。这样复杂度是O(qlogn)。
#include <cmath>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std; #define A first
#define B second
typedef long long ll; int n,q;
vector<pair<int,int> > L, R;
int whereL[], whereR[];
ll cost[][];
ll dp[]; const ll INF = -3.1e16; inline ll match(int a, int b) {
if (a<= || b<= || a>n || b>n) return INF;
return (ll) L[a].A*R[b].A;
} inline void update(int i) {
if (i<= || i>n) return;
cost[i][] = match(i,i+)+match(i+,i);
cost[i][] = max(match(i,i+)+match(i+,i+)+match(i+,i),match(i,i+)+match(i+,i)+match(i+,i+));
} int main() {
scanf("%d%d",&n,&q);
for (int i=;i<=n;i++) {
L.push_back(make_pair(,i));
R.push_back(make_pair(,i));
}
for (int i=;i<=n;i++) scanf("%d",&L[i].A);
for (int i=;i<=n;i++) scanf("%d",&R[i].A);
sort(L.begin(),L.end());
sort(R.begin(),R.end());
for (int i=;i<=n;i++) {
whereL[L[i].B] = i;
whereR[R[i].B] = i;
}
for (int i=;i<=n;i++) update(i);
for (int Q=;Q<q;Q++) {
int a,b;
scanf("%d%d",&a,&b);
swap(R[whereR[a]].B,R[whereR[b]].B);
swap(whereR[a],whereR[b]);
for (int i=-;i<=;i++) {
update(whereL[a]+i);
update(whereR[a]+i);
update(whereL[b]+i);
update(whereR[b]+i);
}
dp[n+] = ;
for (int i=n;i;i--) {
dp[i] = max(dp[i+]+cost[i][],dp[i+]+cost[i][]);
if (L[i].B!=R[i].B) dp[i] = max(dp[i],(ll) L[i].A*R[i].A+dp[i+]);
}
printf("%lld\n",dp[]);
} return ;
}
NOI.AC 20181103 题解的更多相关文章
- NOI.AC 31 MST——整数划分相关的图论(生成树、哈希)
题目:http://noi.ac/problem/31 模拟 kruscal 的建最小生成树的过程,我们应该把树边一条一条加进去:在加下一条之前先把权值在这一条到下一条的之间的那些边都连上.连的时候要 ...
- [NOI.AC 2018NOIP模拟赛 第三场 ] 染色 解题报告 (DP)
题目链接:http://noi.ac/contest/12/problem/37 题目: 小W收到了一张纸带,纸带上有 n个位置.现在他想把这个纸带染色,他一共有 m 种颜色,每个位置都可以染任意颜色 ...
- # NOI.AC省选赛 第五场T1 子集,与&最大值
NOI.AC省选赛 第五场T1 A. Mas的童年 题目链接 http://noi.ac/problem/309 思路 0x00 \(n^2\)的暴力挺简单的. ans=max(ans,xor[j-1 ...
- NOI.ac #31 MST DP、哈希
题目传送门:http://noi.ac/problem/31 一道思路好题考虑模拟$Kruskal$的加边方式,然后能够发现非最小生成树边只能在一个已经由边权更小的边连成的连通块中,而树边一定会让两个 ...
- NOI.AC NOIP模拟赛 第五场 游记
NOI.AC NOIP模拟赛 第五场 游记 count 题目大意: 长度为\(n+1(n\le10^5)\)的序列\(A\),其中的每个数都是不大于\(n\)的正整数,且\(n\)以内每个正整数至少出 ...
- NOI.AC NOIP模拟赛 第六场 游记
NOI.AC NOIP模拟赛 第六场 游记 queen 题目大意: 在一个\(n\times n(n\le10^5)\)的棋盘上,放有\(m(m\le10^5)\)个皇后,其中每一个皇后都可以向上.下 ...
- NOI.AC NOIP模拟赛 第二场 补记
NOI.AC NOIP模拟赛 第二场 补记 palindrome 题目大意: 同[CEOI2017]Palindromic Partitions string 同[TC11326]Impossible ...
- NOI.AC NOIP模拟赛 第一场 补记
NOI.AC NOIP模拟赛 第一场 补记 candy 题目大意: 有两个超市,每个超市有\(n(n\le10^5)\)个糖,每个糖\(W\)元.每颗糖有一个愉悦度,其中,第一家商店中的第\(i\)颗 ...
- NOI.AC NOIP模拟赛 第四场 补记
NOI.AC NOIP模拟赛 第四场 补记 子图 题目大意: 一张\(n(n\le5\times10^5)\)个点,\(m(m\le5\times10^5)\)条边的无向图.删去第\(i\)条边需要\ ...
随机推荐
- 20155229《网络对抗技术》Exp3:免杀原理与实践
实验预习 免杀: 看为一种能使病毒木马避免被杀毒软件查杀的技术. 免杀的分类: 开源免杀:指在有病毒.木马源代码的前提下,通过修改源代码进行免杀.. 手工免杀:指在仅有病毒.木马的可执行文件(.exe ...
- 20155238 2016-2017-2 《JAVA程序设计》第十周学习总结
教材学习内容总结 # Java计算机网络基础 计算机网络 计算机网络是通过传输介质.通信设施和网络通信协议,把分散在不同地点的计算机设备互连起来,实现资源共享和数据传输的系统.网络编程就就是编写程序使 ...
- 20155325 Exp5 MSF基础应用
目录 实验内容 遇到的问题 基础问题问答 老师!!!我实验三的C代码已经删除了,请求评分~~~ 实验内容 1.Windows服务渗透攻击--MS08-067 系统 虚拟机 参考博客 Windows X ...
- 20155334 曹翔 Exp2 后门原理与实践
20155334 曹翔 Exp2 后门原理与实践 不多废话直接上实验过程,本实验的所有端口都是5334. 一.实验过程 查询主机Windows和虚拟机kali的ip地址: Windows获得Linux ...
- WPF编程,获取句柄将外部程序嵌入到WPF界面。
原文:WPF编程,获取句柄将外部程序嵌入到WPF界面. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_43307934/article/details ...
- Luogu P1726 上白泽慧音
这显然是一道求强连通分量(SCC)的题目. 只要你正常,都知道应该写Tarjan. 然后(假装会写Tarjan),其实我当然不会.但是求SCC还有另一个算法.复杂度和Tarjan一样,只不过常数大了点 ...
- libgdx退出对话框
package com.fxb.newtest; import com.badlogic.gdx.ApplicationListener; import com.badlogic.gdx.Gdx; i ...
- 巧用Alt 键
1,查看表的元数据信息 在TSQL 查询编辑器中,选中一个表,如图 点击Alt+F1,就可以查看表的元数据,列的定义和ID列等 2,使用Alt批量插入逗号 在TQL语句中,有时为了使用 in 子句,必 ...
- 软件测试 —— Bug
[Bug规范] Bug标题中需包含Bug的具体位置并以[]标注 举例:[模块-子模块-页面]XXXXXXXXXXXX Bug标题尽量简明 做什么操作 + 出现什么结果,比如(点击提交按钮,出现卡顿现象 ...
- 基于Vue手写一个下拉刷新
当然不乏有很多下拉刷新的插件可以直接使用,但是自定义程度不强,大部分都只能改改文字,很难满足设计师的创意,譬如淘宝和京东首页那种效果,就需要自己花心思倒腾了,最近刚好有这种需求,做完了稍微总结一下,具 ...