Codeforces Round #287 (Div. 2)

54 篇文章 0 订阅
53 篇文章 0 订阅

比赛链接: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);
        else
            return 2 * (mid - l + 1) + DFS(mid + 1, r, 0);
    }
    else
    {
        if(dir == 0)
            return 1 + DFS(l, mid, 1);
        else
            return 2 * (r - mid) + DFS(l, mid, 1);

    }
}

int main()
{
    scanf("%lld %lld", &h, &n);
    printf("%lld\n", DFS((ll)1, (ll)1 << h, 0));
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值