3.18

https://www.cnblogs.com/noblex/p/9197896.html

相當於廢話了

函數原型:int printf (char * format,args,···); 

功能:按format指向的格式字符串所規定的格式,將輸出表列args的值輸出到標準輸出設備。

例題:

輸入i=56479854987; 

輸出最終結果就是56479854987112; 

因爲56479854987是11個字符,11是兩個字符;

https://www.cnblogs.com/xingyunblog/p/3657580.html

printf返回值瞭解一下

  題目鏈接   http://acm.hdu.edu.cn/showproblem.php?pid=1166

離散化是程序設計中一個常用的技巧,它可以有效的降低時間複雜度。其基本思想就是在衆多可能的情況中,只考慮需要用的值。離散化可以改進一個低效的算法,甚至實現根本不可能實現的算法。要掌握這個思想,必須從大量的題目中理解此方法的特點。例如,在建造線段樹空間不夠的情況下,可以考慮離散化。

思路是:先排序,再刪除重複元素,最後就是索引元素離散化後對應的值。

假定待離散化的序列爲a[n],b[n]是序列a[n]的一個副本,則對應以上三步爲:

sort(sub_a,sub_a+n);

int size=unique(sub_a,sub_a+n)-sub_a;//size爲離散化後元素個數

for(i=0;i<n;i++)

a[i]=lower_bound(sub_a,sub_a+size,a[i])-sub_a + 1;//k爲b[i]經離散化後對應的值

對於第3步,若離散化後序列爲0,1,2,...,size - 1則用lower_bound,從1,2,3,...,size則用upper_bound。其中lower_bound返回第1個不小於b[i]的值的指針,而upper_bound返回第1個大於b[i]的值的指針,當然在這個題中也可以用lower_bound然後再加1得到與upper_bound相同結果,兩者都是針對以排好序列。使用STL離散化大大減少了代碼量且結構相當清晰。

樹狀數組竟然可以求逆序對,之前倒是不清楚

https://blog.csdn.net/SSimpLe_Y/article/details/53744096

#include<bits/stdc++.h>
using namespace std;
struct node{
	int v,pos;
	
}nod[100010];int n;
int tree[100010];
void update(int sum){
	for(int i=sum;i<=n;i+=(i&(-i))){
		tree[i]+=1;
	}
	return;
}
int cmp(node a,node b){
	return a.v<b.v;
}
int getsum(int x){
	int sum=0;
	for(int i=x;i;i-=(i&(-i))){
		sum+=tree[i];
	}
	return sum;
}
int main(){

	cin>>n;int ans=0;
	for(int i=1;i<=n;i++){
		cin>>nod[i].v;
		nod[i].pos=i;
	}
	sort(nod+1,nod+n+1,cmp);//排序 
	for(int i=1;i<=n;i++){
		update(nod[i].pos);
		ans+=getsum(nod[i].pos);
	}
	cout<<ans;
	return 0;
} 

樹狀數組求區間最大值

hdu1754

#include<bits/stdc++.h>
using namespace std;
int h[300010];
int a[300010];int n,m; 
#define ll long long
int lowbit(int x){
	return (x&(-x));
}
void update(int x){
int ans=0;
	while(x<=n){
		h[x]=a[x];
		ans=lowbit(x);
		for(int i=1;i<ans;i<<=1){
			h[x]=max(h[x],h[x-i]);
		}
		x+=lowbit(x);
	}
	return;
}
int query(int x,int y){
	int ans=0;
	while(x<=y){
		ans=max(a[y],ans);
		y--;
		for(;y-lowbit(y)>=x;y-=lowbit(y))//
		ans=max(ans,h[y]);
	}
	return ans;
}
int main(){
	while((scanf("%d%d",&n,&m)!=EOF)){
		for(int i=1;i<=n;i++)
		h[i]=0;
		for(int i=1;i<=n;i++)
		scanf("%d",&a[i]),update(i);
		char ch;
		while(m--){
			scanf("%c",&ch);int x,y;
			scanf("%c",&ch);//空格 
			if(ch=='Q') {
				cin>>x>>y;
				int ans=query(x,y);
				printf("%d\n",ans);
			}
			else if(ch=='U'){//小心空格 
				cin>>x>>y;
				a[x]=y;//賦值 
				update(x);
			}
		}
		
	}
	return 0;
}

二維樹狀數組我不是很懂,畢竟沒題目實踐

一維拓展到二維,樹狀數組和差分數組

樹狀數組的參考博客

https://blog.csdn.net/bestsort/article/details/80796531#%E6%A0%91%E7%8A%B6%E6%95%B0%E7%BB%84%E5%9F%BA%E7%A1%80

看完了後,幾分鐘內完成了一道模板題,還是要有針對性地做,同時,要足量。

題1:https://www.luogu.org/problemnew/show/P3374

