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(贪心)的更多相关文章

  1. Codeforces 830A. Office Keys (贪心二分 or DP)

    原题链接:http://codeforces.com/contest/830/problem/A 题意:在一条数轴上分别有n个人和k把钥匙(n<=k),以及一个目的地,每个人要各自拿到一个钥匙后 ...

  2. Codeforces831D Office Keys

    D. Office Keys time limit per test 2 seconds memory limit per test 256 megabytes input standard inpu ...

  3. 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 ...

  4. 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 ...

  5. codeforce830A. Office Keys

    A. Office Keys time limit per test: 2 seconds memory limit per test: 256 megabytes input standard: i ...

  6. CF830A/831D Office Keys

    思路: 问题的关键在于对钥匙按照位置排序之后,最终选择的n个钥匙一定是其中的一个连续的区间. 实现: #include <iostream> #include <cstdio> ...

  7. 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 ...

  8. 【Codeforces Round #424 (Div. 2) D】Office Keys

    [Link]:http://codeforces.com/contest/831/problem/D [Description] 有n个人,它们都要去一个终点,终点位于p; 但是,在去终点之前,他们都 ...

  9. CF-831D Office Keys 思维题

    http://codeforces.com/contest/831/problem/D 题目大意是在一条坐标轴上,给出n个人,k把钥匙(k>=n)以及终点的坐标,所有人都可以同时运动,但不可以公 ...

随机推荐

  1. entityframework单例模式泛型用法

    public class yms_Entity<T> where T :DbContext { private static T _instance; public static read ...

  2. python flask_Sqlalchemy管理数据库

    懒癌复发直接粘贴代码,算是做一个简单备份吧. #coding:utf8 from flask import Flask from flask_sqlalchemy import SQLAlchemy ...

  3. Python金融大数据分析PDF

    Python金融大数据分析(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1CF2NhbgpMroLhW2sTm7IJQ 提取码:clmt 复制这段内容后打开百度网盘 ...

  4. NOIP-质因数分解

    题目描述 已知正整数n是两个不同的质数的乘积,试求出较大的那个质数. 输入描述: 输入只有一行,包含一个正整数n. 输出描述: 输出只有一行,包含一个正整数p,即较大的那个质数. #include&l ...

  5. Java 中的字符串转为二进制

    /** * 将字符串转为二进制 */ public class StrConversion { public static void main(String args[]) { String str ...

  6. (56)Wangdao.com第八天_JavaScript 流程控制语句

    流程控制语句 条件判断语句 if 条件分支语句 switch 循环语句 for .while switch 和 if 都可以相互转换,switch 的性能更优于 if 1. 条件判断语句 if 在某条 ...

  7. MySQL 详细学习笔记 转

    Windows服务 -- 启动MySQL net start mysql -- 创建Windows服务 sc create mysql binPath= mysqld_bin_path(注意:等号与值 ...

  8. java中String常量的存储原理

    相关题目(运行结果在代码注释后面) 1. package StringTest; public class test1 { public static void main(String[] args) ...

  9. composer相关使用

    #composer安装 curl -sS https://getcomposer.org/installer | php #如果该命令执行不了,通过其他方式下载install文件后再执行“php in ...

  10. 解决配置Windows Update失败问题

    大家都清楚电脑总是需要更新一些补丁,不过,很多系统用户发现更新了补丁之后,开机会出现windows update更新失败的情况,提示“配置Windows Update失败,还原更改,请勿关闭计算机”信 ...