求助
查看原帖
求助
550775
rsy_楼主2022/11/26 21:54

最后两个点被卡了,TLE,大佬求调。

#include <iostream>
#define LL long long
using namespace std;
LL t, ls, hh = 0;
int main(){
	scanf("%lld", &t);
	while ( t-- ){
		scanf("%lld", &ls);
		ls--;
		LL kk = 1;
		for(LL i = 0; ; i ++ ){
			LL ans1 = i + 2 * i * (i+1);
			LL ans2 = i + 2 * (i+1) * (i+2);
			if(ls >= ans1 && ls < ans2){
				kk = ans1;
				hh = i+1;
				break;
			}
		}
//		cout <<kk <<" "<<hh<<"\n";
		if(ls-kk==0||ls-kk==1){
			cout<<0<<"\n";
		}else{
			LL ans1 = ls - kk-1;
//			cout<<ans1<<"\n";
			if(ans1 <= hh) printf("%lld\n", ans1);
			else if(ans1 <= 2 * hh) printf("%lld\n", hh-(ans1-hh));
			else if(ans1 <= 3 * hh) printf("%lld\n", 0-(ans1%hh));
			else{
				printf("%lld\n", -(hh-ans1%hh));
			} 
		}
	}
	return 0;
}
2022/11/26 21:54
加载中...