1313B - Different Rules - CodeForces Solution


constructive algorithms greedy implementation math *1700

Please click on ads to support us..

Python Code:

t=int(input())
 
for k in range(t):
    nit,x,y=map(int,input().split())
    print(max(1,min(nit,x+y-nit+1)),min(x+y-1,nit))

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long int
#define cin std::cin
#define cout std::cout
vector<vector<pair<int,int>>>graph;
vector<int>vis,dist;
vector<vector<int>> tree;
// #define int long long int

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
void cina(int arr[],int n){int i=0;
for(i=0;i<n;i++){
    cin>>arr[i];
}
}
void couta(int arr[],int n){
    for(int i=0;i<n;i++){
        cout<<arr[i]<<" ";
    }cout << "\n";
}

int cdiv(int a, int b) { return a / b + ((a ^ b) > 0 && a % b); }//for ceil
//. . . . . . . . 
//...............
int mod = 1e9 + 7;
class segtree {
 public:
	int neutral = 0;
	struct node {
		// don't forget to set default value (used for leaves)
		// not necessarily neutral element!
		// Enter here
        int mi = 0 , ma = 0 , sum = 0;
		void apply(int l, int r,int v) { // Value of a single node (used by build and update)
            
            mi = v;
			ma = v;
			sum = v;
            // Enter here
		}
		void push(int l, int r, int v) { // Lazy propogation (used by add)
			//sum += v*(r - l + 1); 
			//add += v;
		}
	};
	node unite(const node &a, const node &b) const { // Set combination operation
		node res;
		res.sum = a.sum + b.sum;
        res.mi = min(a.mi ,(a.sum + b.mi));
		res.ma = max(a.ma , a.sum + b.ma);
		
		// Enter here
		return res;
	}
	inline void push(int x, int l, int r) {
		//int y = (l + r) >> 1;
		//int z = x + ((y - l + 1) << 1);
		//if (tree[x].add != neutral) {
			//tree[x + 1].push(l, y, tree[x].add);
			//tree[z].push(y + 1, r, tree[x].add);
			//tree[x].add = neutral;
		//}
		
		// Don't forget to uncomment node.push
	}
	inline void pull(int x, int z) {
		tree[x] = unite(tree[x + 1], tree[z]);
	}
 
	int n;
	vector<node> tree;
	void build(int x, int l, int r) {
		if (l == r) {
			return;
		}
		int y = (l + r) >> 1;
		int z = x + ((y - l + 1) << 1);
		build(x + 1, l, y);
		build(z, y + 1, r);
		pull(x, z);
	}
	template <typename M>
	void build(int x, int l, int r, const vector<M> &v) {
		if (l == r) {
			tree[x].apply(l, r, v[l]);
			return;
		}
		int y = (l + r) >> 1;
		int z = x + ((y - l + 1) << 1);
		build(x + 1, l, y, v);
		build(z, y + 1, r, v);
		pull(x, z);
	}
	node find(int x, int l, int r, int lx, int rx) {
		if (lx <= l && r <= rx) {
			return tree[x];
		}
		int y = (l + r) >> 1;
		int z = x + ((y - l + 1) << 1);
		push(x, l, r);
		node res{};
		if (rx <= y) {
			res = find(x + 1, l, y, lx, rx);
		} else {
			if (lx > y) {
			res = find(z, y + 1, r, lx, rx);
			} else {
			res = unite(find(x + 1, l, y, lx, rx), find(z, y + 1, r, lx, rx));
			}
		}
		pull(x, z);
		return res;
	}
	template <typename... M>
	void update(int x, int l, int r, int lx, int rx, const M&... v) {
		if (lx <= l && r <= rx) {
			tree[x].apply(l, r, v...);
			return;
		}
		int y = (l + r) >> 1;
		int z = x + ((y - l + 1) << 1);
		push(x, l, r);
		if (lx <= y) {
			update(x + 1, l, y, lx, rx, v...);
		}
		if (rx > y) {
			update(z, y + 1, r, lx, rx, v...);
		}
		pull(x, z);
	}
	template <typename... M>
	void add(int x, int l, int r, int lx, int rx, const M&... v) {
		if (lx <= l && r <= rx) {
			tree[x].push(l, r, v...);
			return;
		}
		int y = (l + r) >> 1;
		int z = x + ((y - l + 1) << 1);
		push(x, l, r);
		if (lx <= y) {
			add(x + 1, l, y, lx, rx, v...);
		}
		if (rx > y) {
			add(z, y + 1, r, lx, rx, v...);
		}
		pull(x, z);
	}
	segtree(int _n) : n(_n) {
		assert(n > 0);
		tree.resize(2 * n - 1);
		build(0, 0, n - 1);
	}
	template <typename M>
	segtree(const vector<M> &v) {
		n = v.size();
		assert(n > 0);
		tree.resize(2 * n - 1);
		build(0, 0, n - 1, v);
	}
	node find(int p) { // value at index p
		assert(0 <= p && p <= n - 1);
		return find(0, 0, n - 1, p, p);
	}
	template <typename... M>
	void update(int lx, int rx, const M&... v) {
		assert(0 <= lx && lx <= rx && rx <= n - 1);
		update(0, 0, n - 1, lx, rx, v...);
	}
	void add(int i, int v){
		assert(i >= 0 && i < n);
		add(i,i,v);
	}
	// All functions below
	void update(int i, int v){ // Sets value at index i to v
		assert(i >= 0 && i < n);
		update(i,i,v);
	}
	template <typename... M>
	void add(int lx, int rx, const M&... v) { // adds v to a[lx to rx]
		assert(0 <= lx && lx <= rx && rx <= n - 1);
		add(0, 0, n - 1, lx, rx, v...);
	}
	node find(int lx, int rx) { // value of lx to rx
		assert(0 <= lx && lx <= rx && rx <= n - 1);
		return find(0, 0, n - 1, lx, rx);
	}
};


