Armor layout (two points)

Armor layout

Insert picture description here

Problem solving ideas

Suppose S(i) is the total number
of armors at positions 0~i. Discuss each group of armors by classification.
If S (2 31 -1) is even, there
is no flaw.

Otherwise , the answer is divided into two.

AC code

#include<cstdio>
#include<algorithm>
using namespace std;
int T,n,s[200005],e[200005],d[200005];
int S(int x)//S(i)
{
    
    
	int answer=0;
	for(int i=1;i<=n;i++)
	 if(s[i]<=x)answer+=(min(x,e[i])-s[i])/d[i]+1;
	return answer; 
}
int main()
{
    
    
	scanf("%d",&T);
	while(T--)
	{
    
    
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		 scanf("%d%d%d",&s[i],&e[i],&d[i]);
		if(S(2147483647)%2==0){
    
    printf("There's no weakness.\n");continue;}//特判
		int l=0,r=2147483647;
		while(l<r)//二分答案
		{
    
    
			int mid=(1ll*l+1ll*r)/2;
			if(S(mid)%2==0)l=mid+1;
			else r=mid;
		}
		printf("%d %d\n",l,S(l)-S(l-1));
	}
	return 0;
}

Thank you

Guess you like

Origin blog.csdn.net/weixin_45524309/article/details/112387842