#include <string>
#include <vector>
using namespace std;
int asdf;
struct list{
int time;
int last;
int next;
list() : time(0), last(0), next(0) {}
list(int c1, int c2, int c3) : time(c1), last(c2), next(c3) {}
};
typedef struct list list;
struct data_{
int d;
int less;
int more;
int last;
int next;
int p;
int h;
data_() : d(0), less(0), more(0), last(0), next(0), p(0), h(1) {}
data_(int c1, int c2, int c3, int c4, int c5, int c6) : d(c1), less(c2), more(c3), last(c4), next(c5), p(c6), h(1) {}
};
typedef struct data_ data_;
//times 데이터 정렬 벡터
vector<data_> time;
int seed;
void UpdateTimeH(int i){
int lastH=0;
int mh=0;
int lh=0;
if(time[i].more!=-1) mh=time[time[i].more].h;
if(time[i].less!=-1) lh=time[time[i].less].h;
if(mh>lh){
time[i].h=mh+1;
return;
}
time[i].h=lh+1;
}
void UpdateTimeSpin(int i, int type){//type : 1 -> 반시계 type : 2 -> 시계
int save_=-1;
int ii;
if(type==1){
ii=time[i].more;
if(time[ii].less!=-1){
save_=time[ii].less;
}
time[ii].less=i;
time[i].more=save_;
UpdateTimeH(i);
UpdateTimeH(ii);
}
if(type==2){
ii=time[i].less;
if(time[ii].more!=-1){
save_=time[ii].more;
}
time[ii].more=i;
time[i].less=save_;
UpdateTimeH(i);
UpdateTimeH(ii);
}
if(time[i].p!=-1 && time[time[i].p].less==i){
time[time[i].p].less=ii;
}
if(time[i].p!=-1 && time[time[i].p].more==i){
time[time[i].p].more=ii;
}
time[ii].p=time[i].p;
time[i].p=ii;
if(seed==i) seed=ii;
}
void UpdateTimeE(int i){
int ch_=(time[i].less!=-1 ? time[time[i].less].h : 0)-(time[i].more!=-1 ? time[time[i].more].h : 0);
if(ch_>1)
UpdateTimeSpin(i, 2);
if(ch_<-1)
UpdateTimeSpin(i, 1);
UpdateTimeH(i);
}
void UpdateTime(int num){
int i = seed;//확인 중인 값
int ml=0;//1 -> more, 2 -> less
vector<int> history(0,0);
history.push_back(i);
do{
if(ml==1) i=time[i].more;
if(ml==2) i=time[i].less;
if(time[i].d > num){
ml=2;
if(time[i].less==-1){
break;
}
history.push_back(time[i].less);
}
else{
ml=1;
if(time[i].more==-1){
break;
}
history.push_back(time[i].more);
}
}while(time[i].more!=-1 || time[i].less!=-1);//다음 and 이전 값이 있는 경우
int newc=time.size();
if(ml==1){
time.push_back({num, -1, -1, i, time[i].next, i});
time[i].more=newc;
time[i].next=newc;
if(time[i].next!=-1) time[time[newc].next].last=newc;
}else if(ml==2){
time.push_back({num, -1, -1, time[i].last, i, i});
time[i].less=newc;
time[i].last=newc;
if(time[i].last!=-1) time[time[newc].last].next=newc;
}
for(i=history.size()-1;i>=0;i--){
UpdateTimeE(history[i]);
}
}
long long solution(int n, vector<int> times) {
asdf=0;
//데이터 정렬
time.push_back({times[0], -1, -1, -1, -1, -1});
for(int i=1, seed=0;i<times.size();i++){//i<times.size()
UpdateTime(times[i]);
}
long long answer = 0;
unsigned long long sum=1;
unsigned long long newsum, newsum2[3];
list list1;
for(int i=0; i<time.size(); i++){
newsum2[0]=sum%time[i].d;
newsum2[1]=time[i].d;
while(newsum2[0]){
newsum2[2]=newsum2[1]%newsum2[0];
newsum2[1]=newsum2[0];
newsum2[0]=newsum2[2];
}
sum=sum*time[i].d/newsum2[1];
}
return sum; //공배수 까지 완료
int a[100000];
for(int i=0; i<time.size(); i++){
a[i]=sum/time[i].d;
newsum+=a[i];
}
if(n>newsum){
n-=newsum;
}
else if(n<newsum){
}
else if(n==newsum){
return newsum*(n/time[time.size()-1].d);
}
return answer;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: