代码源每日一题Div2

 一个小整数代码源每日一题Div2_第1张图片

 思路:

开始思路是找规律,for遍历每个数,然后插空什么的,结果问题就是有很多情况考虑不到.最后换了dfs解法,

首先判断数量最多的数的数量和其他数数量总和的关系 

1.如果数量最多的数的数量大于其他数数量总和+1,那么一个隔一个插空都插不了,空不够,直接判失败

2.如果数量最多的数的数量等于其他数数量总和+1,这个时候就只能把最大的这个数推进用于排序的数组

3.如果数量最多的数的数量小于其他数数量总和+1,这个时候就是选合适的最小的数的时候了

最后把用于排序的数组遍历输出一下,结束

当然中间要处理一些特殊情况,比如如果只有一个零,那就在开始直接判断然后输出,不用进入dfs了

#define _CRT_SECURE_NO_WARNINGS
#include
int shuliang[10];
using namespace std;
int dfs(vector& a) {
	int qiyv = 0, maxx = 0, maxshu = -1;
	for (int e = 0; e < 10; e++) {
		if (shuliang[e] > maxx) {
			maxx = shuliang[e];
			maxshu = e;
		}
		qiyv += shuliang[e];
	}
	if (qiyv == 0) {
		return 1;
	}
	qiyv -= maxx;
	if (qiyv + 1 < maxx) {
		return 0;
	}
	if (qiyv + 1 == maxx) {
		if (maxshu == 0 && a.size() == 0) {
			return 0;
		}
		else {
			a.push_back(maxshu);
			shuliang[maxshu]--;
		}
	}
	else {
		int k1 = 0;
		int pan1 = 0;
		if (a.size() == 0) {
			k1 = 1;
			pan1 = -1;
		}
		else {
			k1 = 0;
			pan1 = a.back();
		}
		for (int e = k1; e < 10; e++) {
			if (shuliang[e] > 0 && e !=pan1) {
				a.push_back(e);
				shuliang[e]--;
				break;
			}
		}
	}
	dfs(a);
}
int main() {
	int x = 1;
	for (int e = 0; e < 10; e++) {
		scanf("%d", &shuliang[e]);
		if (e != 0 && shuliang[e] != 0) {
			x = 0;
		}
	}
	if (x&&shuliang[0]==1) {
		printf("%d", 0);
		return 0;
	}
	vectora;
	if (dfs(a)) {
		for (auto i : a) {
			cout << i;
		}
	}
	else {
		cout << -1;
	}
}

 特殊正方形

代码源每日一题Div2_第2张图片

 思路:

开始string一个字符串为n个+,然后找规律,每一层把除了最外层里面的+变成.,之后在把除了最外层.里面的.变成+,每变一次输出一次同时用stack接收,之后把stack中的数据再输出出来就是完整的正方形

#include 
using namespace std;
stackh;
int main()
{
	int a;
	cin >> a;	
	string m;
	int panduan = 0;
	int jishu = 0;
	for (int e = 0; e < a; e++) {
		m += "+";
	}
	for (int ii = 0; ii < a / 2; ii++) {
		if (panduan == 0) {
			for (int e = jishu; e < (a - jishu); e++) {
				m[e] = '+';
			}
			panduan = 1;
		}
		else {
			for (int e = jishu; e < (a - jishu); e++) {
				m[e] = '.';
			}
			panduan = 0;
		}
		h.push(m);
		for (int e = 0; e < m.size(); e++) {
			cout << m[e];
		}
		cout << endl;
		jishu++;
	}
	if (a % 2 != 0) {
		if (panduan == 0) {
			for (int e = jishu; e < (a - jishu); e++) {
				m[e] = '+';
			}
			panduan = 1;
		}
		else {
			for (int e = jishu; e < (a - jishu); e++) {
				m[e] = '.';
			}
			panduan = 0;
		}
		for (int e = 0; e < m.size(); e++) {
			cout << m[e];
		}
		cout << endl;
	}
	while (!h.empty()) {
		cout << h.top() << endl;
		h.pop();
	}
}

 走楼梯代码源每日一题Div2_第3张图片

 思路:

用动态分布解决,建立数组dp[i][j],j分为三个,计算上两节的数量,分为dp[i+1][0],和dp[i+2][1],dp[i+2][2],进行计算.

