线性数据结构(洛谷)

ACM题集:https://blog.csdn.net/weixin_39778570/article/details/83187443
约瑟夫问题:https://www.luogu.org/problemnew/show/P1996
题意:求约瑟夫循环出队顺序
题解:不断把队列里的队头加到队尾(非出队人),遇到出队的人则直接出队

#include
#define ll long long
#define fo(i,j,n) for(register int i=j; i<=n; i++)
using namespace std;

int n,k;
queue<int> q;
int main() {
	scanf("%d%d",&n,&k);
	int t;
	fo(i,1,n){
		q.push(i);
	}
	int f=0;
	while(!q.empty()){
		f++;
		if(f==k){
			f=0;
			int t = q.front();q.pop();
			printf("%d%c",t,q.empty()?'\n':' ');
		}else{
			int t = q.front();q.pop();
			q.push(t);
		} 
	}
	return 0;
}

P1115 最大子段和
题目:https://www.luogu.org/problemnew/show/P1115

#include
#define ll long long
#define fo(i,j,n) for(register int i=j; i<=n; ++i)
using namespace std;
const int maxn = 2e5+5;
int n,a[maxn];
int main(){
	scanf("%d",&n);
	fo(i,1,n)scanf("%d",&a[i]); 
	ll ans = a[1];
	ll sum = a[1];
	fo(i,2,n){
		sum += a[i];
		ans = max(ans,sum);
		if(sum<=0) sum = 0;
	}
	cout<<ans<<endl;
	return 0;
}

P1739 表达式括号匹配
题目:https://www.luogu.org/problemnew/show/P1739
题意:判断一个表达式是否合法
解法:使用栈维护左右括号是否合理,判断是否出现运算符遇见运算符的情况,结尾不能为运算符

#include
#define ll long long 
#define fo(i,j,n) for(register int i=j; i<=n; ++i)
using namespace std;
stack<char> opt; 
int main(){
	char c;
//	cout<
	bool flag = true; // 假设左边有符号 
	string ans = "YES";
	while(scanf("%c",&c)!=EOF && c!='@'){
	//	printf("%c",c);
		if(c>='0'&&c<='9' || c>='a'&&c<='z'){
			flag = false; // 左边不是符号了 
			continue;
		}
		if(c=='('){
			opt.push(c);
			flag = true; // 遇到符号 
		}else if(c==')'){
			if(opt.empty()){
				ans = "NO";
				continue; // 右括号比左括号多!!!! 
			}
			opt.pop(); 
		}else if(flag){
			ans = "NO";
			continue;   // 符号遇到符号 
		}else{
			flag = true;// 数字遇到符号 
		}
	}
	if(opt.empty()&&!flag) cout<<ans<<endl; // 空的并且不是符号结尾的 
	else cout<<"NO"<<endl; 
	
	return 0;
}

P1160 队列安排
题目:https://www.luogu.org/problemnew/show/P1160
题意:写一个链表,支持插入和删除

#include
#define ll long long
#define fo(i,j,n) for(register int i=j; i<=n; ++i)
using namespace std;
struct Nlist{
	struct Nlist *last;
	int num;
	struct Nlist *next;
};
Nlist **arr;
int n,m,k,p;
int first;
// 初始化动态申请内存 
void init(int n){
	// 动态申请指针数组 
	arr = (Nlist **)calloc(n+1,sizeof(Nlist *));
	arr[0]=NULL;
	first = 1;
	// 动态申请一维数组每个节点 
	fo(i,1,n){
		arr[i] = (Nlist *)calloc(1,sizeof(Nlist));
		arr[i]->num=i;
		arr[i]->last=NULL;
		arr[i]->next=NULL;
	}
}
// 插入 
void insert(){
	fo(i,2,n){
		scanf("%d %d",&k,&p);
		if(k==first && p==0) first=i; // 插到头节点左边
		if(p==0){// i k
			// i插入k左 
			arr[i]->next = arr[k];
			arr[i]->last = arr[k]->last;
			// 修改i左右节点 
			arr[k]->last = arr[i]; // 右 
		//	cout<last->num<<" "<num<
			if(arr[i]->last!=NULL) // 左 
				arr[i]->last->next = arr[i];
		}
		if(p==1){ // k i
			// i插入k右
			arr[i]->last = arr[k];
			arr[i]->next = arr[k]->next;
			// 修改i左右节点
			arr[k]->next = arr[i]; // 左 
		//	cout<num<<" "<next->num<
			if(arr[i]->next!=NULL) // 右 
				arr[i]->next->last = arr[i]; 
		} 
	}
	// DeBug
//	Nlist *temp;
//	for(temp=arr[first]; temp->next!=NULL; temp=temp->next)
//		printf("%d ",temp->num);
//	printf("%d\n",temp->num); // 打印最后一个 
}
// 删除一个节点 
void del(int i){
	arr[i]->num = 0;
	arr[i]->last = NULL;
	arr[i]->next = NULL;
}
// 删除 
void dele(){
	scanf("%d",&m);
	int x;
	fo(i,1,m){
		scanf("%d",&x);
		if(arr[x]->num == 0) continue; // 删除过了
	//	if(arr[x]==NULL) continue;     // 删除过了,本来是想配合free使用的,却RE了 
		// 若删除头结点,更新头节点 
		if(first==x) first = arr[x]->next!=NULL ? arr[x]->next->num : 0;
		// 更新左右节点 
		if(arr[x]->last != NULL) arr[x]->last->next = arr[x]->next;
		if(arr[x]->next != NULL) arr[x]->next->last = arr[x]->last;
		// 删除本节点 
		del(x);
	//	free(arr[x]);
	}
}
// 打印 
void PF(){
	Nlist *temp;
	for(temp=arr[first]; temp->next!=NULL; temp=temp->next)
		printf("%d ",temp->num);
	printf("%d\n",temp->num); // 打印最后一个 
}
// 释放内存 
void FREE(int n){
	fo(i,0,n)if(arr[i]!=NULL)free(arr[i]); // 释放每个数组 
	free(arr);			   // 释放指针数组 
}
void solve(){
	scanf("%d",&n);
	init(n);
	insert();
	dele();
	PF();
	FREE(n);
}
int main(){
	solve(); 
	return 0;
}

P1449 后缀表达式
题目:https://www.luogu.org/problemnew/show/P1449
题意:后缀表达式求值

#include
#define ll long long
#define fo(i,j,n) for(register int i=j; i<=n; ++i)
using namespace std;

stack<int> st; 
int main(){
	char c;
	int now=0,t1,t2;
	while(scanf("%c",&c) && c!='@'){
		if(c=='*'){
			t1=st.top();st.pop();
			t2=st.top();st.pop();
			st.push(t1*t2);
		}else if(c=='/'){
			t1=st.top();st.pop();
			t2=st.top();st.pop();
			st.push(t2/t1);
		}else if(c=='+'){
			t1=st.top();st.pop();
			t2=st.top();st.pop();
			st.push(t1+t2);
		}else if(c=='-'){
			t1=st.top();st.pop();
			t2=st.top();st.pop();
			st.push(t2-t1);
		}else if(c=='.'){
			st.push(now);
			now=0;
		}else{
			now = now*10 + c -'0';
		}
	}
	cout<<st.top();
	return 0;
} 

你可能感兴趣的:(洛谷,ACM算法日常,数据结构,洛谷)