Codeforces Round #287 (Div. 2)

2024-03-20 14:48
文章标签 codeforces round div 287

本文主要是介绍Codeforces Round #287 (Div. 2),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

比赛链接:http://codeforces.com/contest/507


A - Amr and Music
Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

Amr is a young coder who likes music a lot. He always wanted to learn how to play music but he was busy coding so he got an idea.

Amr has n instruments, it takesai days to learni-th instrument. Being busy,

Amr dedicatedk days to learn how to play the maximum possible number of instruments.

Amr asked for your help to distribute his free days between instruments so that he can achieve his goal.

Input

The first line contains two numbers n, k (1 ≤ n ≤ 100,0 ≤ k ≤ 10 000), the number of instruments and number of days respectively.

The second line contains n integersai (1 ≤ ai ≤ 100), representing number of days required to learn thei-th instrument.

Output

In the first line output one integer m representing the maximum number of instruments Amr can learn.

In the second line output m space-separated integers: the indices of instruments to be learnt. You may output indices in any order.

if there are multiple optimal solutions output any. It is not necessary to use all days for studying.

Sample Input

Input
4 10
4 3 1 2
Output
4
1 2 3 4
Input
5 6
4 3 1 1 2
Output
3
1 3 4
Input
1 3
4
Output
0

Hint

In the first test Amr can learn all 4 instruments.

In the second test other possible solutions are: {2, 3, 5} or {3, 4, 5}.

In the third test Amr doesn't have enough time to learn the only presented instrument.


题目大意:n个乐器,k天,每个乐器要学a[i]天,求最多能学几种乐器,输出编号


题目分析:排序


#include <cstdio>
#include <algorithm>
using namespace std;
int const MAX = 1005;
struct Info
{int t, id;
}a[1005];int ans[1005];int cmp(Info a, Info b)
{return a.t < b.t;
}int main()
{int n, k;scanf("%d %d", &n, &k);for(int i = 0; i < n; i++){scanf("%d", &a[i].t);a[i].id = i + 1;}sort(a, a + n, cmp);int sum = 0, cnt = 0;for(int i = 0; i < n; i++){sum += a[i].t;if(sum > k)break;ans[cnt++] = a[i].id;}printf("%d\n", cnt);if(cnt == 0)return 0;for(int i = 0; i < cnt - 1; i++)printf("%d ", ans[i]);printf("%d\n", ans[cnt - 1]);
}



B - Amr and Pins
Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

Amr loves Geometry. One day he came up with a very interesting problem.Amr has a circle of radius r and center in point (x, y).

He wants the circle center to be in new position(x', y').In one step Amr can put a pin to the border of the circle in a certain point,

then rotate the circle around that pin by any angle and finally remove the pin.Help Amr to achieve his goal in minimum number of steps.

Input

Input consists of 5 space-separated integers r, x, y, x'y' (1 ≤ r ≤ 105, - 105 ≤ x, y, x', y' ≤ 105), circle radius,

coordinates of original center of the circle and coordinates of destination center of the circle respectively.

Output

Output a single integer — minimum number of steps required to move the center of the circle to the destination point.

Sample Input

Input
2 0 0 0 4
Output
1
Input
1 1 1 4 4
Output
3
Input
4 5 6 5 6
Output
0

Hint

In the first sample test the optimal way is to put a pin at point(0, 2) and rotate the circle by180 degrees counter-clockwise (or clockwise, no matter).

题目大意:给个圆,输入半径,起始圆心位置和目标圆心位置,每次可以沿圆上任意一点旋转任意角度,问要转几次能从起始位置到目标位置


题目分析:因为是任意位置和任意角度,我们只需要求出两圆心间距离除以圆的直径再向下取正就行了


#include <cstdio>
#include <cmath>double cald(double x1, double y1, double x2, double y2)
{return sqrt((x2 - x1) * (x2 - x1) + (y2 - y1)* (y2 -y1));
}int main()
{double r, x, y, x1, y1;scanf("%lf %lf %lf %lf %lf", &r, &x, &y, &x1, &y1); printf("%d\n", (int)ceil(cald(x, y, x1, y1) / 2 / r));
}



C - Guess Your Way Out!
Time Limit:1000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

Description

Amr bought a new video game "Guess Your Way Out!". The goal of the game is to find an exit from the maze that looks like a perfect binary tree of heighth. The player is initially standing at the root of the tree and the exit from the tree is located at some leaf node.

Let's index all the leaf nodes from the left to the right from 1 to2h. The exit is located at some noden where1 ≤ n ≤ 2h, the player doesn't know where the exit is so he has to guess his way out!

Amr follows simple algorithm to choose the path. Let's consider infinite command string "LRLRLRLRL..." (consisting of alternating characters 'L' and 'R'). Amr sequentially executes the characters of the string using following rules:

  • Character 'L' means "go to the left child of the current node";
  • Character 'R' means "go to the right child of the current node";
  • If the destination node is already visited, Amr skips current command, otherwise he moves to the destination node;
  • If Amr skipped two consecutive commands, he goes back to the parent of the current node before executing next command;
  • If he reached a leaf node that is not the exit, he returns to the parent of the current node;
  • If he reaches an exit, the game is finished.

Now Amr wonders, if he follows this algorithm, how many nodes he is going to visit before reaching the exit?

Input

Input consists of two integers h, n (1 ≤ h ≤ 50,1 ≤ n ≤ 2h).

Output

