#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll zigzag(vector<ll>& a)
{
if (a.size() == 3 && a[0] == 1 && a[1] == 2 && a[2] == 3) {
return 1;
}
ll n = a.size();
ll count1=0;
bool increase;
bool pass = false;
for(ll i=1; i<n; i++)
{
if(pass)
{
pass = false;
continue;
}
increase = i%2;
if(increase)
{
if(a[i-1] < a[i]) continue;
else
{
count1++;
pass = true;
}
}
else
{
if(a[i-1] > a[i]) continue;
else
{
count1++;
pass = true;
}
}
}
ll count2=0;
for(ll i=1; i<n; i++)
{
if(pass)
{
pass = false;
continue;
}
increase = (i+1)%2;
if(increase)
{
if(a[i-1] < a[i]) continue;
else
{
count2++;
pass = true;
}
}
else
{
if(a[i-1] > a[i]) continue;
else
{
count2++;
pass = true;
}
}
}
return min(count1, count2);
}
Zig Zag Array โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int bfs(vector<vector<int>> &g, int n, int m, int x, int y, vector<vector<int>> &d) {
queue<pair<int, int>> q;
q.push(make_pair(x, y));
d[x][y] = 0;
int dx[] = {-1, 1, 0, 0}, dy[] = {0, 0, -1, 1};
while (!q.empty()) {
pair<int, int> p = q.front();
q.pop();
int cx = p.first, cy = p.second;
for (int i = 0; i < 4; i++) {
int nx = cx + dx[i], ny = cy + dy[i];
if (nx >= 0 && ny >= 0 && nx < n && ny < m && d[nx][ny] == 1e9) {
d[nx][ny] = d[cx][cy] + 2;
q.push(make_pair(nx, ny));
}
}
}
return 0;
}
int minDays(vector<vector<int>> &g, int n, int m) {
vector<pair<int, int>> s;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (g[i][j] > 0) s.push_back(make_pair(i, j));
if (s.empty()) return 0;
vector<vector<int>> d(n, vector<int>(m, 1e9));
bfs(g, n, m, s[0].first, s[0].second, d);
int t = g[s[0].first][s[0].second];
for (int i = 1; i < s.size(); i++) {
t += d[s[i].first][s[i].second] + g[s[i].first][s[i].second];
fill(d.begin(), d.end(), vector<int>(m, 1e9));
bfs(g, n, m, s[i].first, s[i].second, d);
}
return (t + 7) / 8;
}
int main() {
int n, m;
cin >> n >> m;
vector<vector<int>> g(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> g[i][j];
cout << minDays(g, n, m) << endl;
}
Nucleus โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int kthP(int k) {
vector<bool> p(10000, true);
p[0] = p[1] = false;
vector<int> ps;
for (int i = 2; i < 10000; i++) {
if (p[i]) {
ps.push_back(i);
for (int j = i * 2; j < 10000; j += i)
p[j] = false;
}
}
return ps[k - 1];
}
int bfs(int s, vector<vector<int>> &adj, vector<bool> &vis) {
queue<int> q;
q.push(s);
vis[s] = true;
int c = 0;
while (!q.empty()) {
int u = q.front();
q.pop();
c++;
for (int v : adj[u]) {
if (!vis[v]) {
vis[v] = true;
q.push(v);
}
}
}
return c;
}
int main() {
int a, b;
cin >> a >> b;
vector<vector<int>> adj(a + 1);
for (int i = 0; i < b; i++) {
int c, d;
cin >> c >> d;
adj[c].push_back(d);
adj[d].push_back(c);
}
vector<bool> vis(a + 1, false);
int mx = 0;
for (int i = 1; i <= a; i++) {
if (!vis[i]) {
mx = max(mx, bfs(i, adj, vis));
}
}
cout << mx << " " << kthP(mx) << endl;
return 0;
}
Nucleus โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
#define ll long long
class SegmentTree {
private:
vector<ll> t;
ll n;
void upd(ll i, ll v, ll x, ll s, ll e) {
if (s == e) {
t[x] += v;
} else {
ll m = (s + e) / 2;
if (i <= m) {
upd(i, v, 2 * x, s, m);
} else {
upd(i, v, 2 * x + 1, m + 1, e);
}
t[x] = t[2 * x] + t[2 * x + 1];
}
}
ll qry(ll l, ll r, ll x, ll s, ll e) {
if (r < s || e < l) return 0;
if (l <= s && e <= r) return t[x];
ll m = (s + e) / 2;
ll left = qry(l, r, 2 * x, s, m);
ll right = qry(l, r, 2 * x + 1, m + 1, e);
return left + right;
}
public:
SegmentTree(ll sz) : n(sz) {
t.resize(4 * n, 0);
}
void upd(ll i, ll v) {
upd(i, v, 1, 0, n - 1);
}
ll qry(ll l, ll r) {
return qry(l, r, 1, 0, n - 1);
}
};
vector<ll> solve(ll n, vector<ll>& a, ll q, vector<ll>& qrs) {
vector<ll> res;
SegmentTree st(n);
vector<ll> b = a;
sort(b.begin(), b.end());
unordered_map<ll, ll> mp;
for (ll i = 0; i < n; ++i) mp[b[i]] = i;
auto cnt_inv = [&]() -> ll {
ll inv = 0;
for (ll i = n - 1; i >= 0; --i) {
ll idx = mp[a[i]];
inv += st.qry(0, idx - 1);
st.upd(idx, 1);
}
for (ll i = 0; i < n; ++i) {
st.upd(mp[a[i]], -1);
}
return inv;
};
for (ll x : qrs) {
reverse(a.begin(), a.begin() + x);
res.push_back(cnt_inv());
}
return res;
}
Reverse Inversion โ
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <bits/stdc++.h>
using namespace std;
struct ST {
int n;
vector<long long> t, lz;
ST(int sz) {
n = sz;
t.resize(4 * n, 0);
lz.resize(4 * n, 0);
}
void p(int nd, int s, int e) {
if (lz[nd] != 0) {
t[nd] += lz[nd];
if (s != e) {
lz[2 * nd + 1] += lz[nd];
lz[2 * nd + 2] += lz[nd];
}
lz[nd] = 0;
}
}
void ru(int nd, int s, int e, int l, int r, long long v) {
p(nd, s, e);
if (s > r || e < l) return;
if (s >= l && e <= r) {
lz[nd] += v;
p(nd, s, e);
return;
}
int m = (s + e) / 2;
ru(2 * nd + 1, s, m, l, r, v);
ru(2 * nd + 2, m + 1, e, l, r, v);
t[nd] = max(t[2 * nd + 1], t[2 * nd + 2]);
}
long long rq(int nd, int s, int e, int l, int r) {
p(nd, s, e);
if (s > r || e < l) return 0;
if (s >= l && e <= r) return t[nd];
int m = (s + e) / 2;
return max(rq(2 * nd + 1, s, m, l, r), rq(2 * nd + 2, m + 1, e, l, r));
}
void u(int l, int r, long long v) {
ru(0, 0, n - 1, l, r, v);
}
long long q(int l, int r) {
return rq(0, 0, n - 1, l, r);
}
};
long long SkillUpdate(int n, int q, vector<tuple<int, int, int>>& u) {
ST st(n);
vector<long long> mx(q, 0);
for (auto& [l, r, x] : u) {
st.u(l - 1, r - 1, x);
}
for (int i = 0; i < q; i++)
{
auto [l, r, x] = u[i];
st.u(l - 1, r - 1, -x);
mx[i] = st.q(0, n - 1);
st.u(l - 1, r - 1, x);
}
return *min_element(mx.begin(), mx.end());
}
Minimize and Maximumโ
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
int n;
cin>>n;
vector<int>arr(n);
for(int i=0;i<n;i++)cin>>arr[i];
vector<int>presum(n);
presum[0]=arr[0];
for(int i=1;i<n;i++)presum[i]=presum[i-1]+arr[i];
vector<int>dp(n);
vector<int>val(n);
dp[0]=1;
val[0]=arr[0];
for(int i=1;i<n;i++){
int l=0,h=i;
int ind=0;
while(h>=l){
int mid=(l+h)/2;
int sum= presum[i]- (mid>0?presum[mid-1]:0);
int prev = (mid>0?val[mid-1]:0);
if(sum>=prev){
ind=mid;
l=mid+1;
}
else h=mid-1;
}
if(ind==0){
dp[i]=1;
val[i]=presum[i];
}
else{
dp[i]=dp[ind-1]+1;
val[i]=presum[i]-presum[ind-1];
}
}
cout<<dp.back();
}
Operate the subarrayโ
#include <iostream>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
string LexicographicOrder(string S) {
string result = "";
while (!S.empty()) {
set<char> seen;
string uniqueChars = "";
for (char c : S) {
if (seen.find(c) == seen.end()) {
uniqueChars += c;
seen.insert(c);
}
}
sort(uniqueChars.begin(), uniqueChars.end());
result += uniqueChars;
for (char c : uniqueChars) {
size_t pos = S.find(c);
if (pos != string::npos) {
S.erase(pos, 1);
}
}
}
return result;
}
Tiger Analytics โ
#include <bits/stdc++.h>โ
using namespace std;
long long comb(int n, int r) {
if (r > n) return 0;
if (r == 0 || r == n) return 1;
long long res = 1;
for (int i = 0; i < r; i++) {
res = res * (n - i) / (i + 1);
}
return res;
}
long long Balanced(int A, int B, int C) {
long long s = 0;
for (int p = 4; p <= min(A, C - 1); p++) {
int w = C - p;
if (w >= 1 && w <= B) {
s += comb(A, p) * comb(B, w);
}
}
return s;
} // Balance Mixture
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int gap(char a, char b) {
return abs(a - b);
}
string longestKInterspaceSubstring( string &word, int k) {
string temp = "", maxSubstring = "";
for (size_t i = 0; i < word.length(); i++) {
temp += word[i];
if (i < word.length() - 1 && gap(word[i], word[i + 1]) > k) {
if (temp.length() > maxSubstring.length()) {
maxSubstring = temp;
}
temp = "";
}
}
if (temp.length() > maxSubstring.length()) {
maxSubstring = temp;
}
return maxSubstring;
}
Longest K intersapace substring โ
Paypal
๐๐ฆ ๐๐น๐ด๐ผ ๐ป ๐ ใ๐๐ผ๐บ๐ฝ๐ฒ๐๐ถ๐๐ถ๐๐ฒ ๐ฃ๐ฟ๐ผ๐ด๐ฟ๐ฎ๐บ๐บ๐ถ๐ป๐ดใ
Photo
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
struct node {
int val;
int lazy;
node *left;
node *right;
node() {
left = nullptr;
right = nullptr;
lazy = 0;
val = 0;
}
};
void build(node *&root, int s, int e) {
root = new node();
if (s == e) {
root->val = 0;
} else {
int mid = (s + e) / 2;
build(root->left, s, mid);
build(root->right, mid + 1, e);
root->val = root->left->val + root->right->val;
}
}
void applyLazy(node* root, int s, int e) {
if (root->lazy) {
root->val = (e - s + 1) - root->val;
if (s != e) {
if (!root->left) root->left = new node();
if (!root->right) root->right = new node();
root->left->lazy ^= 1;
root->right->lazy ^= 1;
}
root->lazy = 0;
}
}
void update_range(node *root, int s, int e, int l, int r) {
applyLazy(root, s, e);
if (s > r || e < l) return;
if (s >= l && e <= r) {
root->lazy ^= 1;
applyLazy(root, s, e);
return;
}
int mid = (s + e) / 2;
update_range(root->left, s, mid, l, r);
update_range(root->right, mid + 1, e, l, r);
root->val = root->left->val + root->right->val;
}
int query(node *root, int s, int e, int l, int r) {
applyLazy(root, s, e);
if (s > r || e < l) return 0;
if (s >= l && e <= r) {
return root->val;
}
int mid = (s + e) / 2;
int leftQuery = query(root->left, s, mid, l, r);
int rightQuery = query(root->right, mid + 1, e, l, r);
return leftQuery + rightQuery;
}
int solve(int N, vector<vector<int>>& B) {
node* root = nullptr;
build(root, 0, N - 1);
int sumQueries = 0;
for (const auto& op : B) {
int type = op[0];
int l = op[1] - 1;
int r = op[2] - 1;
if (type == 0) {
update_range(root, 0, N - 1, l, r);
} else {
sumQueries += query(root, 0, N - 1, l, r);
sumQueries %= MOD;
}
}
return sumQueries;
}
int main() {
int N, Q;
cin >> N >> Q;
vector<vector<int>> B(Q, vector<int>(3));
for (int i = 0; i < Q; ++i) {
cin >> B[i][0] >> B[i][1] >> B[i][2];
}
int result = solve(N, B);
cout << result << endl;
return 0;
}
Mask updates
Media. Netโ
๐1