60A - Where Are My Flakes - CodeForces Solution


implementation two pointers *1300

Please click on ads to support us..

Python Code:

n, m = [int(i) for i in input().split()]

hints = [input() for _ in range(m)]

l = [0 for i in range(n)]

total = 0

for h in hints:
    s = h.split()
    
    if "right" in s:
        for i in range(0, int(s[-1])): l[i] = -1
        
    if "left" in s:
        for i in range(int(s[-1])-1, n): l[i] = -1
        

for i in l:
    if i == 0: total += 1
    
if total == 0: print(-1)
else: print(total)

C++ Code:

#pragma GCC optimization("Ofast")
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define ELZOMOR cin.tie(0)->sync_with_stdio(0);
#define ll  long long
using namespace std;
void file() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
#ifdef ONLINE_JUDGE
	cin.tie(0)->sync_with_stdio(0);
#endif
}

ll mod = 1e9;
ll N = 1e6 + 5;
ll fact[(int)1e6 + 5];
ll inv[(int)1e6 + 5];
ll PowMod(ll x, ll y) {
	ll res = 1;
	while (y) {
		if (y & 1) res = (res * x) % mod;
		y >>= 1, x = (x * x) % mod;
	}
	return res;
}
void pre() {
	fact[0] = inv[0] = 1;
	for (ll i = 1; i < N; i++)
		fact[i] = (fact[i - 1] * i) % mod,
		inv[i] = PowMod(fact[i], mod - 2);
}
ll a, b, c, n, r, q, m, h,x,y, k;
ll modInv(ll n) {
	return PowMod(n, mod - 2);
}
ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a % b);
}
int DR[] = { 0,0,-1,1 };
int DC[] = { 1,-1,0,0 };
char arr[1000 + 2][1000 + 2];
int vis[1000 + 2][1000 + 2];
int f = 0;
void solve() {
	string one, two, three, four, five;
	cin >> n >> m;
	int lm = 1,rr=n;
	for (int i = 0; i < m; i++)
	{
		cin >> one >> two >> three >> four >> five;
		if (three == "right") {
			lm = max(lm,stoi(five) + 1);
		}
		else if (three == "left") {
		
			rr = min(stoi(five) - 1,rr);
		}
	}
	if (lm<1 || rr>n||lm>rr) { cout << -1; return; }
	cout << (rr - lm+1 );
}
int main() {
	file();
	ELZOMOR;

	int t = 1;
	//cin>>t;

	while (t--) {
		solve();
	}
}


Comments

Submit
0 Comments
More Questions

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
238. Product of Array Except Self
229. Majority Element II
222. Count Complete Tree Nodes
215. Kth Largest Element in an Array