Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b8dd744

Browse files
Adds two question, one being good 2pointer question
1 parent fc1272b commit b8dd744

File tree

2 files changed

+270
-0
lines changed

2 files changed

+270
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Type definitions
5+
typedef long long ll;
6+
typedef pair<int, int> pii;
7+
typedef pair<ll, ll> pll;
8+
typedef vector<int> vi;
9+
typedef vector<ll> vl;
10+
typedef vector<pii> vpii;
11+
typedef vector<pll> vpll;
12+
typedef vector<vi> vvi;
13+
typedef vector<vl> vvl;
14+
15+
// Macros
16+
#define nline "\n"
17+
#define all(x) (x).begin(), (x).end()
18+
#define rall(x) (x).rbegin(), (x).rend()
19+
#define sz(x) (int)(x).size()
20+
#define pb push_back
21+
#define mp make_pair
22+
#define F first
23+
#define S second
24+
#define forn(i, n) for (int i = 0; i < int(n); i++)
25+
#define forr(i, a, b) for (int i = a; i <= b; i++)
26+
#define ford(i, a, b) for (int i = a; i >= b; i--)
27+
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
28+
29+
// Constants
30+
const int MOD = 1e9 + 7;
31+
const ll INF = 1e18;
32+
const double EPS = 1e-9;
33+
const double PI = acos(-1);
34+
35+
ll mod_add(ll a, ll b, ll m = MOD) { return ((a % m) + (b % m)) % m; }
36+
ll mod_sub(ll a, ll b, ll m = MOD) { return ((a % m) - (b % m) + m) % m; }
37+
ll mod_mul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }
38+
39+
ll mod_pow(ll base, ll exp, ll m = MOD)
40+
{
41+
ll res = 1;
42+
base %= m;
43+
while (exp > 0)
44+
{
45+
if (exp & 1)
46+
res = mod_mul(res, base, m);
47+
base = mod_mul(base, base, m);
48+
exp >>= 1;
49+
}
50+
return res;
51+
}
52+
53+
ll mod_inv(ll a, ll m = MOD)
54+
{
55+
return mod_pow(a, m - 2, m); // Only works if m is prime
56+
}
57+
58+
ll mod_div(ll a, ll b, ll m = MOD)
59+
{
60+
return mod_mul(a, mod_inv(b, m), m);
61+
}
62+
63+
// Binary exponentiation (for non-modular calculations)
64+
ll binpow(ll base, ll exp)
65+
{
66+
ll res = 1;
67+
while (exp > 0)
68+
{
69+
if (exp & 1)
70+
res *= base;
71+
base *= base;
72+
exp >>= 1;
73+
}
74+
return res;
75+
}
76+
77+
/************/
78+
void solve()
79+
{
80+
int n;
81+
cin >> n;
82+
set<int> arr;
83+
forn(i, n)
84+
{
85+
int x;
86+
cin >> x;
87+
arr.insert(x);
88+
}
89+
90+
int cnt = 0, init = -1;
91+
92+
for (auto it : arr)
93+
{
94+
if (init + 1 < it)
95+
{
96+
cnt++;
97+
init = it;
98+
}
99+
}
100+
101+
cout << cnt << nline;
102+
}
103+
int main()
104+
{
105+
ios_base::sync_with_stdio(0);
106+
cin.tie(0);
107+
cout.tie(0);
108+
109+
#ifndef ONLINE_JUDGE
110+
freopen("./outputs/input.txt", "r", stdin);
111+
freopen("./outputs/output.txt", "w", stdout);
112+
#endif
113+
114+
int t;
115+
cin >> t;
116+
while (t--)
117+
solve();
118+
119+
return 0;
120+
}
121+
// Explanation
122+
/*
123+
*/
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// Type definitions
5+
typedef long long ll;
6+
typedef pair<int, int> pii;
7+
typedef pair<ll, ll> pll;
8+
typedef vector<int> vi;
9+
typedef vector<ll> vl;
10+
typedef vector<pii> vpii;
11+
typedef vector<pll> vpll;
12+
typedef vector<vi> vvi;
13+
typedef vector<vl> vvl;
14+
15+
// Macros
16+
#define nline "\n"
17+
#define all(x) (x).begin(), (x).end()
18+
#define rall(x) (x).rbegin(), (x).rend()
19+
#define sz(x) (int)(x).size()
20+
#define pb push_back
21+
#define mp make_pair
22+
#define F first
23+
#define S second
24+
#define forn(i, n) for (int i = 0; i < int(n); i++)
25+
#define forr(i, a, b) for (int i = a; i <= b; i++)
26+
#define ford(i, a, b) for (int i = a; i >= b; i--)
27+
#define elasped_time 1.0 * clock() / CLOCKS_PER_SEC
28+
29+
// Constants
30+
const int MOD = 1e9 + 7;
31+
const ll INF = 1e18;
32+
const double EPS = 1e-9;
33+
const double PI = acos(-1);
34+
35+
ll mod_add(ll a, ll b, ll m = MOD) { return ((a % m) + (b % m)) % m; }
36+
ll mod_sub(ll a, ll b, ll m = MOD) { return ((a % m) - (b % m) + m) % m; }
37+
ll mod_mul(ll a, ll b, ll m = MOD) { return ((a % m) * (b % m)) % m; }
38+
39+
ll mod_pow(ll base, ll exp, ll m = MOD)
40+
{
41+
ll res = 1;
42+
base %= m;
43+
while (exp > 0)
44+
{
45+
if (exp & 1)
46+
res = mod_mul(res, base, m);
47+
base = mod_mul(base, base, m);
48+
exp >>= 1;
49+
}
50+
return res;
51+
}
52+
53+
ll mod_inv(ll a, ll m = MOD)
54+
{
55+
return mod_pow(a, m - 2, m); // Only works if m is prime
56+
}
57+
58+
ll mod_div(ll a, ll b, ll m = MOD)
59+
{
60+
return mod_mul(a, mod_inv(b, m), m);
61+
}
62+
63+
// Binary exponentiation (for non-modular calculations)
64+
ll binpow(ll base, ll exp)
65+
{
66+
ll res = 1;
67+
while (exp > 0)
68+
{
69+
if (exp & 1)
70+
res *= base;
71+
base *= base;
72+
exp >>= 1;
73+
}
74+
return res;
75+
}
76+
77+
/************/
78+
79+
void solve()
80+
{
81+
int n;
82+
cin >> n;
83+
vl arr(n, 0);
84+
85+
forn(i, n) cin >> arr[i];
86+
87+
ll tail = 0, head = -1;
88+
89+
ll cost = LLONG_MAX;
90+
91+
while (tail < n)
92+
{
93+
94+
while (head + 1 < n && arr[tail] == arr[head + 1])
95+
{
96+
head++;
97+
}
98+
99+
ll val = arr[tail];
100+
ll left_cost = val * tail;
101+
ll right_cost = val * (n - head - 1);
102+
ll total_cost = left_cost + right_cost;
103+
104+
cost = min(cost, total_cost);
105+
106+
if (tail > head)
107+
{
108+
tail++;
109+
head = tail - 1;
110+
}
111+
else
112+
{
113+
tail++;
114+
}
115+
}
116+
117+
cout << cost << nline;
118+
}
119+
int main()
120+
{
121+
ios_base::sync_with_stdio(0);
122+
cin.tie(0);
123+
cout.tie(0);
124+
125+
#ifndef ONLINE_JUDGE
126+
freopen("./outputs/input.txt", "r", stdin);
127+
freopen("./outputs/output.txt", "w", stdout);
128+
#endif
129+
130+
int t;
131+
cin >> t;
132+
while (t--)
133+
solve();
134+
135+
return 0;
136+
}
137+
// Explanation
138+
/*
139+
% This code solves a problem where you want to minimize a "cost" based on an array.
140+
% For each unique value in the array, you calculate the cost if you make all elements
141+
% before its segment and after its segment equal to that value.
142+
% The cost is: value * (number of elements before) + value * (number of elements after).
143+
% The code uses two pointers (tail and head) to find segments of equal values.
144+
% It updates the minimum cost found for all such segments.
145+
% At the end, it prints the smallest cost for each test case.
146+
% It's pretty efficient since it only scans the array once per test case.
147+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /