[YBT high-efficiency advanced] 1 basic algorithm/3 bisection algorithm/2 armor arrangement

[YBT high-efficiency advanced] 1 basic algorithm/3 bisection algorithm/2 armor arrangement

Memory limit: 64 MiB
Time limit: 1000 ms
standard input and output
Question type: Traditional
Evaluation method: Text comparison

Title description

Now there are N groups of armors. We can think that the line of defense is one-dimensional, so each group of armor is distributed on a certain section of the line of defense, and the same group of armor is arranged at equal distances. In other words, we can use three integers S, E, D to describe a group of armor, that is, this group of armor is arranged on the line of defense S, S+D, S+2D,......,S+kD(k<= z,S+kD<=E,S+(k+1)D>E) position. If the number of armors in a position is odd, we call this position a flaw, but there is one and only one position on the entire defense line with flaws or no flaws at all. Please find the location of the vulnerability, or make sure that the line of defense is not flawed.

Input format

The first line is an integer T, indicating that there are T groups of independent test data.

The first row of each group of data is an integer N.

In the next N lines, each line contains three integers S i E i , D i represents the three parameters of the i-th armor set, and the data is separated by spaces.

Output format

For each set of test data, if the line of defense is not flawed, output a line of "There's no weakness.".

Otherwise, output two space-separated integers P and C in one line, indicating that there are C armors at position P. Of course C should be an odd number.

Sample

Sample input

3
2
1 10 1
2 10 1
2
1 10 1 
1 10 1 
4
1 10 1 
4 4 1 
1 5 1 
6 10 1

Sample output

1 1
There's no weakness.
4 3

Data range and tips

For 100% data 1<=T<+5,1<=n<=2*10 5 ,0<=s i ,e i ,d i <2 31 The total number of armors is not more than 10 8 .

Ideas

Let sum(i) denote the total number of armors at positions 0~i.
If sum(2^31-1), there is no flaw.
If mid is odd: ans<=mid;r=mid;
if mid is even: ans>mid;l=mid+1;

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int s[200010],e[200010],d[200010],n;
int sum(int x)
{
    
    
	int ans=0,i;
	for(i=1;i<=n;i++)
		if(s[i]<=x)
			ans+=(min(x,e[i])-s[i])/d[i]+1;
	return ans;
}
int main()
{
    
    
	long long T,i,l,r,mid;
	ios::sync_with_stdio(false);
	for(cin>>T;T;T--)
	{
    
    
		for(cin>>n,i=1;i<=n;i++)
			cin>>s[i]>>e[i]>>d[i];
		if(!(sum(2147483647)&1))cout<<"There's no weakness.\n";
		else
		{
    
    
			for(l=0,r=2147483647;l<r;)//二分查找 
			{
    
    
				mid=(long long)(l+r)>>1;
				if(sum(mid)&1) r=mid;
				else l=mid+1;
			}
			cout<<l<<' '<<sum(l)-sum(l-1)<<endl;
		}
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46975572/article/details/115099494