vector<pair<int,int>>ran;
void dijkstra(int ind , int m){
		set<pair<int,int>>s;
		s.insert({0,ind});
		dist[ind]=0;
		vector<int> vis1(m , 0);
		while(s.size()>0){
				pair<int,int>cur=*s.begin();
				int i=cur.second;
				int wt=cur.first;
				s.erase(cur);
				if(vis1[i])continue;
				vis1[i]=1;
				for(auto it:graph[i]){
					if(dist[i]+it.second<dist[it.first]){
						dist[it.first]=dist[i]+it.second;
						s.insert({dist[it.first],it.first});
					}
				}
				
		}
	return;
}
int  prims(int n){
	set<pair<int,int>>s;
	s.insert({0,0});
	vector<int>dist2(n,1e17);
	dist2[0]=0;
	vector<int>mst(n,0);
	int c=0;
	
	while(!s.empty()){
		pair<int,int>pi=*s.begin();
		s.erase(pi);
		int wt=pi.first;
		int v=pi.second;
		if(mst[v]==1){continue;}
		mst[v]=1;
		for(auto it:graph[v]){
			int w=it.second;
			if(mst[it.first]==0&&dist2[it.first]>w){
				dist2[it.first]=w;
				c+=w;
				
				s.insert({w,it.first});
				
			}
		}
	}
c=0;
	for(auto it:dist2){
		c+=it;
	}
	return c;
}
class LCA{
	template<typename T>
	struct SparseTable{
		vector<vector<T>> table;
		vector<int> logtable;
		function<T(T, T)> merge_func;
		SparseTable(){}
		SparseTable(const vector<T> &vec, const function<T(T, T)> &f): merge_func(f){
			int maxlength = 0;
			while((1 << (maxlength+1)) <= (int)vec.size()) maxlength++;
			table.resize(maxlength+1, vector<T>(vec.size()));
			logtable.resize(vec.size()+1);
			for(int i = 0;i < maxlength + 1;i++){
				for(int j = 0;j < (int)vec.size() - (1 << i) + 1;j++){
					if(i)table[i][j] = merge_func(table[i-1][j], table[i-1][j + (1 << (i-1))]);
					else table[i][j] = vec[j];
				}
			}
			for(int i = 2;i <= (int)vec.size();i++)logtable[i] = logtable[i >> 1]+1;
		}
		T query(int l, int r){
			assert(l < r);
			int length = r - l;
			return merge_func(table[logtable[length]][l], table[logtable[length]][r - (1 << logtable[length])]);
		}
	};
	private:
	int a = 0, b = 0;
	vector<int> begin, number, tour, dep, rev;
	SparseTable<int> table;
	void init_dfs(int v, int p, const vector<vector<int>> &g){
		number[v] = b;rev[b] = v;b++;
		for(auto t : g[v]){
			if(t == p) continue;
			dep[t] = dep[v] + 1;
			init_dfs(t, v, g);
			tour.push_back(number[v]);
			if(begin[v] == -1)begin[v] = a;
			a++;
		}
		if(begin[v] == -1)begin[v] = a;
	}
	public:
	//initialization O(NlogN)
	LCA(const vector<vector<int>> &g, int root = 0):begin(g.size(), -1), number(g.size()), dep(g.size(), -1), rev(g.size()){
		dep[root] = 0;
		init_dfs(root, -1, g);
		table = SparseTable<int>(tour, [](int x, int y){return min(x, y);});
	}
	//O(1) per query
	int lca(int u, int v){
		if(begin[u] == begin[v]) return (dep[u] > dep[v] ? v : u);
		if(begin[u] > begin[v]) swap(u, v);
			return rev[table.query(begin[u], begin[v]+1)];
	}
	int depth(int v){
		return dep[v];
	}
	int dist(int u, int v){
		return dep[u] + dep[v] - 2*dep[lca(u, v)];
	}
};
//--------------//

