Skip to content

Commit 723c701

Browse files
committed
cf round 725
1 parent b8f830f commit 723c701

12 files changed

+823
-40
lines changed

450_DSA/1.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string reverseWord(string str){
2+
3+
//Your code here
4+
string ans="";
5+
for(int i=str.size()-1;i>=0;i--){
6+
ans+=str[i];
7+
}
8+
return ans;
9+
}

Codeforces/1535C-Unstable_String.cpp

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -98,44 +98,21 @@ const ll mod2=998244353;
9898
// Experience :
9999
// Cp is nothing but only observation and mathematics.
100100

101-
ll dp[200005][2];
102-
103101
ll solve()
104102
{
105103
string s;
106104
cin>>s;
107-
ll n=sz(s);
108-
memset(dp,0,sizeof(dp));
109-
if(s[n-1]=='1' or s[n-1]=='0'){
110-
dp[n-1][s[n-1]-'0']=n-1;
111-
dp[n-1][1-s[n-1]+'0']=-1;
112-
}
113-
else{
114-
dp[n-1][0]=n-1;
115-
dp[n-1][1]=n-1;
116-
}
117105
ll ans=0;
118-
for(ll i=n-2;i>=0;i--){
119-
if(s[i]=='0'){
120-
dp[i][1]=-1;
121-
dp[i][0]=max(dp[i+1][1],i);
122-
}
123-
else if(s[i]=='1'){
124-
dp[i][0]=-1;
125-
dp[i][1]=max(dp[i+1][0],i);
126-
}
127-
else{
128-
dp[i][0]=i;
129-
dp[i][1]=i;
130-
if(s[i+1]!='0')
131-
dp[i][0]=dp[i+1][1];
132-
if(s[i+1]!='1')
133-
dp[i][1]=dp[i+1][0];
106+
ll n=sz(s);
107+
vl pos(2,-1);
108+
for(ll i=0;i<n;i++){
109+
ll temp=ll(s[i]-'0');
110+
if(temp==0||temp==1){
111+
pos[(temp^(i%2))]=i;
134112
}
135-
ans-=(i-1);
136-
ans+=max(dp[i][0],dp[i][1]);
113+
ll mini=min(pos[0],pos[1]);
114+
ans+=i-mini;
137115
}
138-
ans++;
139116
cout<<ans<<endl;
140117
return 0;
141118
}

Codeforces/1538A-Stone_Game.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
#include <bits/stdc++.h>
7+
#include <ext/pb_ds/assoc_container.hpp>
8+
#include <ext/pb_ds/tree_policy.hpp>
9+
using namespace std;
10+
using namespace __gnu_pbds;
11+
typedef long long ll ;
12+
typedef unsigned long long ull;
13+
typedef vector<ll> vl;
14+
typedef vector<vector<ll>> vvl;
15+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
16+
/* Abbrevations */
17+
#define ff first
18+
#define ss second
19+
#define mp make_pair
20+
#define line cout<<endl;
21+
#define pb push_back
22+
#define Endl "\n"
23+
// loops
24+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
25+
// Some print
26+
#define no cout<<"NO"<<endl;
27+
#define yes cout<<"YES"<<endl;
28+
// sort
29+
#define all(V) (V).begin(),(V).end()
30+
#define srt(V) sort(all(V))
31+
#define srtGreat(V) sort(all(V),greater<ll>())
32+
// some extra
33+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
34+
#define precision(x) cout<<fixed<<setprecision(x);
35+
#define sz(V) ll(V.size())
36+
// template
37+
template <typename T>
38+
T mymax(T x,T y)
39+
{
40+
return (x>y)?x:y;
41+
}
42+
// function
43+
ll power(ll x,ll y,ll mod)
44+
{
45+
ll res=1;
46+
// x=x%mod;
47+
while(y>0)
48+
{
49+
if(y%2==1)
50+
{
51+
res*=x;
52+
// res=res%mod;
53+
}
54+
y/=2; x*=x; // x=x%mod;
55+
}
56+
return res;
57+
}
58+
ll str_to_num(string s)
59+
{
60+
stringstream pk(s);
61+
ll num;
62+
pk>>num;
63+
return num;
64+
}
65+
66+
string num_to_str(ll num)
67+
{
68+
return to_string(num);
69+
}
70+
// datatype definination
71+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
72+
class Point
73+
{
74+
public:
75+
ll x;
76+
ll y;
77+
ll z;
78+
ll getsum()
79+
{
80+
return x+y+z;
81+
}
82+
};
83+
/* ascii value
84+
A=65,Z=90,a=97,z=122
85+
*/
86+
/* --------------------MAIN PROGRAM----------------------------*/
87+
// to run ctrl+b
88+
const ll INF=1e18;
89+
const ll mod1=1e9+7;
90+
const ll mod2=998244353;
91+
92+
93+
// Techniques :
94+
// divide into cases, brute force, pattern finding
95+
// sort, greedy, binary search, two pointer
96+
// transform into graph
97+
98+
// Experience :
99+
// Cp is nothing but only observation and mathematics.
100+
101+
ll solve()
102+
{
103+
ll n;
104+
cin>>n;
105+
vector<pair<ll,ll>> v(n);
106+
for(ll i=0;i<n;i++){
107+
ll temp;
108+
cin>>temp;
109+
v[i]={temp,i+1};
110+
}
111+
srt(v);
112+
ll mini=v[0].ss;
113+
ll maxo=v[n-1].ss;
114+
// cout<<"mini "<<mini<<" and maxo "<<maxo<<endl;
115+
ll ans=min({(max({v[0].ss,v[n-1].ss})),(max({n+1-v[0].ss,n+1-v[n-1].ss})),(v[0].ss+n+1-v[n-1].ss),(n+1-v[0].ss+v[n-1].ss)});
116+
cout<<ans<<endl;
117+
return 0;
118+
}
119+
120+
int main()
121+
{
122+
speed;
123+
/* #ifndef ONLINE_JUDGE
124+
freopen("input.txt","r",stdin);
125+
freopen("output.txt","w",stdout);
126+
#endif */
127+
ll TestCase=1;
128+
cin>>TestCase;
129+
while(TestCase--)
130+
{
131+
solve();
132+
}
133+
}
134+
/* -----------------END OF PROGRAM --------------------*/
135+
/*
136+
* stuff you should look before submission
137+
* constraint and time limit
138+
* int overflow
139+
* special test case (n=0||n=1||n=2)
140+
* don't get stuck on one approach if you get wrong answer
141+
*/
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/*
2+
written by Pankaj Kumar.
3+
country:-INDIA
4+
Institute: National Institute of Technology, Uttarakhand
5+
*/
6+
#include <bits/stdc++.h>
7+
#include <ext/pb_ds/assoc_container.hpp>
8+
#include <ext/pb_ds/tree_policy.hpp>
9+
using namespace std;
10+
using namespace __gnu_pbds;
11+
typedef long long ll ;
12+
typedef unsigned long long ull;
13+
typedef vector<ll> vl;
14+
typedef vector<vector<ll>> vvl;
15+
#define speed cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(0);
16+
/* Abbrevations */
17+
#define ff first
18+
#define ss second
19+
#define mp make_pair
20+
#define line cout<<endl;
21+
#define pb push_back
22+
#define Endl "\n"
23+
// loops
24+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
25+
// Some print
26+
#define no cout<<"NO"<<endl;
27+
#define yes cout<<"YES"<<endl;
28+
// sort
29+
#define all(V) (V).begin(),(V).end()
30+
#define srt(V) sort(all(V))
31+
#define srtGreat(V) sort(all(V),greater<ll>())
32+
// some extra
33+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
34+
#define precision(x) cout<<fixed<<setprecision(x);
35+
#define sz(V) ll(V.size())
36+
// template
37+
template <typename T>
38+
T mymax(T x,T y)
39+
{
40+
return (x>y)?x:y;
41+
}
42+
// function
43+
ll power(ll x,ll y,ll mod)
44+
{
45+
ll res=1;
46+
// x=x%mod;
47+
while(y>0)
48+
{
49+
if(y%2==1)
50+
{
51+
res*=x;
52+
// res=res%mod;
53+
}
54+
y/=2; x*=x; // x=x%mod;
55+
}
56+
return res;
57+
}
58+
ll str_to_num(string s)
59+
{
60+
stringstream pk(s);
61+
ll num;
62+
pk>>num;
63+
return num;
64+
}
65+
66+
string num_to_str(ll num)
67+
{
68+
return to_string(num);
69+
}
70+
// datatype definination
71+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
72+
class Point
73+
{
74+
public:
75+
ll x;
76+
ll y;
77+
ll z;
78+
ll getsum()
79+
{
80+
return x+y+z;
81+
}
82+
};
83+
/* ascii value
84+
A=65,Z=90,a=97,z=122
85+
*/
86+
/* --------------------MAIN PROGRAM----------------------------*/
87+
// to run ctrl+b
88+
const ll INF=1e18;
89+
const ll mod1=1e9+7;
90+
const ll mod2=998244353;
91+
92+
93+
// Techniques :
94+
// divide into cases, brute force, pattern finding
95+
// sort, greedy, binary search, two pointer
96+
// transform into graph
97+
98+
// Experience :
99+
// Cp is nothing but only observation and mathematics.
100+
101+
ll solve()
102+
{
103+
ll n;
104+
cin>>n;
105+
vl v(n);
106+
ll sum=0;
107+
set<ll> s;
108+
for(ll i=0;i<n;i++){
109+
cin>>v[i];
110+
sum+=v[i];
111+
s.insert(v[i]);
112+
}
113+
if(sum%n)
114+
cout<<-1<<endl;
115+
else{
116+
if(sz(s)==1)
117+
cout<<0<<endl;
118+
else{
119+
ll ans=0;
120+
ll need=sum/n;
121+
for(ll i=0;i<n;i++){
122+
if(v[i]>need)
123+
ans++;
124+
}
125+
cout<<ans<<endl;
126+
}
127+
}
128+
return 0;
129+
}
130+
131+
int main()
132+
{
133+
speed;
134+
/* #ifndef ONLINE_JUDGE
135+
freopen("input.txt","r",stdin);
136+
freopen("output.txt","w",stdout);
137+
#endif */
138+
ll TestCase=1;
139+
cin>>TestCase;
140+
while(TestCase--)
141+
{
142+
solve();
143+
}
144+
}
145+
/* -----------------END OF PROGRAM --------------------*/
146+
/*
147+
* stuff you should look before submission
148+
* constraint and time limit
149+
* int overflow
150+
* special test case (n=0||n=1||n=2)
151+
* don't get stuck on one approach if you get wrong answer
152+
*/

0 commit comments

Comments
 (0)