Codeforces Round #661 (Div. 3)

A : R e m o v e A:Remove A:Remove S m a l l e s t Smallest Smallest
思路:
排序后,若有两数差大于一的,则输出 “ N O ” “NO” NO,反则输出 “ Y E S ” “YES” YES

参考代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
//#define ll long long
const ll N = 1e3 + 5;
const ll maxn = 2e6 + 20;
const ll mod = 998244353;
ll inv[maxn], vis[maxn], dis[maxn];
ll fac[maxn], a[maxn], q[maxn];
vector<ll> vec;
//typedef pair p;
//priority_queue, greater

> m; ll max(ll a, ll b) { return a > b ? a : b; } ll min(ll a, ll b) { return a < b ? a : b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } map<ll, ll> mp; ll ksc(ll a, ll b) { ll ans = 0; while (b) { if (b & 1) ans = (ans + a) % mod; a = (a + a) % mod; b >>= 1; } return ans; } ll ksm(ll a, ll b) { ll ans = 1ll; while (b) { if (b & 1) ans = ksc(ans, a) % mod; a = ksc(a, a) % mod; b >>= 1; } return ans; } ll dp[105][16005]; string p = "abacaba"; int main() { ios::sync_with_stdio(false); cin.tie(0); ll t; cin >> t; while (t--) { ll n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } sort(a + 1, a + n + 1); bool fla = true; for (int i = 2; i <= n; i++) { if (abs(a[i] - a[i - 1]) > 1) { fla = false; cout << "NO" << endl; break; } } if (fla) cout << "YES" << endl; } }

B : G i f t s B:Gifts B:Gifts F i x i n g Fixing Fixing
思路:

求出两个数组的最小值,然后在每个位置上分别对每个数组的最小值做差,累加差的最大值。

参考代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
//#define ll long long
const ll N = 1e3 + 5;
const ll maxn = 2e6 + 20;
const ll mod = 998244353;
ll inv[maxn], vis[maxn], dis[maxn];
ll fac[maxn], a[maxn], q[maxn], b[maxn];
vector<ll> vec;
//typedef pair p;
//priority_queue, greater

> m; ll max(ll a, ll b) { return a > b ? a : b; } ll min(ll a, ll b) { return a < b ? a : b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } map<ll, ll> mp; ll ksc(ll a, ll b) { ll ans = 0; while (b) { if (b & 1) ans = (ans + a) % mod; a = (a + a) % mod; b >>= 1; } return ans; } ll ksm(ll a, ll b) { ll ans = 1ll; while (b) { if (b & 1) ans = ksc(ans, a) % mod; a = ksc(a, a) % mod; b >>= 1; } return ans; } ll dp[105][16005]; string p = "abacaba"; int main() { ios::sync_with_stdio(false); cin.tie(0); ll t; cin >> t; while (t--) { ll n, mina = 1e9 + 7, minb = 1e9 + 7; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i], mina = min(a[i], mina); for (int i = 1; i <= n; i++) cin >> b[i], minb = min(b[i], minb); ll ans = 0; for (int i = 1; i <= n; i++) ans += max(a[i] - mina, b[i] - minb); cout << ans << endl; } }

C : B o a t s C: Boats C:Boats C o m p e t i t i o n Competition Competition
思路:
首先得到所有可能两两相加的答案,然后进行去重处理,标记初始数组中每个元素出现的次数,并标记当前元素存在,然后暴力枚举处理后的两元素和-标记元素是否存在,根据数量求答案。

参考代码:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
//#define ll long long
const ll N = 1e3 + 5;
const ll maxn = 2e6 + 20;
const ll mod = 998244353;
ll inv[maxn], vis[maxn], dis[maxn];
ll fac[maxn], a[maxn], q[maxn], b[N], c[N];
vector<ll> vec;
//typedef pair p;
//priority_queue, greater

> m; ll max(ll a, ll b) { return a > b ? a : b; } ll min(ll a, ll b) { return a < b ? a : b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } map<ll, ll> mp; ll ksc(ll a, ll b) { ll ans = 0; while (b) { if (b & 1) ans = (ans + a) % mod; a = (a + a) % mod; b >>= 1; } return ans; } ll ksm(ll a, ll b) { ll ans = 1ll; while (b) { if (b & 1) ans = ksc(ans, a) % mod; a = ksc(a, a) % mod; b >>= 1; } return ans; } ll dp[105][16005]; string p = "abacaba"; int main() { ios::sync_with_stdio(false); cin.tie(0); ll t; cin >> t; while (t--) { ll n, ma = -1000, minb = 1e9 + 7, ans = 0; cin >> n; vec.clear(); memset(vis, 0, sizeof vis); memset(b, 0, sizeof b); for (ll i = 1; i <= n; i++) cin >> a[i], vis[a[i]] = 1, b[a[i]]++; sort(a + 1, a + 1 + n); int cnt = 0; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) vec.push_back(a[i] + a[j]); sort(vec.begin(), vec.end()); vec.erase(unique(vec.begin(), vec.end()), vec.end()); int k = vec.size(); for (int i = 0; i < k; i++) { //cout << vec[i] << endl; int t = 0; for (int j = 1; j <= n; j++) { int kp = vec[i] - a[j]; if (kp > 0 && vis[kp]) { if (kp == a[j] && (b[kp] - c[kp]) <= 1) continue; int x = min(b[a[j]] - c[a[j]], b[kp] - c[kp]); //cout << x << "*****" << endl; x = max(x, 0); if (kp == a[j]) t += x / 2; else t += x; c[a[j]] += x, c[kp] += x; } } memset(c, 0, sizeof c); ans = max(ans, t); } cout << ans << endl; } }

D : B i n a r y D:Binary D:Binary S t r i n g String String T o To To S u b s e q u e n c e s Subsequences Subsequences
思路:
队列+模拟

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
//#define ll long long
const ll N = 1e3 + 5;
const ll maxn = 2e6 + 20;
const ll mod = 998244353;
ll inv[maxn], vis[maxn], dis[maxn];
//ll fac[maxn], a[maxn], q[maxn], b[N], c[N];
//vector vec;
//typedef pair p;
//priority_queue, greater

> m; ll max(ll a, ll b) { return a > b ? a : b; } ll min(ll a, ll b) { return a < b ? a : b; } ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll a, ll b) { return a * b / gcd(a, b); } map<ll, ll> mp; ll ksc(ll a, ll b) { ll vec = 0; while (b) { if (b & 1) vec = (vec + a) % mod; a = (a + a) % mod; b >>= 1; } return vec; } ll ksm(ll a, ll b) { ll vec = 1ll; while (b) { if (b & 1) vec = ksc(vec, a) % mod; a = ksc(a, a) % mod; b >>= 1; } return vec; } ll dp[105][16005]; string p = "abacaba"; queue<ll> qk, q; vector<ll> vec; int main() { ios::sync_with_stdio(false); cin.tie(0); ll t,n; cin >> t; string str; while (t--) { cin >> n; cin >> str; vec.clear(); while (!q.empty()) q.pop(); while (!qk.empty()) qk.pop(); ll ans = 1; for (int i = 0; i < n; i++) { if (str[i] == '0') { if (q.empty()) vec.push_back(ans++); else vec.push_back(vec[q.front()]), q.pop(); qk.push(i); } else { if (qk.empty()) vec.push_back(ans++); else vec.push_back(vec[qk.front()]), qk.pop(); q.push(i); } } cout << ans - 1 << endl; for (int i = 0; i < vec.size(); i++) cout << vec[i] << ' '; cout << endl; } }

你可能感兴趣的:(ACM刷题题解)