void cleanup()
{
	graph.clear(), vis.clear(), dist.clear(), ran.clear() , tree.clear();
}
int power(int a,int m,int p){
		int ans=1;
		a=a%p;
		while(m>0){
			if(m%2){
				ans=(ans*a)%p;
			}
			m=m/2;
			a=(a*a)%p;
		}
		return ans % p;
}
int fac(int n,int p){
		int ans=1;
		for(int i=1;i<=n;i++){
			ans=(ans*i)%p;
		}
		return ans;
}
int  nCrModPFermat(int n,int r,int p){
	        if(n < r){
				return 0;
			}
			if(r==0){
				return 1;
			}

			int x=fac(n,p);
			int y=fac(n-r,p),z=fac(r,p);
			y=power(y,p-2,p);
			z=power(z,p-2,p);
			x=(x*y)%p;
			int ans=(x*z)%p;
			return ans;
} 
struct DSU{
		vector< int > el;
		DSU(int n){
			el=vector<int>(n,-1);
		}
		int find(int x){
				if(el[x]<0){
					return x;
				}
				return el[x]=find(el[x]);
		}
		int siz(int x){
			return -el[find(x)];
		}
		bool unite(int a,int b){
			int p1=find(a);
			int p2=find(b);
			if(p1==p2){
				return false;
			}
			if(el[p1]>el[p2]){
				swap(p1,p2);
			}
			el[p1]+=el[p2];
			el[p2]=p1;
			return true;
		}
};
bool cmp(pair<int,int>& p1, pair<int,int>& p2){
	if(p1.first == p2.first){
		return p1.second > p2.second;
	}
	else{
		return p1.first < p2.first;
	}
}

vector<int>primes;
void findprimes(){
	int s = sqrt(1e7) + 10;
	bool sieve[s];
	fill(sieve , sieve + s , true);
	for(int i = 2; i < s; i++){
		if(sieve[i] == false)continue;
		primes.push_back(i);
		for(int j = i * i; j < s; j += i){
			sieve[j] = false;
		}
	}
}

//function which gives all prime factors of a number upto 1e9
vector<int> numprimes(int a){
	int s = sqrt(a);
	vector<int> res;
	for(auto p : primes){
			if(p > s)break;
			if(a % p == 0){
				res.push_back(p);
				
				a = a / p;
				while(a % p == 0){
					res.push_back(p);
					a /= p;
				}
			}
		
	}
	if(a >= 2){
		res.push_back(a);
	}
	return res;
}

int md =  998244353;


void testcase()
{
	int n , q , m , k;
	int a , b;
	cin >> n >> a >> b;
	
	m = max(1ll , a + b - n + 1);
	m = min(n , m);
	cout << m << " ";
	m = min(a + b - 1 , n);
	cout << m << "\n";
}
///////////////////////
int32_t main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int tt = 1,h = 0;
	cin>>tt;
	
	//findprimes();
	
	while(tt--){
		
		testcase();
		cleanup();
	}
	return (0-0);
}


Comments

Submit
0 Comments
More Questions

791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST
445. Add Two Numbers II
442. Find All Duplicates in an Array
437. Path Sum III
436. Find Right Interval
435. Non-overlapping Intervals
406. Queue Reconstruction by Height
380. Insert Delete GetRandom O(1)
332. Reconstruct Itinerary
368. Largest Divisible Subset
377. Combination Sum IV
322. Coin Change
307. Range Sum Query - Mutable
287. Find the Duplicate Number
279. Perfect Squares
275. H-Index II
274. H-Index
260. Single Number III
240. Search a 2D Matrix II