A. Amr and Music (贪心)

水题,没能秒切,略尴尬。

 #include <cstdio>
#include <algorithm>
using namespace std; const int maxn = +;
int a[maxn], r[maxn], ans[maxn]; int cmp(int i, int j) { return a[i] < a[j]; } int main()
{
//freopen("in.txt", "r", stdin); int n, k;
scanf("%d%d", &n, &k);
for(int i = ; i < n; ++i) r[i] = i;
for(int i = ; i < n; ++i) scanf("%d", &a[i]);
sort(r, r + n, cmp);
int sum = , cnt = ;
for(int i = ; sum + a[r[i]] <= k && cnt < n; ++i)
{
sum += a[r[i]];
ans[cnt++] = r[i];
} printf("%d\n", cnt);
for(int i = ; i < cnt - ; ++i) printf("%d ", ans[i]+);
if(cnt) printf("%d\n", ans[cnt-]+); return ;
}

代码君

B. Amr and Pins (找规律)

题意:

给出一个圆的半径 和 圆心的始末位置。每次操作只能绕圆周上的点旋转若干角度。求该圆从初位置到末位置最少的操作次数。

分析:

不妨设圆的半径为r,圆心在原点处。

先看只旋转一次的情况,圆心所有可能位置构成的区域为  半径为2r的圆(除去圆心那点),也可理解为半径为(0, 2r]的圆环区域

再看第二次旋转,其区域为半径在(2r, 4r]的圆环区域。  //这个不容易画图,自行脑补一下好啦

以此类推。

算法:

所以我们可以先算出圆心始末位置间的距离d,然后根据d和直径2r的关系,最终答案为

 #include <cstdio>
#include <cmath> int main()
{
//freopen("in.txt", "r", stdin); int r, x1, y1, x2, y2, ans;
scanf("%d%d%d%d%d", &r, &x1, &y1, &x2, &y2);
double d = sqrt((long long)(x1-x2)*(x1-x2) + (long long)(y1-y2)*(y1-y2));
ans = (int)ceil(d / 2.0 / r);
printf("%d\n", ans); return ;
}

代码君

C. Guess Your Way Out! (模拟 LCA)

题意:

有一个树高为h的完全二叉树 和 一个目标叶子节点, 给出一种遍历方式(LRLRLR...)

求在到达目标节点时,遍历节点的总数。(不包括目标节点)

具体参见原文Guess Your Way Out!

分析:

以官方题解中的图为例。自己最开始对这道题没什么想法,所以后面的分析相当于题解的翻译。=_=||

从树根开始走到最底端,到达节点X。

如果X刚好是目标节点E,则得到结果为树高h。

否则,找到X和E的 Least Common Ancestor (最小公共祖先)。在右子树遍历之前,一定会遍历完左子树中所有的节点 (图中用红色标出) ,从而退出,进入右子树。

所以在到达右子树之前共遍历sum[h1] + h - h1,其中sum[h1]表示树高为h1的完全二叉树的节点个数。

在进入右子树后,更新树高h、根节点编号、遍历的方向(L or R),重复刚才的过程。

 #include <cstdio>
#include <vector>
#include <algorithm>
using namespace std; typedef long long LL; LL h, n, sum[], ans = ;
vector<LL> anc1; int main()
{
//freopen("in.txt", "r", stdin); for(int i = ; i <= ; ++i) sum[i] = (1LL << (i+)) - ;
scanf("%I64d%I64d", &h, &n);
n += sum[h-]; //转化成整个二叉树中的编号
LL x = n;
anc1.push_back(x); //目标节点的所有祖先
while(x != )
{
x /= ;
anc1.push_back(x);
}
//for(int i = 0; i < anc1.size(); ++i) printf("%I64d ", anc1[i]); LL node = ;
bool choice = false; //遍历防线(L or R) while(node != n)
{
vector<LL> anc2; //当前叶子节点X的祖先
for(int i = ; i < h; ++i)
{
anc2.push_back(node);
if(!choice) node = node * ; else node = node * + ;
choice = !choice;
}
if(n == node) { ans += h; break; }
anc2.push_back(node);
reverse(anc2.begin(), anc2.end());
//for(int i = 0; i < anc2.size(); ++i) printf("%I64d ", anc2[i]); for(int i = ; i <= h; ++i) if(anc1[i] == anc2[i])
{//find LCA
ans += sum[i-] + h - i + ;
h = i - ; //更新树高
node = anc2[i-];
if((anc2[i]<<) == node) //更新根节点的编号 及 遍历方向
{
node = anc2[i] * + ;
choice = false;
}
else
{
node = (anc2[i] << );
choice = true;
}
break;
}
} printf("%I64d\n", ans); return ;
}

