HELLO WORLD !!!

The real hero is

to see the world as it is

and to love it !

figvalar

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

写在前面:
以后大概刷多少题写几篇题解,不求量,把心态放平
选座位

点击查看代码
#include<bits/stdc++.h>
using ll = long long;
using namespace std;
const int N = 11;
ll max_tal, m, n;
vector<ll> st(N), ch(N); 
bool vis[N];
ll dfs(ll pos_st, ll sum_tal)
{
	if(pos_st == m+1)
	{
		return (ll) sum_tal >= max_tal/2; 
	}
	//枚举座位
	ll res = 0;
	for(int i = 1;i <= n; i++)
	{
		if(!vis[i])
		{
			vis[i] = 1;
			res += dfs(pos_st+1,sum_tal + ch[i] * st[pos_st]);
			vis[i] = 0;
		}	
	}
	return res;
}

void solve()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++) cin >> ch[i];
	for(int i = 1; i <= m; i++) cin >> st[i];
	sort(st.begin()+1,st.begin()+m+1);
	sort(ch.begin()+1,ch.begin()+n+1);
	//预处理最大值
	max_tal = 0;
	for(int i = m, j = n; i && j ;i--, j--) max_tal += st[i] * ch[j];
	cout << dfs(1,0) << '\n';
}

int main()
{
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
    int T; cin >> T;
    while(T--) solve();
    return 0;
}

数字组合

点击查看代码 ``` #include using ll = long long; using namespace std; const int N = 30; ll n, k; vector a(N);

ll dfs(ll dep, ll sum)
{
if(dep == n+1) return (ll)sum <= k;
ll res = 0;
res = dfs(dep+1, sum) + dfs(dep+1, sum+a[dep]);
return res;
}

void solve()
{
cin >> n >> k;
for(int i = 1; i <= n; i++) cin >> a[i];
cout << dfs(1, 0) << '\n';
}

int main()
{
cin.tie(0), cout.tie(0);
ios::sync_with_stdio(false);
int T; cin >> T;
while(T--) solve();
return 0;
}

</details>

搜索常见算法就两种:dfs|bfs,其中dfs尤为重要:
<details>
<summary>点击查看代码</summary>

void dfs(int x)
{
cout << x << ' ';
for (auto &i : g[x]) dfs(i);
}

</details>

<details>
<summary>点击查看代码</summary>

//遍历图
void dfs(int x)
{
if (vis[x]) return;
vis[x] = true;
for (auto &i : g[x]) dfs(i);
}

</details>
dfs其实是一种更优雅的暴力,它的枚举有两种形式:枚举集合,枚举排列。
posted on 2025-04-15 21:16  figvalar  阅读(31)  评论(0)    收藏  举报