Output a single integer representing the number of nodes (excluding the exit node) Amr is going to visit before reaching the exit by following this algorithm.

Sample Input

Input
1 2
Output
2
Input
2 3
Output
5
Input
3 6
Output
10
Input
10 1024
Output
2046

Hint

A perfect binary tree of height h is a binary tree consisting of h + 1 levels. Level0 consists of a single node calledroot, level h consists of2h nodes calledleaves. Each node that is not a leaf has exactly two children,left and right one.

Following picture illustrates the sample test number3. Nodes are labeled according to the order of visit.


题目大意:给一棵高度为h的满二叉树,将其叶子结点从1-2^h编号,输入n,n为所要到达的叶子结点。给出指令:LRLRLRL....L表示向左移动,R表示向右移动,若已经到叶子结点,则跳过改次指令,若改结点的左右孩子都被访问过,则跳过改次指令,最后求到达目标叶子结点前要经过几个结点


题目分析:有点像模拟,可是h比较大,直接模拟肯定不行,考虑二分+DFS,DFS中三个参数l,r,dir分别表示叶子的左右位置和方向,方向0表示向左,1表示向右。

若中点小于目标点  1.当前方向向右,则找到一个新结点并往右端搜索,方向向左

                               2.当前方向向左,显然其左侧的子孙都会被访问到,没必要搜索,直接算出来2*(mid-l+1),并往右端搜索

若中点大于目标点  1.当前方向向右,显然其右侧的子孙都会被访问到,没必要搜索,直接算出来2*(r-mid),并往左端搜索

                               2.当前方向向左,则找到一个新结点并往左端搜索,方向向右


#include <cstdio>
#define ll long long
ll n, h;ll DFS(ll l, ll r, int dir)
{if(l == r)return 0;ll mid = (l + r) >> 1;if(mid < n){   if(dir == 1)return 1 + DFS(mid + 1, r, 0);elsereturn 2 * (mid - l + 1) + DFS(mid + 1, r, 0);}else{if(dir == 0)return 1 + DFS(l, mid, 1);elsereturn 2 * (r - mid) + DFS(l, mid, 1);}
}int main()
{scanf("%lld %lld", &h, &n);printf("%lld\n", DFS((ll)1, (ll)1 << h, 0));
}


这篇关于Codeforces Round #287 (Div. 2)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/829812

相关文章

Codeforces Round #240 (Div. 2) E分治算法探究1

Codeforces Round #240 (Div. 2) E  http://codeforces.com/contest/415/problem/E 2^n个数,每次操作将其分成2^q份,对于每一份内部的数进行翻转(逆序),每次操作完后输出操作后新序列的逆序对数。 图一:  划分子问题。 图二: 分而治之,=>  合并 。 图三: 回溯:

Codeforces Round #261 (Div. 2)小记

A  XX注意最后输出满足条件,我也不知道为什么写的这么长。 #define X first#define Y secondvector<pair<int , int> > a ;int can(pair<int , int> c){return -1000 <= c.X && c.X <= 1000&& -1000 <= c.Y && c.Y <= 1000 ;}int m

Codeforces Beta Round #47 C凸包 (最终写法)

题意慢慢看。 typedef long long LL ;int cmp(double x){if(fabs(x) < 1e-8) return 0 ;return x > 0 ? 1 : -1 ;}struct point{double x , y ;point(){}point(double _x , double _y):x(_x) , y(_y){}point op

Codeforces Round #113 (Div. 2) B 判断多边形是否在凸包内

题目点击打开链接 凸多边形A, 多边形B, 判断B是否严格在A内。  注意AB有重点 。  将A,B上的点合在一起求凸包,如果凸包上的点是B的某个点,则B肯定不在A内。 或者说B上的某点在凸包的边上则也说明B不严格在A里面。 这个处理有个巧妙的方法,只需在求凸包的时候, <=  改成< 也就是说凸包一条边上的所有点都重复点都记录在凸包里面了。 另外不能去重点。 int

Codeforces 482B 线段树

求是否存在这样的n个数; m次操作,每次操作就是三个数 l ,r,val          a[l] & a[l+1] &......&a[r] = val 就是区间l---r上的与的值为val 。 也就是意味着区间[L , R] 每个数要执行 | val 操作  最后判断  a[l] & a[l+1] &......&a[r] 是否= val import ja

CSS实现DIV三角形

本文内容收集来自网络 #triangle-up {width: 0;height: 0;border-left: 50px solid transparent;border-right: 50px solid transparent;border-bottom: 100px solid red;} #triangle-down {width: 0;height: 0;bor

创建一个大的DIV,里面的包含两个DIV是可以自由移动

创建一个大的DIV,里面的包含两个DIV是可以自由移动 <body>         <div style="position: relative; background:#DDF8CF;line-height: 50px"> <div style="text-align: center; width: 100%;padding-top: 0px;"><h3>定&nbsp;位&nbsp;

Codeforces Round 971 (Div. 4) (A~G1)

A、B题太简单,不做解释 C 对于 x y 两个方向,每一个方向至少需要 x / k 向上取整的步数,取最大值。 由于 x 方向先移动,假如 x 方向需要的步数多于 y 方向的步数,那么最后 y 方向的那一步就不需要了,答案减 1 代码 #include <iostream>#include <algorithm>#include <vector>#include <string>

CF#271 (Div. 2) D.(dp)

D. Flowers time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/474/problem/D We s

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There