Codeforces Round #611 (Div. 3) E
Oh, New Year. The time to gather all your friends and reflect on the heartwarming events of the past year...
nn friends live in a city which can be represented as a number line. The ii -th friend lives in a house with an integer coordinate xixi . The ii -th friend can come celebrate the New Year to the house with coordinate xi−1xi−1 , xi+1xi+1 or stay at xixi . Each friend is allowed to move no more than once.
For all friends 1≤xi≤n1≤xi≤n holds, however, they can come to houses with coordinates 00 and n+1n+1 (if their houses are at 11 or nn , respectively).
For example, let the initial positions be x=[1,2,4,4]x=[1,2,4,4] . The final ones then can be [1,3,3,4][1,3,3,4] , [0,2,3,3][0,2,3,3] , [2,2,5,5][2,2,5,5] , [2,1,3,5][2,1,3,5] and so on. The number of occupied houses is the number of distinct positions among the final ones.
So all friends choose the moves they want to perform. After that the number of occupied houses is calculated. What is the minimum and the maximum number of occupied houses can there be?
Input
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105 ) — the number of friends.
The second line contains nn integers x1,x2,…,xnx1,x2,…,xn (1≤xi≤n1≤xi≤n ) — the coordinates of the houses of the friends.
Output
Print two integers — the minimum and the maximum possible number of occupied houses after all moves are performed.
大意是有n个点,每个点有一定数量的人,一个点的人可以移动到这个点的位置xi左右两边或选择不移动(xi+1 xi-1 xi)。让求这些人经过移动(或不移动)后最多能占据多少个不同的点,最少能占据多少个不同的点。
最少点的情况比较简单(但做的时候想麻烦了 ,只需要遍历一遍数组,遇到没有人的地方直接跳过,有人的地方计数器++然后i+=2。因为这是从左到右统计的,基于贪心的思想,如果i位置有人,i+1位置的所有人一起移动到i位置显然比其他方案更优。
最多点的情况稍微复杂一点。我用一个vis数组记录了点的占据情况。
(1)如果一个点有三个人以上,可以把这三个人分到i-1,i,i+1的位置;
(2)如果一个点有两个人:
[1] i-1没有人:分到i-1和i
[2]其他情况:分到i和i+1
(3)一个点只有一个人:
[1]i-1没有人:分到i-1
[2]i-1有人,i没有人:分到i
[3]其他情况:分到i+1
“分到xx”即为vis数组的对应位置打上标记。最后统计打了标记的位置即可。
#include <bits/stdc++.h>
using namespace std;
int n;
struct people
{
int pos;
int cnt;
}p[];
int vis[]={};
int solvemin()
{
int ans=;
int i;
for(i=;i<=n;i++)
{
if(p[i].cnt==)continue;
ans++;
i+=;
}
return ans;
}
int solvemax()
{
int i;
int ans=;
for(i=;i<=n;i++)
{
if(p[i].cnt>=)
{
vis[i-]++;
vis[i]++;
vis[i+]++;
}
else if(p[i].cnt==)
{
if(!vis[i-])
{
vis[i-]++;
vis[i]++;
}
else
{
vis[i]++;
vis[i+]++;
}
}
else if(p[i].cnt==)
{
if(!vis[i-])
{
vis[i-]++;
}
else if(!vis[i])
{
vis[i]++;
}
else vis[i+]++;
}
else continue;
}
for(i=;i<=n+;i++)
{
if(vis[i])ans++;
}
return ans;
}
int main()
{
cin>>n;
int i;
for(i=;i<=n;i++)
{
p[i].pos=i;
p[i].cnt=;
}
for(i=;i<=n;i++)
{
int temp;
scanf("%d",&temp);
p[temp].cnt++;
}
int mmin=solvemin();
int mmax=solvemax();
cout<<mmin<<' '<<mmax;
return ;
}
Codeforces Round #611 (Div. 3) E的更多相关文章
- Codeforces Round #611 (Div. 3) A-F简要题解
contest链接:https://codeforces.com/contest/1283 A. Minutes Before the New Year 题意:给一个当前时间,输出离第二天差多少分钟 ...
- Codeforces Round #611 (Div. 3)
原题面:https://codeforces.com/contest/1283 A.Minutes Before the New Year 题目大意:给定时间,问距离零点零分还有多久? 分析:注意一下 ...
- Codeforces Round #611 (Div. 3) C
There are nn friends who want to give gifts for the New Year to each other. Each friend should give ...
- Codeforces Round #611 (Div. 3) D
There are nn Christmas trees on an infinite number line. The ii -th tree grows at the position xixi ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
随机推荐
- webpack4.41.0配置一(基础配置webpack文件,入口出口,实现打包)
1.查看node.js版本.npm版本和webpack版本(使用webpack4时,请确保node.js的版本>=8.9.4) 2.我先重新卸载了webpack和webpack-cli(全局) ...
- MySQL学习(九)小结
redo-log 和 bin-log 是如何联系起来的? update 语句在更新的时候先更新内存后,写 redo-log 然后 bin-log ,其中后面一步是使用了两阶段提交, 也就是每一个更新都 ...
- Spark对接Kafka、HBase
本项目是为网站日志流量分析做的基础:网站日志流量分析系统,Kafka.HBase集群的搭建可参考:使用Docker搭建Spark集群(用于实现网站流量实时分析模块),里面有关于该搭建过程 本次对接Ka ...
- pyfits fits图像区域选择
在用pyfits读取fits格式的图像时,得到的数组的结构如下 f=pyfits.open('rr.fits') data1=f[0].data data1数组的第一行,对应于图像的最下面一行,数组第 ...
- fiddler中文乱码解决方案
只用添加一个注册表变量就行 cmd窗口执行regedit命令,在弹出的注册表编辑界面找到fiddler 右击新建一个字符传值 HeaderEncodingGBK 结果如上图右所示~ 重启fiddler ...
- VS Code 配置 C/C++ (Windwos)
下载VSCode https://code.visualstudio.com/Download 在扩展里安装C/C++插件 配置MinGW 需要再环境变量中的path中添加MinGW的bin目录 修改 ...
- selenium通过cookies直接免密登录
前提知识: 1.webdriver中提供了操作cookie的相关方法: get_cookies() 获得cookie信息 add_cookie(cookie_di ...
- 每天进步一点点------SOPC的Avalon-MM IP核(三) LCD1602 IP定制
注:Avalon信号类型命名参考图 /********************************************************************************* ...
- JavaScript可枚举的属性
/* 把P中的可枚举属性复制到o中,并返回o中 如果o和p中含有同名的属性,则覆盖O中的属性 这个函数并不处理getter和setter以及复制属性 */ function extend(o,p){ ...
- linux中的diff命令
今天在公司的代码中看到了一个用的不是很多的命令diff,一开始以为不是,后来一查发现还真有这个命令,有关它的详细资料在这个网址中查看[http://blog.chinaunix.net/uid-253 ...