HDU 4768 Flyer two points set two points

Title:

Give N groups of numbers, each group of three numbers A_i, B_i, C_i, representing the number of K
A_i, A_i+C_i, A_i+2 C_i,...A_i+k C_i
(A_i+k*C_i<=B_i, A_i+(k+ 1)*C_i>B_i).) is actually an arithmetic sequence. Find the number with an odd number of occurrences and the number of occurrences in the N sequence. The data in the question ensure that there is only one number

Ideas:

Because the data guarantees that there is only one number, if it exists, the sum of all occurrences of the number must be an odd number. If this is an even number, you can directly output the answer. Otherwise, we dichotomize the answer. What if we check? If the sum of the number of occurrences (all less than or equal to mid) is an odd number, it means that the answer is on the left side of mid and execute R=mid-1; if it is an even number, it means that the answer is on the right side of mid, then execute L=mid+1. There are many details in the code to pay attention to!

#pragma GCC optimize(2)
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>

using namespace std;

typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
#define pi acos(-1.0)
#define e exp(1.0)
#define pb push_back
#define mk make_pair
#define fir first
#define sec second
#define scf scanf
#define prf printf
typedef pair<ll,ll> pa;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll MAX_N=2e4+7;
ll N,A[MAX_N],B[MAX_N],C[MAX_N];
ll tmp;
bool check(ll nmd){
    
    
	ll i,j,cnt=0;
	tmp=0;
	for(i=1;i<=N;i++){
    
    
		ll tmd=min(B[i],nmd);//保证数不能越界,数值最大才B[i]
		if(tmd<A[i])//小于起始值了,跳过
		continue;
		cnt+=((tmd-A[i])/C[i]+1);
	}
	if(cnt&1)
	return 1;
	return 0;
}
int main()
{
    
    
//  freopen(".../.txt","w",stdout);
//  freopen(".../.txt","r",stdin);
//	ios::sync_with_stdio(false);
	ll i,j,k;
	while(~scf("%lld",&N)){
    
    
		ll cnt=0,L=INF,R=0,mid; 
		for(i=1;i<=N;i++){
    
    
			scf("%lld %lld %lld",&A[i],&B[i],&C[i]);
			if(B[i]>=A[i]){
    
    
				cnt+=((B[i]-A[i])/C[i]+1);
				R=max(R,B[i]);
				L=min(L,A[i]);				
			} 
		}
		if(!(cnt&1)){
    
    
			prf("DC Qiang is unhappy.\n");
			continue;
		}
		ll res,res1;
		while(L<=R){
    
    
			mid=L+(R-L)/2;
			if(check(mid)){
    
    //左边的区间是奇数 
				res=mid;					
				R=mid-1;
			}
			else{
    
    
				L=mid+1;
			}
		}
		//求出现次数
		res1=0;
		for(i=1;i<=N;i++){
    
    
			if(res>=A[i]&&res<=B[i]){
    
    
				if((res-A[i])%C[i]==0)
				res1++;
			}
		}
		prf("%lld %lld\n",res,res1);
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43311695/article/details/108564783