#include<bits/stdc++.h>
using namespace std;
int a[500005];int n;int maxx=-1e9;
int lowbit(int x){
	return (x&(-x));
}
void add(int i,int x){
	for(int j=i;j<=n;j+=lowbit(j)){
		a[j]+=x;
	}
	return;
}
int query(int x){
	int ans=0;
	for(int i=x;i;i-=lowbit(i)){
		ans+=a[i];
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	int m;
	
	cin>>n>>m;int x;int y,z;
	for(int i=1;i<=n;i++){
		cin>>x;
		maxx=max(maxx,x);
		add(i,x);
	}
	while(m--){
		cin>>x>>y>>z;
		if(x==1){
			add(y,z);
			
		}
		else {
			cout<<query(z)-query(y-1)<<endl;
		}
		
	}
	return 0;
} 

題2:https://www.luogu.org/problemnew/show/P3368

#include<bits/stdc++.h>
using namespace std;
int b[500005];
int a[500005];int n;int maxx=-1e9;
int lowbit(int x){
	return (x&(-x));
}
void add(int i,int x){
	for(int j=i;j<=n;j+=lowbit(j)){
		a[j]+=x;
	}
	return;
}
int query(int x){
	int ans=0;
	for(int i=x;i;i-=lowbit(i)){
		ans+=a[i];
	}
	return ans;
}
int main(){
	ios::sync_with_stdio(false);
	int m;
	
	cin>>n>>m;int x;int y,z;int k;
	for(int i=1;i<=n;i++){
		cin>>b[i];
		
	}
	for(int i=1;i<=n;i++){
		int xx=b[i]-b[i-1];//細節問題 
		add(i,xx);
	}
	while(m--){
		cin>>x>>y;
		if(x==1){
			cin>>z>>k;
			add(y,k);
			add(z+1,-k);
			
		}
		else {
			cout<<query(y)<<endl;
			
		}
		
	}
	return 0;
} 

每天根據那個ACM修煉指南看一個知識點,對應做兩道題吧。在之前的計劃基礎上。

線段樹:

需要注意的是如果是n個數,那麼線段樹需要開4n的空間.理論上是2n-1的空間,但是你遞歸建立的時候當前節點爲r,那麼左右孩子分別是2*r,2*r+1,此時編譯器並不知道遞歸已結束,因爲你的結束條件是在遞歸之前的,所以編譯器會認爲下標訪問出錯,也就是空間開小了,應該再開大2倍。有時候可能你發現開2,3倍的空間也可以AC,那只是因爲測試數據並沒有那麼大。
原文:https://blog.csdn.net/bestsort/article/details/80815548 

總是忘。

先是對二叉樹的回顧

先序+後序:不唯一確定,因爲根節點單孩子情況下,先序和後序並無分別。

任何結點只有左子樹的二叉樹和任何結點只有右子樹的二叉樹,其前序序列相同,後序序列相同,但卻是兩棵不同的二叉樹。
void preorder(tree *p){
	if(p==null){
		return;
	}
	visit(p->data);
	preorder(p->lchild);
	preorder(p->rchild);
} 

上課的時候做了一下cf,結果還有20分鐘的時候,中間走來走去,反正老師不允許我上課用電腦了。

打算一旦不想寫作業,想自暴自棄的時候就要打cf,剩了好多時間,以前總是無所事事。

C. Balanced Team

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are a coach at your local university. There are nn students under your supervision, the programming skill of the ii-th student is aiai.

You have to create a team for a new programming competition. As you know, the more students some team has the more probable its victory is! So you have to create a team with the maximum number of students. But you also know that a team should be balanced. It means that the programming skill of each pair of students in a created team should differ by no more than 55.

Your task is to report the maximum possible number of students in a balanced team.

Input

The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of students.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is a programming skill of the ii-th student.

Output

Print one integer — the maximum possible number of students in a balanced team.

Examples

input

Copy

6
1 10 17 12 15 2

output

Copy

3

input

Copy

10
1337 1337 1337 1337 1337 1337 1337 1337 1337 1337

output

Copy

10

input

Copy

6
1 1000 10000 10 100 1000000000

output

Copy

1

Note

In the first example you can create a team with skills [12,17,15][12,17,15].

In the second example you can take all students in a team because their programming skills are equal.

In the third example you can create a team consisting of a single student (and you cannot create a team consisting of at least two students).

#include<bits/stdc++.h>
using namespace std;
int a[200020];
int arr[200020];int maxx=0;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		
	}
	sort(a+1,a+n+1);int j=1;
	for(int i=1;i<=n;i++){
		for(;j<=n;j++){
			if((a[j]-a[i])<=5){
				
				
				 maxx=max(maxx,j-i+1);
				
			}
			else {
			//	maxx=max(maxx,x);
				break;
			}
		//	if((x==n-i+1)) {
		//		maxx=max(maxx,x);
			//	cout<<maxx<<endl;
			//	return 0;
		//	}
		}
	}
	cout<<maxx<<endl;
	
	return 0;
	
}

超時了所以要會用下標相減表示數量。要簡約。

 

 

 

 

 

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章