codeforces D. Nastya and Scoreboard

编程入门 行业动态 更新时间:2024-10-26 00:20:40

<a href=https://www.elefans.com/category/jswz/34/1770097.html style=codeforces D. Nastya and Scoreboard"/>

codeforces D. Nastya and Scoreboard

题目

题意:

给你 n n n个 7 7 7位数,然后你可以尽力把木棍关闭的打开来使着 n n n个数字最大(只能从关闭->打开),问最大是多少。

思路

我们可以直接用 d f s dfs dfs先从最大的 9 9 9开始递归,然后因为无法从把打开的关闭,所以就有 ( v a l [ i ] & a [ s t e p ] ) = = a [ s t e p ] (val[i] \& a[step]) == a[step] (val[i]&a[step])==a[step],这样可以保证 a s t e p a_{step} astep​上的位数在 v a l i val_i vali​都存在,就保证了只能打开了,然后 d f s ( s t e p + 1 , 剩 余 的 数 量 − 此 次 打 开 的 数 量 ) dfs(step+1,剩余的数量-此次打开的数量) dfs(step+1,剩余的数量−此次打开的数量),因为使从最大的开始找的,所以一旦符合条件,那么这个就是答案了,但是数据有点大,所以需要加上剪枝,也就是 i f ( b o o k [ s t e p ] [ k k ] ) r e t u r n ; if(book[step][kk]) return ; if(book[step][kk])return; b o o k [ s t e p ] [ k k ] = t r u e ; book[step][kk] = true; book[step][kk]=true;这样的话,因为可能某些数字和某些数字翻转所需的使一样的,这样就会导致重复,因为已经递归过并且无法成功。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <map>
#include <deque>
#include <stack>
using namespace std;
typedef long long ll;
typedef vector<int> veci;
typedef vector<ll> vecl;
typedef pair<int, int> pii;
template <class T>
inline void read(T &ret) {char c;int sgn;if (c = getchar(), c == EOF) return ;while (c != '-' && (c < '0' || c > '9')) c = getchar();sgn = (c == '-') ? -1:1;ret = (c == '-') ? 0:(c - '0');while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return ;
}
inline void out(int x) {if (x > 9) out(x / 10);putchar(x % 10 + '0');
}
const int maxn = 2010;
int val[20] = {119, 18, 93, 91, 58, 107, 111, 82, 127, 123}, a[maxn], ans[maxn];
int n, k, x;
bool book[maxn][maxn] = {false};
void dfs(int step, int kk) {if (kk < 0) return ;if (book[step][kk]) return ;book[step][kk] = true;if (step == n) {if (kk == 0) {for (int i = 0; i < n; i++) printf("%d", ans[i]);exit(0);}return ;}for (int i = 9; i >= 0; i--) {if ((val[i] & a[step]) == a[step]) {ans[step] = i;dfs(step + 1, kk - __builtin_popcount(val[i] ^ a[step]));}}
}
int main() {read(n) ,read(k);for (int i = 0; i < n; i++) {for (int j = 0; j < 7; j++) {scanf("%1d", &x);a[i] = (a[i] << 1) + x;}}dfs(0, k);printf("-1\n");return 0;
}

更多推荐

codeforces D. Nastya and Scoreboard

本文发布于:2024-02-12 17:00:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1688659.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:codeforces   Nastya   Scoreboard

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!