代码君

CodeForces Round #287 Div.2的更多相关文章

  1. Codeforces Round #287 (Div. 2) E. Breaking Good 最短路

    题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...

  2. 贪心 Codeforces Round #287 (Div. 2) A. Amr and Music

    题目传送门 /* 贪心水题 */ #include <cstdio> #include <algorithm> #include <iostream> #inclu ...

  3. Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 思路

    C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standa ...

  4. Codeforces Round #287 (Div. 2) C. Guess Your Way Out! 水题

    C. Guess Your Way Out! time limit per test 1 second memory limit per test 256 megabytes input standa ...

  5. Codeforces Round #287 (Div. 2) B. Amr and Pins 水题

    B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  6. Codeforces Round #287 (Div. 2) A. Amr and Music 水题

    A. Amr and Music time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  7. Codeforces Round #287 (Div. 2) E. Breaking Good [Dijkstra 最短路 优先队列]

    传送门 E. Breaking Good time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Codeforces Round #287 (Div. 2) D. The Maths Lecture [数位dp]

    传送门 D. The Maths Lecture time limit per test 1 second memory limit per test 256 megabytes input stan ...

  9. codeforcfes Codeforces Round #287 (Div. 2) B. Amr and Pins

    B. Amr and Pins time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

随机推荐

  1. 统计某一字段等于不同值的个数的sql语句(分享)

    本文介绍下,用一条sql语句统计某一字段等于不同值的个数,方法很独特,有需要的朋友参考下. 表t,数据:  id      type001     1001     0002     1001     ...

  2. PHP中CURL技术模拟登陆抓取网站信息,用与微信公众平台成绩查询

    伴随微信的红火,微信公众平台成为许多开发者的下一个目标.笔者本身对于这种新鲜事物没有如此多的吸引力.但是最近有朋友帮忙开发微信公众平台中一个成绩查询的功能.于是便在空余时间研究了一番. 主要的实现步骤 ...

  3. pdo 连接数据库 报错 could not find driver 解决方法

    在windows 下,调试一个PHP程序时,报了这个错误, could not find driver 原来我的这个程序中用到了PDO对象, 连接mysql 5.  在PHP的默认设置中,只打开了ph ...

  4. cxgrid GridMode 等于 True 时的一些问题。

    When using grid mode, the data controller loads a fixed number of dataset records into memory. The n ...

  5. ISBN

    问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字.1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位 ...

  6. (转)Qt Model/View 学习笔记 (六)——在views中选择数据项

    在views中选择数据项 概念 用于新的view类中的选择模型比Qt3中的模型有了很大的改进.它为基于model/view架构的选择提供了更为全面的描述.尽管对提供了的views来说,负责操纵选择的标 ...

  7. Create a SharePoint Application Page for Anonymous Access

    http://dishasharepointworld.blogspot.com/2011/07/how-to-create-sharepoint-application_1072.html http ...

  8. python学习笔记20(字符串格式化)

    Python中内置有对字符串进行格式化的操作% 模板 格式化字符串时,Python使用一个字符串作为模板.模板中有格式符,这些格式符为真实值预留位置,并说明真实数值应该呈现的格式.Python用一个t ...

  9. python学习笔记17(动态类型)

    动态类型 在我们接触的对象中,有一类特殊的对象,是用于存储数据的,常见的该类对象包括各种数字,字符串,表,词典.在C语言中,我们称这样一些数据结构为变量,而在Python中,这些是对象. 对象是储存在 ...

  10. PAT-乙级-1042. 字符统计(20)

    1042. 字符统计(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 请编写程序,找出一段给定文字中出现最 ...