[BZOJ1583] [Usaco2009 Mar]Moon Mooing 哞哞叫(队列)

传送门问题解法
本文提供了两种解决传送门问题的方法,一种使用双队列维护两个序列的最小值和最大值,另一种采用单队列的方式,通过不断更新队列元素来生成整个数列,并输出第n个元素。

传送门

 

思想有点像蚯蚓那个题

#include <cstdio>
#define N 4000001
#define LL long long
#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) > (y) ? (x) : (y))

LL q1[N], q2[N];
LL a1, b1, d1, a2, b2, d2;
int n, h1 = 1, t1 = 1, h2 = 1, t2;

int main()
{
	int i;
	LL x, x1, x2;
	scanf("%lld %d", &q1[1], &n);
	scanf("%lld %lld %lld", &a1, &b1, &d1);
	scanf("%lld %lld %lld", &a2, &b2, &d2);
	while(n--)
	{
		x = ~(1 << 31);
		x <<= 32;
		if(h1 <= t1) x = min(x, q1[h1]);
		if(h2 <= t2) x = min(x, q2[h2]);
		if(h1 <= t1 && x == q1[h1]) h1++;
		if(h2 <= t2 && x == q2[h2]) h2++;
		x1 = a1 * x / d1 + b1;
		x2 = a2 * x / d2 + b2;
		if(min(x1, x2) > q1[t1]) q1[++t1] = min(x1, x2);
		if(max(x1, x2) > q2[t2]) q2[++t2] = max(x1, x2);
		else if(max(x1, x2) > q1[t1]) q1[++t1] = max(x1, x2);
	}
	printf("%lld\n", x);
	return 0;
}

  

来自洛谷的更简便的题解

#include <cstdio>
#include <iostream>
typedef unsigned long long ull; //注意本题要用unsigned long long。
const int N = 4000000; 
ull a[N+5]; //这里我们将a数组看成一个队列
inline ull mn(ull x, ull y) {
    return x < y ? x : y;
}
int main() {
    int c, n, a1, b1, d1, a2, b2, d2, i = 1, f1 = 1, f2 = 1;
    scanf("%d%d%d%d%d%d%d%d", &c, &n, &a1, &b1, &d1, &a2, &b2, &d2);
    a[i++] = c; //初始元素c入队
    while(i <= N) {
        ull x = mn(a1*a[f1]/d1+b1, a2*a[f2]/d2+b2); //取F1()和F2()中的较小值
        a[i++] = x; //将该较小值入队
        if(x == a1*a[f1]/d1+b1) f1++; //如果较小值来自F1(),则将F1()的指针f1+1。
        if(x == a2*a[f2]/d2+b2) f2++; //如果较小值来自F2(),则将F2()的指针f2+1
    } //重点理解while循环的内容。因为算出的F1()或F2()的数据单调递增,所以可以用这样的方式生成整个数列
    std::cout << a[n]; //最后输出即可。
    return 0;
}
//祝各位早日AC此题!

  

转载于:https://www.cnblogs.com/zhenghaotian/p/7513348.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值