2022-10洛谷

P1160 队列安排 

list不支持<=等的比较

感觉自己写的思路还是不太行。。。。。。

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int N = 1e5 + 10;
list<int> lt;
int vist[N];
list<int>:: iterator pos[N];
int main(){
    int n, m;
	cin >> n;
	lt.push_front(1);
	pos[1] = lt.begin();
	for(int i = 2; i <= n; i ++ ){
		int k, p;
		cin >> k >> p;
		if(!p){
			pos[i] = lt.insert(pos[k], i);
		}
		else{
			list<int>::iterator nex = pos[k];
			nex ++ ;
			pos[i] = lt.insert(nex, i);
		}
	}
	cin >> m;
	while(m -- ){
		int x;
		cin >> x;
		if(!vist[x]){
			vist[x] = 1;
			lt.erase(pos[x]);
		}
	}
	for(list<int>::iterator it = lt.begin(); it != lt.end(); it ++ ){
		cout << *it << " ";
	}
	return 0;
}

P1449 后缀表达式

woc二刷忘记了栈的进出顺序,减法和除法出现问题了呜呜呜呜

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int N = 1e5 + 10;
string s; 
stack<int> st;
int a, b, t;
int main(){
    cin >> s;
    for(int i = 0; s[i] != '@'; i ++ ){
    	switch(s[i]){
    		case '+': {
    			a = st.top();
    			st.pop();
    			b = st.top();
    			st.pop();
    			st.push(a + b);
				break;
			}
			case '-': {
				a = st.top();
    			st.pop();
    			b = st.top();
    			st.pop();
    			st.push(b - a);
				break;
			}
			case '*': {
				a = st.top();
    			st.pop();
    			b = st.top();
    			st.pop();
    			st.push(a * b);
				break;
			}
			case '/': {
				a = st.top();
    			st.pop();
    			b = st.top();
    			st.pop();
    			st.push(b / a);
				break;
			}
			case '.': {
				st.push(t);
				t = 0;
				break;
			}
			default: {
				t = t * 10 + s[i] - '0';
				break;
			}
		}
	}
	cout << st.top();
	return 0;
}
//3.5.2.-*7.+@

P1981 [NOIP2013 普及组] 表达式求值 

运算符优先级优先级!!!

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int m = 10000;
string s;
stack<int> st;
stack<char> ope;
int t, a, b, ans;
int main(){
	cin >> s;
	for(int i = 0; i < s.size(); i ++ ){
		if(s[i] >= '0' && s[i] <= '9'){
			if(s[i + 1] >= '0' && s[i + 1] <= '9'){
				t = t * 10 + s[i] - '0';
			}
			else{
				t = (t * 10 + s[i] - '0') % m;
				st.push(t);
				t = 0;
			}
		}
		else{
			if(ope.empty()) ope.push(s[i]);
			else{
				if(s[i] < ope.top()) ope.push(s[i]);
				else{
					int a = st.top();
					st.pop();
					int b = st.top();
					st.pop();
					if(ope.top() == '*') st.push((a * b) % m);
					else st.push((a + b) % m);
					ope.pop();
					ope.push(s[i]);
				}
			}
			
		}
	}
	while(!ope.empty()){
		int a = st.top();
		st.pop();
		int b = st.top();
		st.pop();
		char x = ope.top();
		ope.pop();
		if(x == '*') st.push((a * b) % m);
		else st.push((a + b) % m);
	}
	cout << st.top() << endl;
	return 0;
}

P1175 表达式的转换 

