Skip to content

Commit c2ab4f9

Browse files
committed
practice question
1 parent 7a4c89b commit c2ab4f9

File tree

7 files changed

+833
-7
lines changed

7 files changed

+833
-7
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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+
// loops
23+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
24+
// Some print
25+
#define no cout<<"NO"<<endl;
26+
#define yes cout<<"YES"<<endl;
27+
// sort
28+
#define all(V) (V).begin(),(V).end()
29+
#define srt(V) sort(all(V))
30+
#define srtGreat(V) sort(all(V),greater<ll>())
31+
// some extra
32+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} cout<<endl;
33+
#define precision(x) cout<<fixed<<setprecision(x);
34+
#define sz(V) ll(V.size())
35+
// template
36+
template <typename T>
37+
T mymax(T x,T y)
38+
{
39+
return (x>y)?x:y;
40+
}
41+
// function
42+
ll power(ll x,ll y,ll mod)
43+
{
44+
ll res=1;
45+
// x=x%mod;
46+
while(y>0)
47+
{
48+
if(y%2==1)
49+
{
50+
res*=x;
51+
// res=res%mod;
52+
}
53+
y/=2; x*=x; // x=x%mod;
54+
}
55+
return res;
56+
}
57+
ll str_to_num(string s)
58+
{
59+
stringstream pk(s);
60+
ll num;
61+
pk>>num;
62+
return num;
63+
}
64+
string num_to_str(ll num)
65+
{
66+
return to_string(num);
67+
}
68+
// datatype definination
69+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
70+
/* ascii value
71+
A=65,Z=90,a=97,z=122
72+
*/
73+
74+
/* Some syntax
75+
//Syntax to create a min heap for priority queue
76+
//priority_queue <int, vector<int>, greater<int>>pq;
77+
*/
78+
79+
80+
/* --------------------MAIN PROGRAM----------------------------*/
81+
// to run ctrl+b
82+
const ll INF=1e18;
83+
const ll mod1=1e9+7;
84+
const ll mod2=998244353;
85+
// Techniques :
86+
// divide into cases, brute force, pattern finding
87+
// sort, greedy, binary search, two pointer
88+
// transform into graph
89+
// Experience :
90+
// Cp is nothing but only observation and mathematics.
91+
92+
const ll N=500000+15;
93+
vl ans(N,0);
94+
set<ll> s;
95+
96+
void dfs(vector<vector<ll>>&graph,ll node,vector<bool>&check){
97+
if(check[node]){
98+
return ;
99+
}
100+
check[node]=true;
101+
s.insert(node);
102+
for(auto x:graph[node]){
103+
if(check[x]==false){
104+
dfs(graph,x,check);
105+
}
106+
}
107+
return ;
108+
}
109+
110+
ll solve()
111+
{
112+
ll n,m;
113+
cin>>n>>m;
114+
vector<vector<ll>> graph(n+1);
115+
for(ll i=0;i<m;i++){
116+
ll t;
117+
cin>>t;
118+
vl v(t);
119+
forin(v,t);
120+
if(t>1){
121+
for(ll j=1;j<t;j++){
122+
graph[v[j]].pb(v[0]);
123+
graph[v[0]].pb(v[j]);
124+
}
125+
}
126+
}
127+
vector<bool>check1(n+1,false);
128+
129+
for(ll i=1;i<=n;i++){
130+
if(check1[i]==false){
131+
dfs(graph,i,check1);
132+
for(auto x:s){
133+
ans[x]=sz(s);
134+
}
135+
s.clear();
136+
}
137+
}
138+
for(ll i=1;i<=n;i++){
139+
cout<<ans[i]<<" ";
140+
}
141+
line;
142+
return 0;
143+
}
144+
int main()
145+
{
146+
speed;
147+
/* #ifndef ONLINE_JUDGE
148+
freopen("input.txt","r",stdin);
149+
freopen("output.txt","w",stdout);
150+
#endif */
151+
ll TestCase=1;
152+
//cin>>TestCase;
153+
while(TestCase--)
154+
{
155+
solve();
156+
}
157+
}
158+
/* -----------------END OF PROGRAM --------------------*/
159+
/*
160+
* stuff you should look before submission
161+
* constraint and time limit
162+
* int overflow
163+
* special test case (n=0||n=1||n=2)
164+
* don't get stuck on one approach if you get wrong answer
165+
*/

