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 ...
随机推荐
- 2019牛客多校第一场E ABBA dp
ABBA dp 题意 给出2(N+M)个AB字符,问能构造出N个AB子序列和M个BA子序列组成的2*(n+m)的序列种类有多少 思路 碰到计数构造类的题目,首先要去找到判断合法性的条件,即什么情况下合 ...
- OSI七层协议详解
一.简介 开放系统互连参考模型 (Open System Interconnect 简称OSI)是国际标准化组织(ISO)和国际电报电话咨询委员会(CCITT)联合制定的开放系统互连参考模型,为开放式 ...
- SpringBoot集成mybatis以及自动化测试代码实现
Mybatis和logback的应用配置 1.在module的pom.xml文件中,加载springboot和swagger.lombok.fastjson.mysql.mybatis包 2.在res ...
- try catch 语句中有return 的各类情况
在牛客上做java题时遇到过多到关于try catch语句的问题,看了很多答案解析,在这里记录一下. 首先给出一道题目: 下面代码的运行结果为? A.catch语句块 和是43 B.编译异常 C.fi ...
- MyBatis(5)——解决属性名与列名不一致的问题
解决属性名与列名不一致的问题 问题描述: 当实体类的属性与数据库的列名不对应时取不到该列数据 说明:MyBatis会根据查询的列名设值(列名的setter方法),然后以此列名为做查询等操作,在此过程中 ...
- JVM中的动态语言支持简介
抽丝剥茧 细说架构那些事——[优锐课] 从版本6开始,JVM已扩展为支持现代动态语言(也称为脚本语言).Java8的发行为这一领域提供了更多动力.感到这种支持的必要性是因为Java作为一种语言固有地是 ...
- STM32程序烧录总结
1.程序烧录方式 1)ST-LINK下载 2)SWD下载 SWD对应的引脚为:GND.RST.SWDIO.SWDCLK SWD与Jlink的比较 3)串口下载 串口下载不能直接在MDK点击Downlo ...
- Drf模块详细分析
drf的请求模块 drf的request是在wdgi的request基础上再次封装 wsgi的request作为drf的request一个属性:_request 新的request对旧的request ...
- Java - Test - TestNG: Idea 引入 testng.xml 自动生成插件
1. 概述 Idea 引入自动生成 testng.xml 插件 自动生成 testng.xml 2. 背景 testng 调试 调试 testng, 主要是这两种方法 ide 下直接执行测试 方法 类 ...
- Springmvc-crud-06(路径忘记加上“/”错误)
错误: 原因:自己马虎忘记加" / ",罚继续写代码┭┮﹏┭┮ 前端代码: <h1>添加功能</h1> <form action="te ...