模拟 A - Summer Camp

#include <bits/stdc++.h>

int a[1100];
int b[100];
int len; void init() {
int i = 1, tot = 1;
while (tot <= 1010) {
int t = i, c = 0;
while (t) {
b[c++] = t % 10;
t /= 10;
}
for (int i=c-1; i>=0; --i) {
a[tot++] = b[i];
}
i++;
}
} int main() {
init ();
int n; scanf ("%d", &n);
printf ("%d", a[n]);
return 0;
}

构造 B - Different is Good

题意:问最少改变多少个字母使得该字符串的所有子串不相同

分析:子串有长度为1的,所以如果字符串长度大于26一定不可行,否则就把相同的字母用没出现的字母替换.

#include <bits/stdc++.h>

const int N = 1e5 + 5;
char str[N];
int vis[30]; int main() {
int n; scanf ("%d%s", &n, str);
if (n > 26) {
puts ("-1");
} else {
int ans = 0;
for (int i=0; i<n; ++i) {
if (vis[str[i]-'a']) {
ans++;
} else {
vis[str[i]-'a'] = true;
}
}
printf ("%d\n", ans);
}
return 0;
}

几何+贪心 C - Recycling Bottles

题意:A和B捡罐头,每次只能捡一个然后到垃圾桶去,再从垃圾桶出发去捡,问捡完所有罐头所需的最少总路程

分析:除从A或B出发第一次捡罐头的距离不确定外,其他的都可以看成sum (dis (T, p[i]) *2),所以只需要 min (-dis (T, p[i]) + dis (A, p[i])).因为A和B捡罐头是独立的,只要A和B都捡一个罐头使得差值最小,重复时特判下就行了.更一般的做法是A任意选择一个罐头,B选择除A选的外里最小差值的那一个,可以预处理出前缀最小和后缀最小.

#include <bits/stdc++.h>

const int N = 1e5 + 5;
const double INF = 1e18 + 5;
const double EPS = 1e-8;
struct Point {
double x, y;
Point() {}
Point(double x, double y) {
this->x = x;
this->y = y;
}
}point[N];
Point A, B, O;
double dis[N];
double sum;
double pre[N], suff[N];
double add[N];
int n; Point read_point() {
double x, y; scanf ("%lf%lf", &x, &y);
return Point (x, y);
} double squ(double x) {
return x * x;
} double calc_dis(Point a, Point b) {
return sqrt (squ (a.x - b.x) + squ (a.y - b.y));
} int main() {
A = read_point ();
B = read_point ();
O = read_point ();
scanf ("%d", &n);
for (int i=1; i<=n; ++i) {
point[i] = read_point ();
dis[i] = calc_dis (point[i], O);
pre[i] = suff[i] = -dis[i] + calc_dis (point[i], B);
add[i] = -dis[i] + calc_dis (point[i], A);
sum += dis[i] * 2;
}
pre[0] = suff[n+1] = INF;
for (int i=1; i<=n; ++i) {
pre[i] = std::min (pre[i], pre[i-1]);
}
for (int i=n; i>=1; --i) {
suff[i] = std::min (suff[i], suff[i+1]);
}
double ans = sum + suff[1]; //just B
for (int i=1; i<=n; ++i) {
ans = std::min (ans, sum + std::min (0.0, std::min (pre[i-1], suff[i+1])) + add[i]);
}
printf ("%.12f\n", ans);
return 0;
}

二分 D - Robin Hood

题意:n个人,k次操作,每次把最大的数分1到最小的数,问k次后最大值和最小值的差

分析:二分最小值和最大值,思想是求出min(max) 刚好使得小于它的到它的操作正好是k.处理好二分的边界,因为二分时是尽量使得最小值大,最大值小.

#include <bits/stdc++.h>

