题目描述
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 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.
输出描述:
输入
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;
}