[分治] 分治入門

題單
本菜雞帶着大家複習分治

T1 一元三次方程

菜題,但是卡了我很久
精度問題好坑😭

#include<bits/stdc++.h>
using namespace std;

const double eps=1e-7;
double a,b,c,d;
double l,r,mid;
double ans[100];
int s;

inline double f(double x){
	return a*x*x*x+b*x*x+c*x+d;
}

int main(){
	scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
	for(int i=-100;i<100;++i){
		if(fabs(f(i))<eps){
			if(fabs(ans[s]-i)<eps) continue;
			printf("%.2lf ",(double)i);
			ans[s++]=(double)i;
			continue;
		}
		else if(f(i)*f(i+1)<0){
			l=i,r=i+1;
			while(r-l>eps){
				mid=(l+r)/2;
				if(f(l)*f(mid)<=0) r=mid;
				else l=mid;
			}
			if(fabs(ans[s]-l)<eps) continue;
			printf("%.2lf ",l);
			ans[++s]=l;
		}
		if(!(s^3))break;
	}
	return 0;
}

T2 冪次方

遞歸
字符串

#include<bits/stdc++.h>
#define in Read()
using namespace std;
inline int in{
	int i=0,f=1;char ch;
	while(!isdigit(ch)&&ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))i=(i<<1)+(i<<3)+ch-48,ch=getchar();
	return i*f;
}

inline string print(int x){
	if(!x)return string("0");
	string s=string("");
	int i=0;
	do{
		if(x&1)
			s=((i==1)?"2":"2("+print(i)+")")+((s=="")?"":"+")+s;
	}
	while(++i&&(x>>=1));
	return s;
}

int main(){
	cout<<print(in);
	return 0;
}

T3 煩惱的高考志願

T4 逆序對

晚上15min寫完
注意要離散化,要long long

#include<bits/stdc++.h>
#define in Read()
#define int long long
using namespace std;
inline int in{
	int i=0,f=1;char ch;
	while(!isdigit(ch)&&ch!='-')ch=getchar();
	if(ch=='-')ch=getchar(),f=-1;
	while(isdigit(ch))i=(i<<1)+(i<<3)+ch-48,ch=getchar();
	return i*f;
}

const int NNN=5e5+10;
int n,ans;
int tree[NNN];
int a[NNN],b[NNN];

inline int lowbit(int x){return x&(-x);}

inline void update(int x){
	while(x<=n){
		tree[x]+=1;
		x+=lowbit(x);
	}
	return ;
}

inline int query(int x){
	int res=0;
	while(x){
		res+=tree[x];
		x-=lowbit(x);
	}
	return res;
}

signed main(){
	n=in;
	
	for(int i=1;i<=n;++i) b[i]=a[i]=in;
	sort(b+1,b+n+1);
	int tot=unique(b+1,b+n+1)-b-1;
	
	for(int i=1;i<=n;++i){
		int x=lower_bound(b+1,b+tot+1,a[i])-b;
		update(x);
		ans+=i-query(x);
	}
	printf("%lld\n",ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章