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 ...
随机推荐
- windows 安装 MySQL
windows 安装 MySQL MySQL 目录结构 成功完成 MySQL 数据库的安装和配置!
- Scrapy爬虫基本使用
一.Scrapy爬虫的第一个实例 演示HTML地址 演示HTML页面地址:http://python123.io/ws/demo.html 文件名称:demo.html 产生步骤 步骤1:建议一个Sc ...
- 【学习笔记】《Java编程思想》 第8~11章
第八章 多态 多态的条件: 1. 要有继承 2.父类对象引用子类对象 3. 要有方法的重写 多态的作用:消除类型之间的耦合关系. 将一个方法调用与一个方法主体关联起来称作绑定.若在程序执行前进行绑定, ...
- 解决VS2017中出现:This function or variable may be unsafe
解决办法:项目名称-右键属性-C/C++ - 预处理器 -预处理器定义 - 右侧下拉框中选择"编辑"- 在第一个编辑框中添加_CRT_SECURE_NO_WARNINGS
- imread函数+cvtColor()函数
加载图像(用cv::imread) imread功能是加载图像文件成为一个Mat对象,其中第一个参数表示图像文件名称 第二个参数,表示加载的图像是什么类型,支持常见的三个参数值 IMREAD_UNCH ...
- 2.1 【配置环境】 JDK + eclipse + selenium
1.jdk以及eclipse的具体安装详见 http://www.cnblogs.com/ericazy/p/6082194.html 安装1.7 jdk即可 2.selenium 旧版本安装: s ...
- LPR-贷款市场报价利率
贷款市场报价利率(Loan Prime Rate)介绍: 贷款市场报价利率(Loan Prime Rate,简称LPR)是商业银行对其最优质客户执行的贷款利率,其他贷款利率可在此基础上加减点生成.贷款 ...
- Atcoder Beginner Contest151E(排列组合)
排列组合 #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace std; ]; ; ]; long lo ...
- Java经典面试笔试题及答案
1.什么是对象序列化,为什么要使用? 所谓对象序列化就是把一个对象以二进制流的方式保存到硬盘上.好处:方便远程调用. 2.值传递与引用传递的区别? 所谓值传递就是把一个对象的值传给一个新的变量,但是系 ...
- 刷题5. Longest Palindromic Substring
一.题目说明 Longest Palindromic Substring,求字符串中的最长的回文. Difficuty是Medium 二.我的实现 经过前面4个题目,我对边界考虑越来越"完善 ...