好难,看了题解懂了思路,但还是不能自己打出来QAQ

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int m = 10000;
stack<char> dat, op;
stack<int> num, dat2;
int rank(char c){
	switch(c){
		case '+': return 1;
		case '-': return 1;
		case '*': return 2;
		case '/': return 2;
		case '^': return 3;
		case '(': return 0;
		case ')': return 0;
		default: return -1;
	}
}
int calans(int a, int b, char t){
	switch(t){
		case '+': return a + b;
		case '-': return a - b;
		case '*': return a * b;
		case '/': return a / b;
		case '^': return pow(a, b);
		default: return -INF;
	}
}
void change(string s){
	int len = s.size();
	for(int i = 0; i < len; i ++ ){
		if(isdigit(s[i])) dat.push(s[i]);
		else if(s[i] == '(') op.push(s[i]);
		else if(s[i] == ')'){
			char t = op.top();
			while(t != '('){
				op.pop();
				dat.push(t);
				t = op.top();
			}
			op.pop();
		}
		else if(rank(s[i]) >= 1 && rank(s[i]) <= 3){
			if(!op.empty()){
				char t = op.top();
				while(!op.empty() && rank(s[i]) <= rank(t)){
					if(rank(s[i]) == rank(t) && s[i] == '^') break;//在s[i]与栈顶都是^号时也能进栈
					op.pop();
					dat.push(t);
					if(!op.empty()) t = op.top();
				}
			}
			op.push(s[i]);
		}
	}
	while(!op.empty()){
		char t = op.top();
		op.pop();
		dat.push(t);
	}
	while(!dat.empty()){
		char t = dat.top();
		dat.pop();
		op.push(t);
	}
	while(!op.empty()){
		char t = op.top();
		cout << t << " ";
		op.pop();
		dat.push(t);
	}
	puts("");
}
void calculate(){
	while(!dat.empty()){
		char t= dat.top();
		dat.pop();
		op.push(t);
	}
	while(!op.empty()){
		char t = op.top();
		op.pop();
		if(isdigit(t)) num.push(t - '0');
		else{
			int a = num.top();
			num.pop();
			int b = num.top();
			num.pop();
			num.push(calans(b, a, t));
			while(!num.empty()){
				int t = num.top();
				num.pop();
				dat2.push(t); 
			}
			while(!dat2.empty()){
				int t = dat2.top();
				cout << t << " ";
				dat2.pop();
				num.push(t);
			}
			while(!op.empty()){
				char t= op.top();
				cout << t << " ";
				op.pop();
				dat.push(t);
			}
			while(!dat.empty()){
				char t = dat.top();
				dat.pop();
				op.push(t);
			}
			puts("");
		}
	}
}
int main(){
	string s;
	cin >> s;   
	change(s);
	calculate();
	return 0;
}

P1111 修复公路

kruskal 

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int N = 1e5 + 10;
int p[N];
int n, m;
struct edge{
	int x, y, t;
	bool operator < (const edge &W) const{
	    return t < W.t;
	}
}edge[2 * N];
int find(int x){
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}
int kruskal(){
    sort(edge, edge + m);
	for(int i = 0; i < m; i ++ ){
		int a = find(edge[i].x);
		int b = find(edge[i].y);
		if(a != b){
			p[a] = b;
			n -- ;
		}
		if(n == 1) return edge[i].t;
	}
	return -1;
}
int main(){
	cin >> n >> m;
	for(int i = 0; i < n; i ++ ) p[i] = i;
	for(int i = 0; i < m; i ++ ){
		cin >> edge[i].x >> edge[i].y >> edge[i].t;
	}
	int t = kruskal();
	cout << t << endl;
	return 0;
}

P3958 [NOIP2017 提高组] 奶酪

谢,一开始直接暴力模拟只过了一个,后面看题解怎么用并查集只有80,对着dalao代码写才ac的,真的会谢 

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
#define x first
#define y second
typedef long long LL;
const int N = 1e5 + 10;
int t;
LL p[N];
struct cake{
	LL x, y, z;
	bool operator < (const cake &W) const {
	    return z < W.z;
	}
}cake[N];
LL find(LL x){
	if(p[x] != x) p[x] = find(p[x]);
	return p[x];
}
int main(){
    cin >> t;
    while(t -- ){
    	LL n, h, r;
    	cin >> n >> h >> r;
    	for(int i = 0; i <= n + 1; i ++ ) p[i] = i;
    	for(int i = 0; i < n; i ++ ){
    		cin >> cake[i].x >> cake[i].y >> cake[i].z;
		}
		for(int i = 0; i < n; i ++ ){
			for(int j = i + 1; j < n; j ++ ){
				if(pow(cake[i].x - cake[j].x, 2) + pow(cake[i].y - cake[j].y, 2) + pow(cake[i].z - cake[j].z, 2) <= 4 * r * r){
					LL a = find(i);
					LL b = find(j);
					if(a != b) p[a] = b;
				}
			}
			if(cake[i].z - r <= 0){
				LL a = find(i);
				LL b = find(n);
				if(a != b) p[a] = b;
			}
			if(cake[i].z + r >= h){
				LL a = find(i);
				LL b = find(n + 1);
				if(a != b) p[a] = b;
			}
		}
		if(find(n) != find(n + 1)) puts("No");
		else puts("Yes");
	}
	return 0;
}