typedef long long ll;
const int N = 5e5 + 5;
int a[N];
int n, k; bool check(int val, int op) {
ll need = 0;
for (int i=0; i<n; ++i) {
if (op == 1) {
if (a[i] <= val) {
need += val - a[i];
} else {
break;
}
} else if (op == 2 && a[i] >= val) {
need += a[i] - val;
}
}
return need <= k;
} int main() {
scanf ("%d%d", &n, &k);
ll sum = 0;
for (int i=0; i<n; ++i) {
scanf ("%d", a+i);
sum += a[i];
}
std::sort (a, a+n);
int ansl = -1, ansr = 1e9;
int left = a[0], right = sum / n;
while (left <= right) {
int mid = left + (right - left) / 2;
if (check (mid, 1)) {
ansl = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
left = sum / n;
if (sum % n) {
left++;
}
right = a[n-1];
while (left <= right) {
int mid = left + (right - left) / 2;
if (check (mid, 2)) {
ansr = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
printf ("%d\n", ansr - ansl);
return 0;
}

  

Codeforces Round #352 (Div. 2)的更多相关文章

  1. Codeforces Round #352 (Div. 2) C. Recycling Bottles 暴力+贪心

    题目链接: http://codeforces.com/contest/672/problem/C 题意: 公园里有两个人一个垃圾桶和n个瓶子,现在这两个人需要把所有的瓶子扔进垃圾桶,给出人,垃圾桶, ...

  2. Codeforces Round #352 (Div. 2) D. Robin Hood

    题目链接: http://codeforces.com/contest/672/problem/D 题意: 给你一个数组,每次操作,最大数减一,最小数加一,如果最大数减一之后比最小数加一之后要小,则取 ...

  3. Codeforces Round #352 (Div. 2) D. Robin Hood (二分答案)

    题目链接:http://codeforces.com/contest/672/problem/D 有n个人,k个操作,每个人有a[i]个物品,每次操作把最富的人那里拿一个物品给最穷的人,问你最后贫富差 ...

  4. Codeforces Round #352 (Div. 1) B. Robin Hood 二分

    B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...

  5. Codeforces Round #352 (Div. 1) A. Recycling Bottles 暴力

    A. Recycling Bottles 题目连接: http://www.codeforces.com/contest/671/problem/A Description It was recycl ...

  6. Codeforces Round #352 (Div. 2) B. Different is Good 水题

    B. Different is Good 题目连接: http://www.codeforces.com/contest/672/problem/B Description A wise man to ...

  7. Codeforces Round #352 (Div. 2) A. Summer Camp 水题

    A. Summer Camp 题目连接: http://www.codeforces.com/contest/672/problem/A Description Every year, hundred ...

  8. Codeforces Round #352 (Div. 2) ABCD

    Problems     # Name     A Summer Camp standard input/output 1 s, 256 MB    x3197 B Different is Good ...

  9. Codeforces Round #352 (Div. 2) B - Different is Good

    A wise man told Kerem "Different is good" once, so Kerem wants all things in his life to b ...

随机推荐

  1. 图解JVM字节码执行引擎之栈帧结构

    一.执行引擎      “虚拟机”的概念是相对于“物理机”而言的,这两种“机器”都有执行代码的能力.物理机的执行引擎是直接建立在硬件处理器.物理寄存器.指令集和操作系统层面的:而“虚拟机”的执行引擎是 ...

  2. Linux解压,压缩小总结

    linux下打包与解压的三种命令 最近在读<鸟歌的Linux私房菜基础篇>,想着总结一下所读知识,有益于理解. Linux下常用的命令有三种 gzip,zcat(用于zip,gzip等) ...

  3. HBase中批量修改

    先随便写写..做个随笔记录 使用Rest连接操作Hbase.. 是微软提供的  Microsoft.Hbase.Client 类库.. 版本是0.4.1.0 一直知道   client.StoreCe ...

  4. 大熊君大话NodeJS之 ------ Connect中间件第二季(源码分析)

    一,开篇分析 大家好,大熊君又回来了,今天这篇文章主要是对"Connect"中间件以及相关辅助中间件,做一个源码分析系列,我想上一篇文章大家也看了, 介绍了使用方式及用途,而这篇也 ...

  5. cf723c Polycarp at the Radio

    Polycarp is a music editor at the radio station. He received a playlist for tomorrow, that can be re ...

  6. 手动封装js原生XMLHttprequest异步请求

    Code Object.extend =function(targetObj,fnJson){ //扩展方法,类似于jQuery的$.extend,可以扩展类的方法,也可以合并对象 for(var f ...

  7. 常见HTTP状态码列表

    HTTP状态码 当浏览者访问一个网页时,浏览者的浏览器会向网页所在服务器发出请求.当浏览器接收并显示网页前,此网页所在的服务器会返回一个包含HTTP状态码的信息头(server header)用以响应 ...

  8. 【PHP升级】CentOS6.3编译安装 PHP5.4.38

    先前安装的PHP5.3.28(参考:CentOS6.3编译安装Nginx1.4.7 + MySQL5.5.25a + PHP5.3.28),现在准备升级PHP到5.4.38,有如下几个地方需要重新编译 ...

  9. Alpha版本冲刺总结——曙光初现

    No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 项目预期计划 界面设计 androi ...

  10. myBatis学习笔记

    java.lang.NullPointerException at cn.itcast.mybatis.dao.UserDaoImpl.findUserById(UserDaoImpl.java:22 ...