[gym102900H]Rice Arrangement
(可以先阅读题目中关于顺逆时针的定义,避免理解错误)
考虑一盘菜$b_{i}$被$a_{j}$吃掉,对于其最后一次移动:如果是顺时针,则称$b_{i}$的移动区间为$[a_{j},b_{i}]$(若$b_{i}<a_{j}$则为$[a_{j},n)\cup[0,b_{i}]$的环),反之类似(特别的,如果$a_{j}=b_{i}$则区间为$[b_{i},b_{i}]$)
记顺时针的最大移动区间为$l_{1}$,逆时针为$l_{2}$,答案即为$l_{1}+l_{2}+\min(l_{1},l_{2})$
若某两个人移动区间相互包含,对两者移动方向分类讨论,可以发现都可以更换配对方式使得$l_{1}$和$l_{2}$不增加,换言之存在最优解使得任意两盘菜移动区间不包含
(注意:环的包含可以看作集合的包含)
通过这个,我们可以得到这样一个结论:将$a_{i}$和$b_{i}$从小到大排序后,存在一组最优解,满足存在$x$使得是$a_{i}$匹配$b_{(i-1+x)mod\ k+1}$(其中$0\le x<k$)
(这个结论大概是挺难证的)
有了这个结论,首先枚举$x$,即确定了配对方案
接下来,先考虑暴力枚举$l_{1}$,之后将顺时针结果不超过$l_{1}$用顺时针做,再维护逆时针的最大值,通过将顺时针从小到大排序以及维护后缀逆时针最大值就可以做了
时间复杂度为$o(k^{2}\log k)$,可以通过

1 #include<bits/stdc++.h>
2 using namespace std;
3 #define N 1005
4 pair<int,int>v[N];
5 int t,n,m,ans,a[N],b[N],mx[N];
6 int len(int x,int y){
7 if (x<=y)return y-x;
8 return m-(x-y);
9 }
10 int main(){
11 scanf("%d",&t);
12 while (t--){
13 scanf("%d%d",&m,&n);
14 for(int i=0;i<n;i++)scanf("%d",&a[i]);
15 sort(a,a+n);
16 for(int i=0;i<n;i++)scanf("%d",&b[i]);
17 sort(b,b+n);
18 ans=m;
19 for(int i=0;i<n;i++){
20 for(int j=0;j<n;j++)v[j]=make_pair(len(a[j],b[(j+i)%n]),len(b[(j+i)%n],a[j]));
21 sort(v,v+n);
22 mx[n]=0;
23 for(int j=n-1;j>=0;j--)mx[j]=max(mx[j+1],v[j].second);
24 ans=min(ans,mx[0]);
25 for(int j=0;j<n;j++)ans=min(ans,v[j].first+mx[j+1]+min(v[j].first,mx[j+1]));
26 }
27 printf("%d\n",ans);
28 }
29 }
[gym102900H]Rice Arrangement的更多相关文章
- HDU 4884 TIANKENG’s rice shop (模拟)
TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...
- TIANKENG’s rice shop
Problem Description TIANKENG managers a pan fried rice shop. There are n kinds of fried rice numbere ...
- 1056. Mice and Rice (25)
时间限制 30 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice and Rice is the name of a pr ...
- [leetcode-526-Beautiful Arrangement]
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- zoj3777 Problem Arrangement
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
- ZOJ 3777-Problem Arrangement(状压DP)
B - Problem Arrangement Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- [LeetCode] Beautiful Arrangement II 优美排列之二
Given two integers n and k, you need to construct a list which contains n different positive integer ...
- [LeetCode] Beautiful Arrangement 优美排列
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is const ...
- PAT1056:Mice and Rice
1056. Mice and Rice (25) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Mice an ...
随机推荐
- 高中最后一刻&大学第一课&为人师的责任
文章不是技术文,只是分享一些感想,作为一只程序猿,不说好好敲代码,跑出来思考人生,不是合格的程序猿,罪过罪过,自我反思3秒钟,我们继续,毕竟程序猿的人生不只是Coding,也希望自己这点感想被更多刚入 ...
- 题解 CF241E Flights
题目传送门 题目大意 给出一个 \(n\) 个点 \(m\) 条边的 \(\texttt{DAG}\) ,给每条边设定边权为 \(1\) 或者 \(2\) ,使得 \(1\to n\) 的每条路径长度 ...
- Java(32)File类的介绍
作者:季沐测试笔记 原文地址:https://www.cnblogs.com/testero/p/15228444.html 博客主页:https://www.cnblogs.com/testero ...
- change or reset WSL password
change or reset WSL password To change or reset your password, open the Linux distribution and enter ...
- TX-LCN分布式事务之LCN模式
什么是LCN模式 LCN模式是TX-LCN分布式事务模式的一种,L-lock-锁定事务单元.C-confirm-确认事务模块状态. notify-通知事务单元 原理 LCN模式是通过Spring AO ...
- 【二食堂】Beta - 发布声明
Beta - 发布声明 新功能 在Beta阶段,图谱方面的新功能有:自定义关系的添加与删除.实体查找.实体名称的修改.实体之间关系的修改.新增了项目创建与删除功能,此外还增加了好友系统,可以实现好友的 ...
- [Beta]the Agiles Scrum Meeting 10
会议时间:2020.5.25 21:00 1.每个人的工作 今天已完成的工作 成员 已完成的工作 issue yjy 暂无 tq 暂无 wjx 实现创建.显示博客作业功能 增加博客作业功能 dzx 实 ...
- 第1次 Beta Scrum Meeting
本次会议为Beta阶段第1次Scrum Meeting会议 会议概要 会议时间:2021年5月29日 会议地点:「腾讯会议」线上进行 会议时长:0.5小时 会议内容简介:本次会议为Beta阶段第1次会 ...
- [hi3521] nand flash 的 boot 启动模式的区别?
spi nand flash 的 boot 启动模式选择.0:1 线 boot:1:4 线 boot.请问,1线boot和4线boot有什么区别呢?该如何选择呢? 收藏 顶 踩 回复 使用 ...
- C语言零基础入门难发愁,那就快来看看这篇基础整理资料吧
C语言程序的结构认识 用一个简单的c程序例子,介绍c语言的基本构成.格式.以及良好的书写风格,使小伙伴对c语言有个初步认识. 例1:计算两个整数之和的c程序: #include main() { in ...