Codeforces Gym 100500F Problem F. Door Lock 二分

本文介绍了一个来自CodeForces的编程挑战问题——门锁谜题。该问题要求根据给定的两个整数n和m构建多米诺骨牌集合,并按特定规则排序以获取第m块骨牌的值。文章提供了问题背景、输入输出说明、示例及解决方案,通过二分查找算法高效解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem F. Door Lock
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/gym/100500/attachments

Description

It was a beautiful night at Yekaterinburg, the sky was crystal clear with no clouds, and the view of the moon and the stars was magnificent. Coach Fegla was enjoying this view at the Novotel hotel with Hanaa El-Jazzar (a member of the systems operation team of the ACPC). It was the first time for Hanaa to attend the ICPC world finals event, and she was so happy about it and brought coach Fegla a gift from her hometown Lebanon as a sign of gratitude. The gift was a round shaped plate with engraved images of different sightseeing in Lebanon. While Hanaa was showing the present to the coach, two drunk fellows entered the room by mistake. They were so high they could not distinguish their room, from the coach’s room. Coach Fegla thought what if the ones who entered the room were complete strangers, what should he do? He decided to install another lock to his door, but it was not like any other lock. To obtain the key of the lock a certain puzzle has to be solved, this way coach Fegla might be able to stop most of the intruders. The puzzle was as follows, you will be given 2 numbers n and m. You should build up a domino set. Each piece of the set will contain 2 integer numbers in the range from 0 to n-1, and all the pieces are pairwise distinct and no two pieces of domino should look the same if rotated. As you know a domino piece is divided into 2 halves where the top half contains one integer and other half contains the other integer. You should lay down the pieces in a straight line where the top half of each piece should contain the greatest value of the 2 halves. Then you should sort the domino pieces in increasing order based on the top half, in case of equality sort them in increasing order based on the other half. The lock to the key will be the mth piece in this sequence. You will be given n, m can you get lock key? (check the image below for a domino set where n = 3)

Input

The first line will be the number of test cases T. Each of the following lines will contain 2 numbers n, m. 1 ≤ T ≤ 100,000 1 ≤ n ≤ 1,000,000,000 1 ≤ m ≤ n×(n+1) 2

Output

For each test case print a single line containing: Case_x:_a_b x is the case number starting from 1. a is the greatest value on the domino piece, and b is the other value. Replace underscores with spaces.

Sample Input

2 2 1 3 5

Sample Output

Case 1: 0 0 Case 2: 2 1

HINT

 

题意

让你用两个数来记录这个数的大小,大小记录规则由题目给出

题解

简单分析之后,发现这其实是一个前缀和的东西,所以我们直接二分就好了

代码

#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
const int maxn=202501;
#define mod 1000000007
#define eps 1e-9
const int inf=0x3f3f3f3f;
const ll infll = 0x3f3f3f3f3f3f3f3fLL;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
//*************************************************************************************

int main()
{
    int t=read();
    for(int cas=1;cas<=t;cas++)
    {
        ll n,m;
        n=read(),m=read();
        if(m==1)
        {
            printf("Case %d: 0 0\n",cas);
            continue;
        }
        ll l=0,r=n;
        while(l<r)
        {
            ll mid=(l+r)/2;
            if((mid*mid+mid)/2>m)
                r=mid;
            else if((mid*mid+mid)/2==m)
                l=r=mid;
            else
                l=mid+1;
        }
        l--,m--;
        printf("Case %d: %lld %lld\n",cas,l,m-(l*l+l)/2);
    }
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4681010.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值