标签:run 拆分 names lan als ide bool end tail
视频题解。
签到。
/*
* Author: heyuhhh
* Created Time: 2020/6/5 10:13:34
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
void run() {
ll a, b; cin >> a >> b;
if (a == b) {
cout << 0 << ‘\n‘;
return;
}
if (a > b) {
int cnt = 0;
while (a > b && a % 2 == 0) {
a >>= 1;
++cnt;
}
if (a == b) {
cout << (cnt + 2) / 3 << ‘\n‘;
} else {
cout << -1 << ‘\n‘;
}
return;
}
if (a < b) {
int cnt = 0;
while (a < b) {
a <<= 1;
++cnt;
}
if (a == b) {
cout << (cnt + 2) / 3 << ‘\n‘;
} else {
cout << -1 << ‘\n‘;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}
\(O(n^2)\)枚举即可。
/*
* Author: heyuhhh
* Created Time: 2020/6/5 10:04:42
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
void run() {
int n; cin >> n;
vector <int> a(n);
vector <int> cnt(1024);
for (int i = 0; i < n; i++) {
cin >> a[i];
++cnt[a[i]];
}
for (int k = 1; k <= 1023; k++) {
vector <int> nc = cnt;
for (int i = 0; i < n; i++) {
--nc[a[i] ^ k];
}
bool ok = true;
for (int i = 0; i < 1024; i++) {
if (nc[i] != 0) {
ok = false;
}
}
if (ok) {
cout << k << ‘\n‘;
return;
}
}
cout << -1 << ‘\n‘;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}
找规律推导一下即可。发现我们可以每一位单独考虑,只用求出每一位的情况即可。
/*
* Author: heyuhhh
* Created Time: 2020/6/5 10:38:37
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5;
void run() {
ll n; cin >> n;
ll t = 1;
ll ans = 0;
for (int i = 1; i < 64; i++) {
ans += 1ll * i * ((n + t) / (2ll * t));
t <<= 1;
}
cout << ans << ‘\n‘;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}
第一步:读题。
第二步:模拟。
最后的顺序就是从小到大进行安排,然后可以优化一下,对于每个结点找mex的时候反过来,在操作较小的结点时先给当前结点打上标记就行。
/*
* Author: heyuhhh
* Created Time: 2020/6/4 23:26:19
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 5e5 + 5;
int n, m;
vector <int> G[N], v[N];
int a[N];
void run() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int u, v; cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
int x; cin >> x;
v[x].push_back(i);
}
for (int i = 1; i <= n; i++) {
a[i] = 1;
}
vector <int> ans;
for (int i = 1; i <= n; i++) {
for (auto u : v[i]) {
ans.push_back(u);
if (a[u] != i) {
cout << -1 << ‘\n‘;
return;
}
for (auto to : G[u]) {
if (a[to] == i) {
++a[to];
}
}
}
}
for (auto it : ans) cout << it << ‘ ‘;
cout << ‘\n‘;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
详见代码:
/*
* Author: heyuhhh
* Created Time: 2020/6/5 8:18:48
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 1e5 + 5, MOD = 1e9 + 7;
int qpow(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1) res = res * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return res;
}
void run() {
int n, p; cin >> n >> p;
vector <int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
if (p == 1) {
if (n & 1) {
cout << 1 << ‘\n‘;
} else {
cout << 0 << ‘\n‘;
}
return;
}
sort(all(a));
vector <int> l, r;
while (sz(a)) {
r.push_back(a.back());
a.pop_back();
ll A = 1;
int x = r.back();
for (int i = sz(a) - 1; i >= 0; i--) {
l.push_back(a[i]);
int d = x - a[i];
x = a[i];
a.pop_back();
while (d && A < n) {
A *= p;
--d;
}
if (d == 0 && --A == 0) {
break;
}
}
}
int ans = 0;
for (auto it : l) {
ans = (ans + MOD - qpow(p, it)) % MOD;
}
for (auto it : r) {
ans = (ans + qpow(p, it)) % MOD;
}
cout << ans << ‘\n‘;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
int T; cin >> T; while(T--)
run();
return 0;
}
代码如下:
/*
* Author: heyuhhh
* Created Time: 2020/6/5 17:05:23
*/
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <iomanip>
#include <assert.h>
#include <functional>
#include <numeric>
#define MP make_pair
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define INF 0x3f3f3f3f
#define Local
#ifdef Local
#define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
void err() { std::cout << std::endl; }
template<typename T, typename...Args>
void err(T a, Args...args) { std::cout << a << ‘ ‘; err(args...); }
template <template<typename...> class T, typename t, typename... A>
void err(const T <t> &arg, const A&... args) {
for (auto &v : arg) std::cout << v << ‘ ‘; err(args...); }
#else
#define dbg(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
//head
const int N = 5e5 + 5;
void run() {
int n; cin >> n;
vector <int> a(n), b(n);
for (int i = 0; i < n; i++) {
cin >> a[i] >> b[i];
}
for (int k = 20; k >= 0; k--) {
int t = 1 << k;
vector <vector <pii>> G(t + n);
for (int i = 0; i < n; i++) {
G[a[i] & (t - 1)].push_back(MP(i + t, 2 * i));
G[i + t].push_back(MP(a[i] & (t - 1), 2 * i));
G[b[i] & (t - 1)].push_back(MP(i + t, 2 * i + 1));
G[i + t].push_back(MP(b[i] & (t - 1), 2 * i + 1));
}
bool ok = true;
for (int i = 0; i < t + n; i++) {
if (sz(G[i]) & 1) {
ok = false;
break;
}
}
if (ok == false) continue;
vector <bool> del(2 * n);
vector <int> ans;
function <void(int, int)> dfs = [&] (int u, int from) {
while (sz(G[u])) {
auto it = G[u].back(); G[u].pop_back();
int v = it.fi, id = it.se;
if (!del[id]) {
del[id] = true;
dfs(v, id);
}
}
if (from != -1) ans.push_back(from);
};
ans.clear();
for (int i = 0; i < n + t; i++) {
if (sz(G[i])) {
dfs(i, -1);
break;
}
}
if (sz(ans) < 2 * n) {
continue;
}
cout << k << ‘\n‘;
for (auto it : ans) {
cout << it + 1 << ‘ ‘;
}
cout << ‘\n‘;
break;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cout << fixed << setprecision(20);
run();
return 0;
}
Codeforces Round #647 (Div. 2) - Thanks, Algo Muse!
标签:run 拆分 names lan als ide bool end tail
原文地址:https://www.cnblogs.com/heyuhhh/p/13051654.html