突然发现贼有用的lower-bound()与K - Downgrade

本文介绍了一个关于RPG游戏等级系统的问题,当玩家不玩游戏时,其等级会按照特定规则下降。游戏中的等级由主级和次级组成,主级之间有不同的次级等级数量。在连续缺席的日子里,玩家的等级会先转换为主级的次级等级,然后逐步降低。文章给出了一段C++代码,用于计算玩家返回游戏时的等级。主要涉及二分查找算法在排序数组中的应用。
摘要由CSDN通过智能技术生成

lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的。

在从小到大的排序数组中,

lower_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num):从数组的begin位置到end-1位置二分查找第一个大于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

在从大到小的排序数组中,重载lower_bound()和upper_bound()

lower_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于或等于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

upper_bound( begin,end,num,greater() ):从数组的begin位置到end-1位置二分查找第一个小于num的数字,找到返回该数字的地址,不存在则返回end。通过返回的地址减去起始地址begin,得到找到数字在数组中的下标。

例题
K - Downgrade
The God of Sheep plays an RPG game recently. The game has a level system that records a player’s level with a pair of positive integers A and B where A is the major level and B is the minor level. For each major level k, you have to reach minor level Lk in order to upgrade to the next level k + 1. Different major levels may have different number of minor levels. For example, the God of Sheep is currently on level 3-4, and level 3 contains 4 minor levels: 3-1, 3-2, 3-3, and 3-4. Once the God of Sheep upgrades, he will be on level 4-1.

The God of Sheep has work to do now. Countless sheep are suffering in slaughter houses, woolsheds, and dairy farms. The God of Sheep has to rescue his fellow sheep citizens. He will be busy with the rescue plan for a couple of days and meanwhile the level of the game will be downgraded each day as a penalty. The downgrade penalty works like this: with each day the God of Sheep not playing the game, his level A-B will firstly be transformed to A minor levels (with minor level B discarded), and then be cast to an equivalent - pair as his new level. For example, suppose both level 1 and level 2 have 2 minor levels, and the God of Sheep is currently on level 3-3. For the first day of his absence, the level will become 2-1, the second day the level will become 1-2, the third day the level will become 1-1, and from the third day on the level will keep the same level as 1-1.

The God of Sheep wonders which level will he be when he comes back from the rescue?
Input

The first line of the input gives the number of test cases, T. T test cases follow.

Each test case contains two lines. The first line contains three integers: A, B and N, indicating the current major level, minor level, and the number of days the God of Sheep will be away from the game. The next line contains A integers: L1, L2, …, LA indicating the number of minor levels in each major level.

1 ≤ T ≤ 20.
1 ≤ A ≤ 105.
1 ≤ B ≤ LA.
1 ≤ Li, N ≤ 109.

Output

For each test case, output one line containing “Case #x: y-z”, where x is the test case number (starting from 1), y and z are the major level and the minor level when the God of Sheep is back to the game.

#include <queue>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 2e5 + 7;
const int mod = 1000000007;
typedef long long ll;

ll sum[maxn];
ll l[maxn];

int main()
{
	int T;scanf("%d",&T);
	int kase = 0;
	while(T--)
	{
		ll a,b,n;scanf("%lld%lld%lld",&a,&b,&n);
		for(int i = 1;i <= a;i++)
		{
			scanf("%lld",&l[i]);
			sum[i] = sum[i - 1] + l[i];
		}
		ll nowa = a,nowb = b;
		for(int i = 1;i <= n;i++)
		{
			ll tmpa = lower_bound(sum + 1,sum + 1 + a,nowa) - sum;
			ll tmpb = nowa - sum[tmpa - 1];
			
			if(tmpb == 0)
				tmpa--,tmpb = l[tmpa];

			if(tmpa == nowa && tmpb == nowb)break;
			nowa = tmpa,nowb = tmpb;
		}
		printf("Case #%d: ",++kase);
		printf("%lld-%lld\n",nowa,nowb);
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值