[Ybt] [Basic calculation two-point class example 2] Armor arrangement

Armor layout

Topic link: Arrangement of armor


Title description

Insert picture description here
Insert picture description here

Problem solving ideas

First of all, how many armors can be found for each point and the previous point, O (n) O(n)O ( n )

Then, because only one line of defense has a problem, if the current point is an odd number, then the line of defense in question must be before it, and we can divide it based on this. O (log ⁡ n) O(\log{n})O(logn)

Of course, if the 2147483647 21474836472 1 4 7 4 8 3 6 4 The 7 lines of defense are even, so all the lines of defense are fine.

code

#include<iostream>
#include<cstdio>
#define int long long
using namespace std;

int T;
int n;
int s[200010],e[200010],d[200010];

int check(int t)
{
    
    
	int ans=0;
	for(int i=1;i<=n;i++)
		if(s[i]<=t)
			ans+=(min(e[i],t)-s[i])/d[i]+1;
	return ans;
}

void work()
{
    
    
	if(check(2147483647)%2==0)
	{
    
    
		printf("There's no weakness.\n");
		return; 
	}
	int l=0,r=2147483647;
	while(l<r)
	{
    
    
		int mid=(l+r)/2;
		if(check(mid)%2==1)
			r=mid;
		else
			l=mid+1;
	}
	cout<<l<<" "<<check(l)-check(l-1)<<endl;
}

signed main()
{
    
    
	cin>>T;
	while(T--)
	{
    
    
		cin>>n;
		for(int i=1;i<=n;i++)
			scanf("%lld%lld%lld",&s[i],&e[i],&d[i]);
		work();
	}
}

Guess you like

Origin blog.csdn.net/SSL_guyixin/article/details/111752509