CF830A Office Keys(贪心)
CF830A Office Keys
【题目链接】CF830A Office Keys
【题目类型】贪心
&题意:
有n个人,k个钥匙,一个目的地,求让n个人都回到目的地的最短时间,每个人都要拿钥匙才能回目的地
&题解:
这题做的时候没有认真想样例,如果仔细想的话就能发现n个人选的n个钥匙一定是连续的(在排过序之后),你可以这样想:
如果n个人直接去目的地,那么就肯定是连续的区间了吧,如果这个区间里钥匙数够,就可以直接回去了,如果不够,肯定是在区间的两边寻找剩下的钥匙,所以一定是连续的区间
&代码:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define fo(i,a,b) for(int i=(a);i<=(b);i++)
#define fd(i,a,b) for(int i=(a);i>=(b);i--)
#define cle(a,v) memset(a,(v),sizeof(a))
const int maxn = 2e3 + 7;
int n, m, q;
int a[maxn], b[maxn];
int main() {
#ifndef ONLINE_JUDGE
freopen("E:1.in", "r", stdin);
#endif
scanf("%d%d%d", &n, &m, &q);
fo(i, 0, n - 1) scanf("%d", &a[i]);
fo(i, 0, m - 1) scanf("%d", &b[i]);
sort(b, b + m);
sort(a, a + n);
int ans = 0x7fffffff;
for(int i = 0; i <= m - n; i++) {
int ma = 0;
for(int j = 0; j < n; j++) {
ma = max(ma, abs(a[j] - b[i + j]) + abs(b[i + j] - q));
}
ans = min(ans, ma);
}
printf("%d\n", ans);
return 0;
}
CF830A Office Keys(贪心)的更多相关文章
- Codeforces 830A. Office Keys (贪心二分 or DP)
原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...
- Codeforces831D Office Keys
D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) D. Office Keys time limit per test2 seconds 二分
D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Office Keys(思维)
Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- codeforce830A. Office Keys
A. Office Keys time limit per test: 2 seconds memory limit per test: 256 megabytes input standard: i ...
- CF830A/831D Office Keys
思路: 问题的关键在于对钥匙按照位置排序之后,最终选择的n个钥匙一定是其中的一个连续的区间. 实现: #include <iostream> #include <cstdio> ...
- code force 424 A - Office Keys
There are n people and k keys on a straight line. Every person wants to get to the office which is l ...
- 【Codeforces Round #424 (Div. 2) D】Office Keys
[Link]:http://codeforces.com/contest/831/problem/D [Description] 有n个人,它们都要去一个终点,终点位于p; 但是,在去终点之前,他们都 ...
- CF-831D Office Keys 思维题
http://codeforces.com/contest/831/problem/D 题目大意是在一条坐标轴上,给出n个人,k把钥匙(k>=n)以及终点的坐标,所有人都可以同时运动,但不可以公 ...
随机推荐
- android setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
手工设置文本与图片相对位置时,常用到如下方法: setCompoundDrawables(left, top, right, bottom) setCompoundDrawablesWithIntri ...
- 咸鱼入门到放弃9--jsp中使用的JavaBean
一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...
- angularjs 设置全局变量(constant)
转:angularjs自身有二种,设置全局变量的方法,在加上js的设置全局变量的方法,总共有三种.要实现的功能是,在ng-app中定义的全局变量,在不同的ng-controller里都可以使用. 1, ...
- Numpy初步
1,获取矩阵行列数 Import numpyasnp #创建二维的naaray对象 a=np.array([[1,2,3,4,5],[6,7,8,9,10]]) print(a.shape) #返 ...
- phpstorm设置篇
1.设置全局字体编码: File->settings->Editor->File Encodings 进入这个页面后,有个Global Encoding , 默认是 UTF8 ,如果 ...
- Web版记账本开发记录(一)代码和功能展示
一丶基本机构 数据库截图 record表 年份表 index.jsp <%@ page language="java" contentType="text/html ...
- Gparted Live分区调整
由于年少无知,在安装ubuntu系统的时候,以为/temp是软件包安装时解压的缓冲,所以给/temp留了10G,而以为/var只是记录一些log而已,因此把仅存的1G分配给了它.随后在安装软件时出现“ ...
- Float.intBitsToFloat
Float.intBitsToFloat(0b) Float.intBitsToFloat(0) Float.intBitsToFloat(0x) ========================== ...
- WcPro项目(WordCount优化)
1 基本任务:代码编写+单元测试 1.1 项目GitHub地址 https://github.com/ReWr1te/WcPro 1.2 项目PSP表格 PSP2.1 PSP阶段 预估耗时(分钟) 实 ...
- Linux命令 at cron
at: 可以处理仅执行一次就结束排程的指令.需要atd服务 crontab: 所设定的指令将会循环地一直进行下去.需要crontab服务 at: Ubuntu16.04 默认没有安装atd服务.安装命 ...