提醒自己一下这个(尬


P1090 [NOIP2004 提高组] 合并果子 / [USACO06NOV] Fence Repair G 

这道题不难,但一开始没有想到循环条件。。。。。(呃。。好吧应该就是不会。。。。

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
//#define x first
//#define y second
#define mod 7
typedef long long LL;
const int N = 5e3 + 10;
int n, ans;
priority_queue<int, vector<int>, greater<int> > q;
int main(){
	cin >> n;
	for(int i = 1; i <= n; i ++ ){
		int x;
		cin >> x;
		q.push(x);
	}
	while(q.size() >= 2){
		int a = q.top();
		q.pop();
		int b = q.top();
		q.pop();
		q.push(a + b);
		ans += a + b;
	}
	cout << ans;
	return 0;
}

P1087 [NOIP2004 普及组] FBI 树 

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
//#define x first
//#define y second
#define mod 7
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long LL;
const int N = 15;
int n;
string s;
char FBI(string s){
	if(s.length() > 1){
		cout << FBI(s.substr(0, s.length() / 2));
		cout << FBI(s.substr(s.length() / 2, s.length() / 2));
	}
	if(s == string(s.length(), '0')) return 'B';
	if(s == string(s.length(), '1')) return 'I';
	return 'F';
}
int main(){
	cin >> n >> s;
	cout << FBI(s);
	return 0;
}

P1030 [NOIP2001 普及组] 求先序排列

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
//#define x first
//#define y second
#define mod 7
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long LL;
const int N = 15;
string inorder, postorder;
void preorder(string inorder, string postorder){
	if(inorder.size()){
		char ch = postorder[postorder.size() - 1];
		int k = inorder.find(ch);
		cout << ch;
		preorder(inorder.substr(0, k), postorder.substr(0, k));
		preorder(inorder.substr(k + 1), postorder.substr(k, postorder.size() - 1 - k));
	}
}
int main(){
	cin >> inorder >> postorder;
	preorder(inorder, postorder);
	return 0;
}

P1305 新二叉树 

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
//#define x first
//#define y second
#define mod 7
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long LL;
const int N = 15;
int n;
string s;
int main(){
	cin >> n >> s;
	for(int i = 0; i < n - 1; i ++ ){
		string t;
		cin >> t;
		int k = s.find(t[0]);
		t = t.substr(1);
		s.insert(k + 1, t);
	}
	for(int i = 0; i < 2 * n + 1; i ++ ){
		if(s[i] != '*') cout << s[i];
	}
	return 0;
}

 P1229 遍历问题

这道题感觉有点考思维和基础概念,一开始我虽然知道必须知道中序排列以及另外一种排列才能求出第三种排列,但具体很细的地方没有细想,所以写这道题把我干蒙了。

首先明确一个重点,就是只有单一个根只有一个子节点的时候,会出现不同的中序排列。例如如果一棵树的一个根只有左子树,那么前序中根节点右边第一个是左节点,后序中根节点左边第一个是左节点,如果只有右子树,那么前序中根节点的右边第一个是右节点,后序中根节点左边第一个是右节点。

然而在一般情况下(即有两个子节点的情况下),前序中根节点右边第一个是左节点,后序中左边第一个是右节点。

所以,当出现前序和后序中相邻两个相反相同(即BA和AB),就说明了出现了单节点,这个时候中序排列就会因为这个而出现了两种情况,总数*2;

因此在以上想清楚以后,就不停一个个比较有没有相同相邻的,统计次数。

#include<bits/stdc++.h> 
using namespace std;
#define INF 0x3f3f3f3f
//#define x first
//#define y second
#define mod 7
#define endl '\n'
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
typedef long long LL;
const int N = 15;
string s1, s2;
int cnt;
int main(){
	cin >> s1 >> s2;
	for(int i = 0; i < s1.size(); i ++ ){
		for(int j = 0; j < s2.size(); j ++ ){
			if(s1[i] == s2[j + 1] && s1[i + 1] == s2[j]){
				cnt ++ ;
			}
		}
	}
	cout << (1 << cnt);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值