牛子特有的诈骗题
我真的用贪心做了,主打一个唐氏
1.题目描述
链接:登录—专业IT笔试面试备考平台_牛客网
来源:牛客网
题解
目前而言,我并没有找出一个完美的贪心策略来解决这道题(可能是真的没有,也可能是我唐了),因此我选择无脑爆搜dfs,枚举所有情况,取最大即可(别忘了还原状态喵
#include<bits/stdc++.h>
using namespace std;
typedef int long long ll;
typedef pair<ll, ll>p;
const int N = 2e5 + 9;
ll arr[N]; p qq[N]; int n, q;
int dx[3] = { 0,1,3 };
int dy[3] = { 3,1,0 };
ll cnt=2e5+9;
void dfs(int u)
{
if (u > q)
{
ll res = 1;
for (int i = 2; i <= n; i++)
{
if (arr[i] > arr[1])res++;
}
cnt = min(cnt, res);
return;
}
int x = qq[u].first, y = qq[u].second;
for (int i = 0; i < 3; i++)
{
arr[x] += dx[i];
arr[y] += dy[i];
dfs(u + 1);
arr[x] -= dx[i];
arr[y] -= dy[i];
}
}
int main()
{
std::ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cout << fixed << setprecision(6);
int t;
cin >> t;
while (t--) {
cin >> n >> q;
for (int i = 1; i <= n; i++)cin >> arr[i];
for (int i = 1; i <= q; i++)cin >> qq[i].first >> qq[i].second;
cnt = n;
dfs(1);
cout << cnt << endl;
}
}