倒水问题


题目描述

There are N cups of water which are numbered from 1 to N. The ith cup contains Ai liters of water, and the magical value of the ith cup is Bi.
The 1st operation will pour B1 liters of water from the 1st cup to the 2nd cup.

The 2nd operation will pour B2 liters of water from the 2nd cup to the 3rd cup.
......


The Nth operation will pour BN liters of water from the Nth cup to the 1st cup.
The (N + 1)th operation will pour B1 liters of water from the 1st cup to the 2nd cup.
......
If the water in the cup is not enough to perform the operation, the game will stop immediately. The question is if this game will keep running forever? If not, how many times will it be operated?

输入描述:

The first line contains an integer T, where T is the number of test cases. T test cases follow. For each test case, the first line contains one integer N, where N is the number of cups.

The second line contains N integers A1, A2, ... , AN, where Ai is the volume of water in the ith cup.
The third line contains N integers B1, B2, ... , BN, where Bi is the magical value of the ith cup.

输出描述:

For each test case, output one line containing “Case #x: y”, where x is the test case number (startingfrom 1). If the game will keep running forever, y is “INF”. Otherwise, y is the operation times.
• 1 ≤ T ≤ 102
• 2 ≤ N ≤ 102
• 0 ≤ Ai ≤ 109
• 1 ≤ Bi ≤ 109.
示例1

输入

2
3
4 5 6
1 3 6
3
2 0 1
1 1 1

输出

Case #1: 7
Case #2: INF

备注:

For the first case, this game’s operation times is 7.
[4,5,6] ⇒ [3,6,6] ⇒ [3,3,9] ⇒ [9,3,3] ⇒ [8,4,3] ⇒ [8,1,6] ⇒ [14,1,0] ⇒ [13,2,0]
For the second case, this game will keep running forever.

    题目很简单,大意就是轮流到水,第一行是中的每个数是每个水杯中的初始水量,下面的那行数字是每次倒出去的水量,问最多能到多少次,最后一个水杯里的水倒到第一个被子中,如果可以无限循环的话输出inf 详情看备注。

思路:如果最小排水量和最大排水量一样那么就是无限循环。然后找出每次进水量和出水量的差值。第一个需要特判 之后的规律一样 另外需要注意maxx的数据范围。


#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdio>
#define  N 0x3f3f3f3f
using namespace std;
int a[110],b[110],c[110],d[110];
int main()
{
	int t;
	cin>>t;
	for(int x=1;x<=t;x++)
	{
		memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
		int n;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>a[i];
		}
		for(int i=1;i<=n;i++)
		{
			cin>>b[i];
			c[i]=b[i];
		}
		int f=0;
		sort(c+1,c+n+1);
		if(c[1]==c[n])
		{
			f=1;
		}
		for(int i=1;i<=n;i++)
		{
			if(i==n)
				d[1]=b[n]-b[1];
			else
			d[i+1]=b[i]-b[i+1];
		}
		long long maxx=N;
		if(d[1]<0)
		{
			maxx=(a[1]-b[1])/(b[1]-b[n])+1;
		}
		int q,w=0;
		for(int i=2;i<=n;i++)
		{
			if(d[i]<0)
			{
				d[i]=-d[i];
				q=(a[i]/d[i]);
				if(maxx>q)
				maxx=q,w=i-1;
				
			}
		}
		if(a[1]<b[1])
		cout<<"Case #"<<x<<": 0"<<endl;
		else if(f==0) cout<<"Case #"<<x<<": "<<maxx*n+w<<endl;
		else cout<<"Case #"<<x<<": INF"<<endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值