Codeforces/1183C-Computer_Game.cpp

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
// loops
23+
#define forin(arr,n) for(ll i=0;i<n;i++) cin>>arr[i];
24+
// Some print
25+
#define no cout<<"NO"<<endl;
26+
#define yes cout<<"YES"<<endl;
27+
// sort
28+
#define all(V) (V).begin(),(V).end()
29+
#define srt(V) sort(all(V))
30+
#define srtGreat(V) sort(all(V),greater<ll>())
31+
// some extra
32+
#define printv(v) for(ll i=0;i<ll(v.size());i++){cout<<v[i]<<" ";} line;
33+
#define precision(x) cout<<fixed<<setprecision(x);
34+
#define sz(V) ll(V.size())
35+
// template
36+
template <typename T>
37+
T mymax(T x,T y)
38+
{
39+
return (x>y)?x:y;
40+
}
41+
// function
42+
ll power(ll x,ll y,ll mod)
43+
{
44+
ll res=1;
45+
// x=x%mod;
46+
while(y>0)
47+
{
48+
if(y%2==1)
49+
{
50+
res*=x;
51+
// res=res%mod;
52+
}
53+
y/=2; x*=x; // x=x%mod;
54+
}
55+
return res;
56+
}
57+
ll str_to_num(string s)
58+
{
59+
stringstream pk(s);
60+
ll num;
61+
pk>>num;
62+
return num;
63+
}
64+
string num_to_str(ll num)
65+
{
66+
return to_string(num);
67+
}
68+
// datatype definination
69+
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
70+
/* ascii value
71+
A=65,Z=90,a=97,z=122
72+
*/
73+
74+
/* Some syntax
75+
//Syntax to create a min heap for priority queue
76+
//priority_queue <int, vector<int>, greater<int>>pq;
77+
*/
78+
/* --------------------MAIN PROGRAM----------------------------*/
79+
// to run ctrl+b
80+
const ll INF=1e18;
81+
const ll mod1=1e9+7;
82+
const ll mod2=998244353;
83+
// Techniques :
84+
// divide into cases, brute force, pattern finding
85+
// sort, greedy, binary search, two pointer
86+
// transform into graph
87+
88+
// Experience :
89+
// Cp is nothing but only observation and mathematics.
90+
91+
bool check(ll k,ll a,ll b,ll n,ll count){
92+
ll temp=k;
93+
temp-=(count*a);
94+
temp-=(n-count)*b;
95+
if(temp<=0){
96+
return false;
97+
}
98+
else{
99+
return true;
100+
}
101+
}
102+
103+
ll solve()
104+
{
105+
ll k,n,a,b;
106+
cin>>k>>n>>a>>b;
107+
ll low=0,high=n;
108+
while(high>low){
109+
ll mid=(low+high)/2;
110+
if(check(k,a,b,n,mid)){
111+
low=mid+1;
112+
}
113+
else{
114+
high=mid;
115+
}
116+
}
117+
if(!check(k,a,b,n,0)){
118+
cout<<-1<<endl;
119+
return 0;
120+
}
121+
if(check(k,a,b,n,n)){
122+
cout<<n<<endl;
123+
return 0;
124+
}
125+
low--;
126+
cout<<low<<endl;
127+
return 0;
128+
}
129+
int main()
130+
{
131+
speed;
132+
/* #ifndef ONLINE_JUDGE
133+
freopen("input.txt","r",stdin);
134+
freopen("output.txt","w",stdout);
135+
#endif */
136+
ll TestCase=1;
137+
cin>>TestCase;
138+
while(TestCase--)
139+
{
140+
solve();
141+
}
142+
}
143+
/* -----------------END OF PROGRAM --------------------*/
144+
/*
145+
* stuff you should look before submission
146+
* constraint and time limit
147+
* int overflow
148+
* special test case (n=0||n=1||n=2)
149+
* don't get stuck on one approach if you get wrong answer
150+
*/

0 commit comments

Comments
 (0)