标签:span 贪吃蛇 replace 位置 顺序 二分 sudoku abc 不能
给定 \(n\),问有多少对正整数 \(a,\ b\),使得 \(a + b = n\) 且 \(a > b\)
如果 \(n\) 是偶数,那么 \(ans = n / 2 - 1\)
如果 \(n\) 是奇数,那么 \(ans = n / 2\)
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
int t, n;;
int main()
{
t = read();
while(t--) {
n = read();
int ans = 0;
if(n % 2) ans = n / 2;
else ans = n / 2 - 1;
printf("%d\n", ans);
}
return 0;
}
构造一个长度为 \(n\) 的字符串,使得每一个长为 \(a\) 的子串都只含有 \(b\) 个不同的字符
构造方式如下
先按照顺序填上前 \(b\) 个小写字母,剩下的 \(a - b\) 个位置填第 \(b\) 个字符
将这样一个子串 \(s\) 填满整个长为 \(n\) 的字符串即可
例如样例 \(7\ 5\ 3\)
构造 \(s = abccc\),答案即为 \(abcccab\)
感性理解,就是一个贪吃蛇在子串 \(s\) 上移动,\(s\) 的正确性构造保证了整个字符串的正确性...
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 1e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
int t, a, b, c;
int main()
{
t = read();
while(t--) {
a = read(), b = read(), c = read();
string s = "";
for(int i = 0; i < c; ++i)
s += ‘a‘ + i;
for(int i = 0; i < b - c; ++i)
s += ‘a‘ + c - 1;
for(int i = 0; i < a / b; ++i)
cout<<s;
for(int i = 0; i < a % b; ++i)
cout<<s[i];
cout<<endl;
}
return 0;
}
给定一组数 \(a\),要求将其划分为两个长度一样的集合 \(s,\ t\),\(s\) 中每个数都一样,\(t\) 中每个数都不一样,\(t\) 中的数也可以出现在 \(s\) 中,求最大的可能的长度
一开始的想法是二分长度的答案,用 \(map\) 记录每个数出现的次数,然后去 \(check\)
后来 \(get\) 了一个比较好的想法
用 \(x\) 表示 \(a\) 中 \(distinct\) 的值的数量
用 \(y\) 表示 \(a\) 中出现次数最多的值的出现次数
\(if\ x = y,\ ans = x - 1\)
\(else,\ ans = min(x,\ y)\)
/*二分答案*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
int t, n, a[N];
map<int, int> mp;
bool check(int x)
{
int f1 = 0, f2 = 0;
set<int> st;
for(int i = 1; i <= n; ++i)
st.insert(a[i]);
for(int i = 1; i <= n; ++i) {
if(mp[a[i]] >= x) {
if(mp[a[i]] == x) st.erase(a[i]);
if(st.size() >= x) return 1;
st.insert(a[i]);
}
}
return 0;
}
int main()
{
t = read();
while(t--) {
mp.clear();
n = read();
for(int i = 1; i <= n; ++i) a[i] = read(), mp[a[i]]++;
int l = 0, r = n / 2;
int ans = 0;
while(l <= r) {
int mid = l + r >> 1;
//cout<<l<<‘ ‘<<r<<endl;
if(check(mid)) ans = mid, l = mid + 1;
else r = mid - 1;
}
printf("%d\n", ans);
}
return 0;
}
/*
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
*/
/*good idea*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
int t, n;
map<int, int> mp;
int main()
{
t = read();
while(t--) {
mp.clear();
n = read();
int x = 0;
for(int i = 1; i <= n; ++i) {
int y = read();
mp[y]++;
x = max(x, mp[y]);
}
int ans = 0;
if(mp.size() == x) ans = x - 1;
else ans = min((int)mp.size(), x);
printf("%d\n", ans);
}
return 0;
}
/*
4
7
4 2 4 1 4 3 4
5
2 1 5 4 3
1
1
4
1 1 1 3
*/
给定一个正确的数独,允许至多修改 \(9\) 个数字,使得其成为 \(Anti-Sudoku\)
数独满足
\(Anti-Sudoku\) 满足
观察发现,每个正方形内都要修改一个位置,并且这些位置所在的行、列不能重复,可以用类似于 \(N\) 皇后的搜索方法来写
后来 \(get\) 到一个很好的 \(idea\),既然给定的是正确的数独,那么我们只需要将任意一个数换成另一个数字就可以了,例如将所有的 \(3\) 换成 \(1\)
/*dfs*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 2e5 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
int t, n, vis[20][20];
char g[20][20];
int f = 0, cnt;
const int dx[] = {0, 1, 1, 1, 2, 2, 2, 3, 3, 3};
const int dy[] = {0, 1, 2, 3, 1, 2, 3, 1, 2, 3};
int row[20], col[20];
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
void out()
{
//puts("XCY");
for(int i = 1; i <= 9; ++i)
puts(g[i] + 1);
}
bool check()
{
map<int, int> mp;
for(int i = 1; i <= 9; ++i) {
int f = 0;
for(int j = 1; j <= 9; ++j) {
mp[g[i][j]]++;
if(mp[g[i][j]] > 1) {f = 1; break;}
}
if(!f) return 0;
mp.clear();
}
for(int i = 1; i <= 9; ++i) {
int f = 0;
for(int j = 1; j <= 9; ++j) {
mp[g[j][i]]++;
if(mp[g[j][i]] > 1) {f = 1; break;}
}
if(!f) return 0;
mp.clear();
}
for(int k = 1; k <= 3; ++k) {
for(int l = 1; l <= 3; ++l) {
int f = 0;
for(int i = (k - 1) * 3 + 1; i <= k * 3; ++i) {
for(int j = (l - 1) * 3 + 1; j <= l * 3; ++j) {
mp[g[i][j]]++;
if(mp[g[i][j]] > 1) {f = 1; break;}
}
if(f) break;
}
mp.clear();
if(!f) return 0;
}
}
return 1;
}
void dfs(int step)
{
//cout<<step<<endl;
if(f) return;
if(step > 9) {
cnt++;
if(check()) f = 1, out();
else return;
}
for(int i = 3 * (dx[step] - 1) + 1; i <= 3 * dx[step]; ++i) {
if(row[i]) continue;
for(int j = 3 * (dy[step] - 1) + 1; j <= 3 * dy[step]; ++j) {
if(col[j]) continue;
for(int k = 1; k <= 9; ++k) {
if(g[i][j] == k + ‘0‘) continue;
if(vis[i][j]) continue;
int temp = g[i][j];
vis[i][j] = 1;
g[i][j] = k + ‘0‘;
row[i] = 1, col[j] = 1;
dfs(step + 1);
if(f) return;
g[i][j] = temp;
vis[i][j] = 0;
row[i] = 0, col[j] = 0;
}
}
}
}
int main()
{
t = read();
while(t--) {
memset(vis, 0, sizeof(vis));
memset(col, 0, sizeof(col));
memset(row, 0, sizeof(row));
f = 0;
for(int i = 1; i <= 9; ++i)
scanf("%s", g[i] + 1);
dfs(1);
//cout<<cnt<<endl;
}
return 0;
}
/*
154873296
386592714
729641835
863725149
975314628
412968357
631457982
598236471
247189563
*/
/*replace 3 with 1*/
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define arrayDebug(a, l, r) for(int i = l; i <= r; ++i) printf("%d%c", a[i], " \n"[i == r])
typedef long long LL;
typedef unsigned long long ULL;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int DX[] = {0, -1, 0, 1, 0, -1, -1, 1, 1};
const int DY[] = {0, 0, 1, 0, -1, -1, 1, 1, -1};
const int MOD = 1e9 + 7;
const int N = 4e2 + 7;
const double PI = acos(-1);
const double EPS = 1e-6;
using namespace std;
int t, n;
char g[20][20];
inline int read()
{
char c = getchar();
int ans = 0, f = 1;
while(!isdigit(c)) {if(c == ‘-‘) f = -1; c = getchar();}
while(isdigit(c)) {ans = ans * 10 + c - ‘0‘; c = getchar();}
return ans * f;
}
int main()
{
t = read();
while(t--) {
for(int i = 1; i <= 9; ++i)
scanf("%s", g[i] + 1);
for(int i = 1; i <= 9; ++i)
for(int j = 1; j <= 9; ++j)
if(g[i][j] == ‘3‘) g[i][j] = ‘1‘;
for(int i = 1; i <= 9; ++i)
puts(g[i] + 1);
}
return 0;
}
标签:span 贪吃蛇 replace 位置 顺序 二分 sudoku abc 不能
原文地址:https://www.cnblogs.com/ChenyangXu/p/12702355.html