#include
#include
using namespace std;
int dp[55][3];
int x[2] = { 1,2 };
int main() {
	int aa;
	cin >> aa;
	dp[0][0] = 1;
	for (int e = 1; e <=aa; e++) {//最外层
		dp[e][0] = dp[e][0]+dp[e - 1][0] + dp[e - 1][1] + dp[e - 1][2];
		if (e - 2 >= 0) {
		dp[e][1] = dp[e][1] + dp[e - 2][0];
		dp[e][2] = dp[e][2] + dp[e - 2][1];
		}
	}
	/*for (int e = 0; e <= aa; e++) {
		for (int i = 0; i < 3; i++) {
			cout << dp[e][i];
		}
		cout << endl;
	}*/
	cout << dp[aa][0] + dp[aa][1] + dp[aa][2];
}

 走路代码源每日一题Div2_第4张图片

 思路:

之前先用dfs写的,逻辑到没啥问题,但是超时,全部超时,麻了,改成动态规划,开一个dp[i][j]二维数组,i表示走的次数,j的大小跟m+1一样大,目的就是统计看哪个点被走了,思路就是遍历每一个j如果是1,就加上ai或bi,一步一步下去,最后输出dp[n],结束,后面优化了下空间复杂度.

dfs算法

#include
using namespace std;
int n, m;
int hhh[105][2];
int panduan[105][3] = {0};
vectormm;
void dfs(int x,int y) {
	if (y == n+1) {
		if (m>=x) {
			mm[x] = 1;
			//cout << "JJJ" << endl;
		}
		return;
	}
	if (panduan[y][0] == 0) {
		panduan[y][0] = 1;
		dfs(x + hhh[y][0], y+1);
		//cout << "hhh";
	}
	if (panduan[y][1] == 0) {
		panduan[y][1] = 1;
		dfs(x + hhh[y][1], y+1);
	}
	panduan[y][1] = 0;
	panduan[y][0] = 0;
	//cout << "kkk";
	return;
}
int main() {
	cin >> n >> m;
	for (int e = 0; e <= m; e++) {
		mm.push_back(0);
	}
	for (int e = 1; e <= n; e++) {
		cin >> hhh[e][0] >> hhh[e][1];
	}
	dfs(0,1);
	for (auto i : mm) {
		cout << i;
	}
}

动态规划

#include
using namespace std;
int shujv[105][2];
int dp[105][100005];
int main() {
	int n, m;
	cin >> n >> m;
	for (int e = 1; e <= n; e++) {
		cin >> shujv[e][0] >> shujv[e][1];
	}
	dp[0][0] = 1;
	for (int e = 1; e <= n; e++) {
		for (int i = 0; i <= m; i++) {
			if (dp[e-1][i] == 1) {
				if (i + shujv[e][0] <= m) {
					dp[e][i + shujv[e][0]] = 1;
				}
				if (i + shujv[e][1] <= m) {
					dp[e][i + shujv[e][1]] = 1;
				}
			}
		}
	}
	for (int e = 0; e <= m; e++) {
		cout << dp[n][e];
}
}

动态规划优化空间复杂度

