A1112 A1113 A1114 A1115 模拟练习3 待补并查集

1112 Stucked Keyboard (20 分)

题目链接

Sample Input:

3
caseee1__thiiis_iiisss_a_teeeeeest

Sample Output:

ei
case1__this_isss_a_teest

Note1

  1. 题意, 输入坏键重复的次数,输入字符串,检测坏键
  2. 思路: 以为要用stl(但是并没有)就没想模拟去解,以后碰到这种题管他麻烦还是不麻烦还是得做啊
  3. 思路:先整体循环一边找到不可能为坏键的,再把坏键的去掉,难点在于找不可能为坏键, 自己没有想到好的办法,网上借鉴他人代码的发现逻辑很不错。

Code1

#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<map>
using namespace std;
const int maxn = 1e4;
map<char, int> visited;
int main(){
#ifdef _DEBUG 
	ifstream cin("data.txt");
#endif
	int check_num; 
	cin >> check_num; 
	string s, valid;
	int cnt = 0;
	cin >> s;
    if(check_num == 1) {cout << s << endl; return 0;}
	// for(int i = 1; i < s.size() - 1; i++){
	// 	if(s[i - 1] != s[i] && s[i] != s[i+1]){
	// 		visited[s[i]] = 1; 
	// 	}
	// }
    char check = '@';
    for(int i = 0 ; i < s.size() ; i++)	{		
        if(s[i] != check){			
            if(cnt < check_num || cnt % check_num != 0)			
                visited[check] = 1;			
            cnt = 1;	    		
            check = s[i];		
        }else cnt ++;	
    }

	int flag = 0;
	cnt = 0;
	for(int i = 0; i < s.size() ; i++){
		if( i + check_num - 1 < s.size() && s[i] == s[i + check_num - 1]){
			int p = i, q = i + check_num - 1;
			while(p != q && s[p] == s[q]){p++;}
			if(p ==q &&  visited[s[i]] != 1){
				flag = 1;
				if(visited[s[i]] != 2){
					visited[s[i]] = 2;
					cout << s[i];
				}		
			}
		}
		if(flag == 1){
			cnt ++;
		}
		if(flag == 0 || cnt == check_num){
			flag = 0;
			cnt = 0;
			valid += s[i];
		}
	}
	cout << endl << valid;
	
#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

1113 Integer Set Partition (25 分)

题目链接

Sample Input 1:

10
23 8 10 99 46 2333 46 1 666 555

Sample Output 1:

0 3611

Sample Input 2:

13
110 79 218 69 3721 100 29 135 2 6 13 5188 85

Sample Output 2:

1 9359

Note

  1. 题意: 输入num个数字,分成两个集合,在保证元素数目差值最小的情况下保证元素和的差最大
  2. 思路: 简单题,排序,相加,输出
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
using namespace std;
const int maxn = 1e5;
bool cmp(int a, int b){
	return a < b;
}
int a[maxn];
int main(){
#ifdef _DEBUG 
	ifstream cin("data2.txt");
#endif
	int num, sum1 = 0, sum2 = 0;
	cin >> num;
	for(int i = 0; i < num; i++){
		cin >> a[i];
	}
	sort(a, a+num, cmp);
	int temp = num / 2 ;//+  num % 2;
	for(int i = 0; i < num; i++){
		if(i < temp) sum1 += a[i]; 
		else if(i >= temp) sum2 += a[i];
	}
	cout << num % 2 << " " << sum2 - sum1;
#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

1114 Family Property (25 分)

题目链接

Sample Input:

10
6666 5551 5552 1 7777 1 100
1234 5678 9012 1 0002 2 300
8888 -1 -1 0 1 1000
2468 0001 0004 1 2222 1 500
7777 6666 -1 0 2 300
3721 -1 -1 1 2333 2 150
9012 -1 -1 3 1236 1235 1234 1 100
1235 5678 9012 0 1 50
2222 1236 2468 2 6661 6662 1 300
2333 -1 3721 3 6661 6662 6663 1 100

Sample Output:

3
8888 1 1.000 1000.000
0001 15 0.600 100.000
5551 4 0.750 100.000

Note

  1. 题意: 输入成员信息,输出n个家庭的信息,家庭代号为成员id的最小值,排序的原则为area sum降序, id升序
  2. 思路:应该是要用并查集写的但是突然觉得不熟悉,于是就用了bfs了,导致差bug查了好久哇,而且还有一个点没过。

3. to do 再按照自己的思路写一遍,看看到底为啥bug如何之多

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e5;
struct node{
	int area;
	int estate;
	vector<int> next;
}a[maxn];
vector<int> exits;
int visited[maxn] = {0};
struct family{
	int id;
	int number;
	int estates;
	int areas;
}b[maxn];
bool cmp(family a, family b){
	if(a.areas * 1.0 /( a.number ) == b. areas  * 1.0 / (0 )) return a.id < b.id;
	else return a.areas /( a.number ) > b.areas/ (b.number );
}
int tempid = maxn, tempcnt = 0, temp_sum_estate = 0, temp_area = 0;
void Dfs(int n){
	visited[n] = 1;
	temp_area +=a[n].area;
	tempcnt++;
	if(n < tempid) tempid = n;
	temp_sum_estate += a[n].estate;
	for(int i = 0; i < a[n].next.size(); i++){
		if(visited[a[n].next[i]] == 0){
			int tempp = a[n].next[i];
			Dfs(tempp);
		}
	}

}

int main(){
#ifdef _DEBUG 
	ifstream cin("data3.txt");
#endif
	int num;
	cin >> num;
	for(int i = 0; i < num; i++){
		string  bb, cc;
		int aa,dd;
		cin >> aa >> bb >> cc >>dd;
		exits.push_back(aa);
		if(bb != "-1") {a[aa].next.push_back(stoi(bb)); a[stoi(bb)].next.push_back(aa);}
		if(cc != "-1") {a[aa].next.push_back(stoi(cc)); a[stoi(cc)].next.push_back(aa);}
		for(int j = 0; j < dd; j++){
			int temp;
			cin >> temp;
			a[aa].next.push_back(temp);
			a[temp].next.push_back(aa);
		}
		int ee, ff;
		cin >> ee >> ff;
		a[aa].estate = ee;
		a[aa].area = ff;
	}
	int cnt = 0, last = 0;
	for(int i = 0; i < exits.size(); i++){
		tempid = maxn;
		temp_area = 0;
		temp_sum_estate = 0;
		tempcnt = 0;
		if(visited[exits[i]] == 0){
			Dfs(exits[i]);
			cnt++;
		}
		
		if(cnt == last) break;
		b[cnt].id = tempid;
		b[cnt].areas = temp_area;
		b[cnt].estates = temp_sum_estate;
		b[cnt].number = tempcnt;
		last = cnt;
	}
	sort(b + 1, b+cnt+1  , cmp);
	cout << cnt << endl;
	for(int i = 1; i <= cnt; i++){
		printf("%04d %d %.3lf %.3lf\n", b[i].id, b[i].number,(double) (b[i].estates * 1.000 / b[i].number), (double)(b[i].areas * 1.000 / b[i].number) );
	}


#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

Note2

  1. break 逻辑错误,应该为continue
  2. cmp的地方忽略了int型转double

Code2

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<string>
#include<fstream>
#include<queue>
using namespace std;
const int maxn = 1e5;
struct node{
	int area;
	int estate;
	vector<int> next;
}a[maxn];
vector<int> exits;
int visited[maxn] = {0};
struct family{
	int id;
	int number;
	int estates;
	int areas;
}b[maxn];
bool cmp(family a, family b){
	if(a.areas * 1.0 /( a.number ) == b. areas  * 1.0 / b.number ) return a.id < b.id;
	else return a.areas * 1.0 /( a.number ) > b.areas * 1.0/ (b.number );
}
int tempid = maxn, tempcnt = 0, temp_sum_estate = 0, temp_area = 0;
void Dfs(int n){
	visited[n] = 1;
	temp_area +=a[n].area;
	tempcnt++;
	if(n < tempid) tempid = n;
	temp_sum_estate += a[n].estate;
	for(int i = 0; i < a[n].next.size(); i++){
		if(visited[a[n].next[i]] == 0){
			int tempp = a[n].next[i];
			Dfs(tempp);
		}
	}

}

int main(){
#ifdef _DEBUG 
	ifstream cin("data3.txt");
#endif
	int num;
	cin >> num;
	for(int i = 0; i < num; i++){
		string  bb, cc;
		int aa,dd;
		cin >> aa >> bb >> cc >>dd;
		exits.push_back(aa);
		if(bb != "-1") {a[aa].next.push_back(stoi(bb)); a[stoi(bb)].next.push_back(aa);}
		if(cc != "-1") {a[aa].next.push_back(stoi(cc)); a[stoi(cc)].next.push_back(aa);}
		for(int j = 0; j < dd; j++){
			int temp;
			cin >> temp;
			a[aa].next.push_back(temp);
			a[temp].next.push_back(aa);
		}
		int ee, ff;
		cin >> ee >> ff;
		a[aa].estate = ee;
		a[aa].area = ff;
	}
	int cnt = 0, last = 0;
	for(int i = 0; i < exits.size(); i++){
		tempid = maxn;
		temp_area = 0;
		temp_sum_estate = 0;
		tempcnt = 0;
		if(visited[exits[i]] == 0){
			Dfs(exits[i]);
			cnt++;
		}
		
		if(cnt == last) continue;
		b[cnt].id = tempid;
		b[cnt].areas = temp_area;
		b[cnt].estates = temp_sum_estate;
		b[cnt].number = tempcnt;
		last = cnt;
	}
	sort(b + 1, b+cnt+1  , cmp);
	cout << cnt << endl;
	for(int i = 1; i <= cnt; i++){
		printf("%04d %d %.3lf %.3lf\n", b[i].id, b[i].number,(double) (b[i].estates * 1.000 / b[i].number), (double)(b[i].areas * 1.000 / b[i].number) );
	}


#ifdef _DEBUG 
	cin.close();
#endif
	return 0;
}

1115 Counting Nodes in a BST (30 分)

题目链接

Sample Input:

9
25 30 42 16 20 20 35 -5 28

Sample Output:

2 + 4 = 6

Note

  1. 题意 输入节点,建立bst,输出后两层的节点之和
  2. 思路: bst利用了学ds时候自己做的模板,带指针的考试应该不会考吧。

Code

#include<iostream>
#include<cstdio>
#include<stack>
using namespace std;
typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
const int maxn = 1e3;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};
int depthcout[maxn], max_depth = 0;

BinTree Insert( BinTree BST, ElementType X ) {
    if(BST == NULL){
        BST = (BinTree)malloc(sizeof(Position));
        BST->Data = X;
        BST->Left = NULL;
        BST->Right = NULL;
  
    }
	else if(X > BST->Data)  BST->Right =Insert(BST->Right, X);
    else if(X <= BST->Data)  BST->Left = Insert(BST->Left, X);
	return BST;
}//diyidabug
 
void Dfs(BinTree n, int depth){
	depthcout[depth]++; 
	if(depth > max_depth) max_depth = depth;
	if(n->Left != NULL)  Dfs(n->Left, depth + 1);
	if(n->Right != NULL) Dfs(n->Right, depth+1);
}

int main()
{
    BinTree BST, MinP, MaxP, Tmp;
    ElementType X;
    int num, i;
    BST = NULL;
    scanf("%d", &num);
    for ( i = 0; i < num; i++ ) {
        scanf("%d", &X);
        BST = Insert(BST, X);
    }
	Dfs(BST, 1);
	cout << depthcout[max_depth] << " + " << depthcout[max_depth-1] << " = " << depthcout[max_depth]+ depthcout[max_depth-1];
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值