A
分两种情况
设 a < b
要么 宽(a)两倍覆盖长(b), 边长为 a * 2
要么 直接长(b)覆盖宽 边长 b
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 1e5 + 5;
int n, m, _, k;
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
int a, b, c; cin >> a >> b;
if (a > b) swap(a, b);
if ((a << 1) >= b) c = (a << 1);
else c = b;
cout << c * c << '
';
}
return 0;
}
B
排序 取相邻差最小值
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 50 + 5;
int n, m, _, k;
int s[N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
cin >> n;
rep (i, 1, n) cin >> s[i];
sort(s + 1, s + 1 + n);
m = 10000;
rep (i, 2, n) m = min(m, s[i] - s[i - 1]);
cout << m << '
';
}
return 0;
}
C
奇偶都为偶数对,yes
or 寻找一对 a, a + 1, 变成上面的偶数对的情况
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 50 + 5;
int n, m, _, k;
int s[N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
cin >> n; int a = 0, b = 0;
rep (i, 1, n)
{
cin >> s[i];
if (s[i] & 1) ++a;
else ++b;
}
if (a % 2 == 0) { cout << "YES
"; continue; }
sort(s + 1, s + 1 + n);
int flag = 0;
rep (i, 2, n) if (s[i] == s[i - 1] + 1) { flag = 1; break; }
if (flag) cout << "YES
";
else cout << "NO
";
}
return 0;
}
D
暴力
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 50 + 5;
int n, m, _, k;
int s[N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
cin >> n >> k;
int ans = n;
for (int i = 1; i <= k && i * i <= n; ++i)
if (n % i == 0)
{
int a = n / i;
if (a <= k) { ans = i; break; }
ans = a;
}
cout << ans << '
';
}
return 0;
}
E
从最外层,一层一层判断
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 50 + 5;
int n, m, _, k;
char s[N][N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
cin >> n; bool flag = 1;
rep (i, 1, n) cin >> s[i] + 1;
per (k, n, 1)
{
int h = n - k + 1;
per (i, h, 1)
if (s[h][i] == '1' && !(h == n || i == n || s[h + 1][i] == '1' || s[h][i + 1] == '1'))
{
//cout << h << '!' << i << '
';
flag = 0;
break;
}
per (i, h, 1)
if (s[i][h] == '1' && !(h == n || i == n || s[i + 1][h] == '1' || s[i][h + 1] == '1'))
{
//cout << s[i] << '
';
//cout << i << ' ' << h << '
';
flag = 0;
break;
}
if (flag == 0) break;
}
if (flag == 0) cout << "NO
";
else cout << "YES
";
}
return 0;
}
F
dfs
#include <bits/stdc++.h>
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 10 + 5;
int n, m, _, k;
char s[N][N], ans[N];
int st[N];
bool dfs(int k)
{
if (k == m + 1) return true;
char c = '1'; int cur[N];
rep(i, 1, n) cur[i] = st[i];
rep(i, 1, n)
if (s[i][k] != c)
{
c = ans[k] = s[i][k];
bool flag = 1;
rep(j, 1, n)
{
if (s[j][k] != c) ++st[j];
if (st[j] > 1)
{
rep(p, 1, j) st[p] = cur[p];
flag = 0; break;
}
}
if (flag)
{
if (dfs(k + 1)) return true;
rep(p, 1, n) st[p] = cur[p];
}
}
return false;
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
for (cin >> _; _; --_)
{
cin >> n >> m;
rep(i, 1, n) cin >> s[i] + 1;
memset(st, 0, sizeof st);
ans[m + 1] = ' ';
if (dfs(1)) cout << ans + 1 << '
';
else cout << -1 << '
';
}
return 0;
}
G
dancing links 不会
H
先找到中位数, 然后左右移动中位数就行
用set存一下删掉的数
#define all(n) (n).begin(), (n).end()
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define sqr(n) (n)*(n)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef double db;
const int N = 60 + 5;
int n, m, _, k;
ll b[N];
char s[N];
int main()
{
ios::sync_with_stdio(0); cin.tie(0);
b[0] = 1;
rep (i, 1, 61) b[i] = b[i - 1] << 1;
for (cin >> _; _; --_)
{
cin >> n >> m;
ll ans = 1ll << (m - 1); --ans;
ll x = ans, y = ans + 1;
unordered_set<ll> mp;
rep (i, 1, n)
{
cin >> s + 1;
ll res = 0;
for (int i = 0, j = m; j; --j, ++i)
if (s[j] == '1') res += b[i];
mp.insert(res);
if (res == ans)
{
if (y)
{
++ans, --y;
while (mp.count(ans)) ++ans;
}
else
{
--ans, --x;
while (mp.count(ans)) --ans;
}
}
else if (res > ans) --y;
else --x;
while (x > y)
{
--x, ++y; --ans;
while (mp.count(ans)) --ans;
}
while (y > x + 1)
{
--y, ++x, ++ans;
while (mp.count(ans)) ++ans;
}
}
for (int i = m - 1, j = 1; j <= m; ++j, --i)
if (ans >= b[i]) cout << 1, ans -= b[i];
else cout << 0;
cout << '
';
}
return 0;
}