#include
using namespace std;
int shujv[105][2];
vectordp(100005,0);
vectordp2(100005, 0);
vectordp3(100005, 0);
int main() {
	int n, m;
	cin >> n >> m;
	for (int e = 1; e <= n; e++) {
		cin >> shujv[e][0] >> shujv[e][1];
	}
	dp[0] = 1;
	for (int e = 1; e <= n; e++) {
		for (int i = 0; i <= m; i++) {
			if (dp[i] == 1) {
				if (i + shujv[e][0] <= m) {
					dp2[i + shujv[e][0]] = 1;
				}
				if (i + shujv[e][1] <= m) {
					dp2[i + shujv[e][1]] = 1;
				}	
			}
		}
		dp= dp2;
		dp2 =dp3;
		
	}
	for (int e = 0; e <= m; e++) {
		cout <

 简单分数统计

代码源每日一题Div2_第5张图片

 思路:

用了下map,就过了,挺突然的,出问题的地方在map遍历的时候不是按照插入数据的顺序,不过开始记录一下数据顺序就好了

#include
using namespace std;
int a, b, c;
maphhh;
mapren;
vectorren2;
int main() {
	cin >> a >> b >> c;
	for (int e = 1; e <= a; e++) {
		string zanshi;
		cin >> zanshi;
		ren.insert(pair(zanshi, 1));
		ren2.push_back(zanshi);
	}
	for (int e = 1; e <= b; e++) {
		string zanshi;
		int zanshi2;
		cin >> zanshi >> zanshi2;
		hhh[zanshi] = zanshi2;
	}
	for (int e = 1; e <= c; e++) {
		string zanshi;
		string zanshi1;
		string zanshi2;
		cin >> zanshi >> zanshi1 >> zanshi2;
		if (ren[zanshi]>0) {
			if (zanshi2 == "AC") {
				ren[zanshi] += hhh[zanshi1];
			}
		}
		else {
			ren[zanshi] = -1;
		}
	}
	
	for (int e = 0; e < ren2.size(); e++) {
		map::iterator it;
		for (it = ren.begin(); it != ren.end(); it++) {
			if (it->first == ren2[e]) {
				cout << it->first << " " << it->second - 1 << endl;
				break;
			}
			
		}
		
	}
	
}

alice的德州扑克 

代码源每日一题Div2_第6张图片

 思路:

单纯if,else嵌套了一下,判别点数相同就for遍历对比

#include
using namespace std;
maphhh;
int dianshu[6];
int main() {
	int a[6], b[6];
	cin >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> b[1] >> b[2] >> b[3] >> b[4] >> b[5];
	int panduansige = 0;
	for (int e = 1; e <= 5; e++) {
		int geshu = 0;
		for (int i = 1; i <= 5; i++) {
			if (a[e] == a[i]) {
				geshu++;
			}
		}
		dianshu[e] = geshu;
	}
	for (int e = 1; e <= 5; e++) {
		if (dianshu[e] >= 4) {
			panduansige = 1;
		}
	}
	int sange = 0;
	int liangge = 0;
	for (int e = 1; e <= 5; e++) {
		if (dianshu[e] == 3) {
			sange++;
		}
		if (dianshu[e] == 2) {
			liangge++;
		}
	}
	/*for (int e = 1; e <= 5; e++) {
		cout << b[e] << " ";
	}*/
	if (b[1]==b[2]&&b[2] == b[3]&&b[3] == b[4]&&b[4] == b[5]) {
		//cout << "HH<" << endl;
		if (a[5] == 14 && a[4] == 13 && a[3] == 12 && a[2] == 11 && a[1] == 10) {
			cout << "ROYAL FLUSH";
		}
		else {
			if (a[5] == a[4] + 1 && a[4] == a[3] + 1 && a[3] == a[2] + 1 && a[2] == a[1] + 1) {
				cout << "STRAIGHT FLUSH";
			}
			else {
				
				if (panduansige== 1) {
					cout << "FOUR OF A KIND";
				}
				else {
					if (sange == 3 && liangge == 2) {
						cout << "FULL HOUSE";
					}
					else {
						cout << "FLUSH";
					}
				}
			}
		}
	}
	else {
		if (panduansige == 1) {
			cout << "FOUR OF A KIND";
		}
		else {
			if (sange == 3 && liangge == 2) {
				cout << "FULL HOUSE";
			}
			else {
				if (a[5] == a[4] + 1 && a[4] == a[3] + 1 && a[3] == a[2] + 1 && a[2] == a[1] + 1) {
					cout << "STRAIGHT";
				}
				else {
					cout << "FOLD";
				}
			}
		}
	}
	
	

}

订单编号 

代码源每日一题Div2_第7张图片

思路:

开始不知道这个题考查什么,直接列了map,查询,结果123过了,456超时;

最后把点搜索换成区间搜索,还是超时没过,迷惑,最后发现是cin和cout的问题,换成scanf和printf就好了

 map超时代码:

#include
using namespace std;
mappanduan;
int main() {
	int a;
	cin >> a;
	for (int e = 0; e < a; e++) {
		int x;
		cin >> x;
		if (panduan[x] == 0) {
		panduan[x] = 1;
		cout << x<<" ";
		}
		else {
			for (int e = x + 1;; e++) {
				if (panduan[e] == 0) {
					panduan[e] = 1;
					cout << e << " ";
					break;
				}
			}
		}
	}
}

区间搜索用scanf 和printf代码:

#include
using namespace std;
set>hhh;
inline void insert(int x, int y) {
	if (x < y) {
		return;
	}
	hhh.insert(make_pair(x, y));
}
int main() {
	int a;
	scanf("%d", &a);
	insert(2e9, 1);
	for (int e = 0; e < a; e++) {
		int x;
		scanf("%d", &x);
		auto its = hhh.lower_bound(make_pair(x, 0));
		if (its->second <= x) {
			printf("%d ", x);
			insert(its->first, x + 1);
			insert(x-1,its->second);
			hhh.erase(its);
		}
		else {
			printf("%d ", its->second);
			insert(its->first, its->second + 1);
			hhh.erase(its);
		}
	}
}

任务分配

代码源每日一题Div2_第8张图片

思路:

用动态规划,这个和之前有个叫采药的题比较像,思路就是遍历时间数组,每次到一个点先看是这个点的数大还是前一个点的数大,如果是前一个点数大那就把前一个点的数放到这个点,如果这个点和某个事件的开始时间相符合,那就判断这个事件是否值得执行.

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int n;
struct s {
	int st, ed,w;
};
vectora;
int main() {
	int maxx = 0;
	cin >> n;
	for (int e = 0; e < n; e++) {
		s x;
		cin >> x.st >> x.ed >> x.w;
		a.push_back(x);
		if (x.ed > maxx) {
			maxx = x.ed;
		}
	}
	vectorpd(maxx+1);
	for (int e = 1; e

路径计数 

代码源每日一题Div2_第9张图片

思路:

开始没多想,直接bfs,结果超时

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int tu[105][105];
int panduan[105][105] = { 0 };
queue>a;
inline void push(int x, int y) {
	a.push(make_pair(x, y));
}
int main() {
	int x;
	long long cishu=0;
	cin >> x;
	for (int e = 1; e <= x; e++) {
		for (int i = 1; i <= x; i++) {
			cin >> tu[e][i];
		}
	}
	a.push(make_pair(1, 1));
	panduan[1][1] = 1;
	while (!a.empty()) {
		int x1 = a.front().first;
		int y1 = a.front().second;
		a.pop();
		if (x1 == x && y1 == x) {
			cishu++;
			cishu = cishu%1000000007;
			continue;

		}
		
		if (x1+1<=x) {
			if (tu[x1 + 1][y1] == 1) {
			push(x1 + 1, y1);
			if (x1 + 1 != x && y1 != x) {
			panduan[x1 + 1][y1] = 1;
			}
			}
		}
		if (y1+1<=x) {
			if (tu[x1][y1 + 1] == 1 ) {
			push(x1, y1+1);
			if (x1!= x && y1+1!= x) {
				panduan[x1][y1 + 1] = 1;
			}
			}
		}
	}
	cout << cishu;
}

 换成动态规划

dp[i][j]=dp[i-1][j]+dp[i][j-1]过了

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int tu[105][105];
long long panduan[105][105];
queue>a;
inline void push(int x, int y) {
	a.push(make_pair(x, y));
}
int main() {
	int x;
	long long cishu = 0;
	cin >> x;
	for (int e = 1; e <= x; e++) {
		for (int i = 1; i <= x; i++) {
			cin >> tu[e][i];
		}
	}
	panduan[1][1] = 1;
	for (int e = 1; e <= x; e++) {
		for (int i = 1; i <= x; i++) {
				panduan[e][i] += panduan[e - 1][i] +panduan[e][i - 1];
				panduan[e][i] = panduan[e][i] % 1000000007;
				if (tu[e][i] == 0) {
					panduan[e][i] = 0;
				}
		}
	}
	cout << panduan[x][x];
}

顺便温习了一下dfs,当然,也是超时

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int tu[105][105];
int pan[105][105] = {0};
int x1;
long long jishu=0;
void dfs(int x, int y) {
	if (x == x1 && y == x1) {
		jishu++;
		jishu = jishu % 1000000007;
		return;
	}
	if (pan[x + 1][y] == 0&&tu[x+1][y]==1) {
		pan[x + 1][y] = 1;
		dfs(x + 1, y);
	}
	if (pan[x][y + 1] == 0 && tu[x][y+1] == 1) {
		pan[x][y + 1] = 1;
		dfs(x, y + 1);
	}
	pan[x][y + 1] = 0;
	pan[x + 1][y] = 0;
	return;
}
int main() {

	long long cishu = 0;
	cin >> x1;
	for (int e = 1; e <= x1; e++) {
		for (int i = 1; i <= x1; i++) {
			cin >> tu[e][i];
		}
	}
	dfs(1, 1);
	cout << jishu;
}

最大和上升子序列

代码源每日一题Div2_第10张图片

 思路:

用dp从左到右扫一遍

#define _CRT_SECURE_NO_WARNINGS
#include
using namespace std;
int main() {
	int n;
	cin >> n;
	int a[10005];
	int f[10005];
	for (int e = 1; e <= n; e++) {
		cin >> a[e];
		f[e] = a[e];
	}
	for (int e = 1; e <= n; e++) {
		for (int i = 1; i < e; i++) {
			if (a[e] > a[i]) {
				f[e] = max(f[e], f[i] + a[e]);
			}
		}
	}
	int ans = 0;
	for (int e = 1; e <= n; e++) {
		ans = max(ans, f[e]);
	}
	cout << ans;
}

 

你可能感兴趣的:(c++)