Блог пользователя SeniorBOTx

Автор SeniorBOTx, 2 года назад, По-английски

We know that we waste lot of time to print or cout things in C++.

Suppose you have to print 1, 2, 3, 4.

cout << 1 << " " << 2 << " " << 3 << " " << 4 << endl;

Or print pair in vector . It take lot of time . I know if you are typing speed is 80+ then what ? You still wasting time ? So I decide to make print function same as python to Overcome this.

void Print(int32_t x) {cout << x << " ";} void Print(char x) {cout << x << " ";} void Print(const char *x) {cout << x << " ";} void Print(double x) {cout << x << " ";} void Print(int x) {cout << x << " ";} void print() {cout << endl;}
void Print(bool x) {cout << (x ? "YES" : "NO");} void Print(const string &x) {cout << x << " ";}  template<typename T, typename V> void Print(const pair<T, V> x) {cout << x.F << " " << x.S << endl;}
template<typename T> void Print(const T &x) {for (auto &i : x) Print(i);} template <typename T, typename... V> void print(T t, V... v) { Print(t); print(v...);}


cout << 1 << " " << 2 << " " << 3 << " " << 4 << endl;
print(1,2,3,4); //will do the same effect.

vector<int> v;
for(auto x:v) cout << x << " "; cout << endl;  // 1 2 3 4...
print(v)  // same effect as above

set<int> s;
for(auto x:s) cout << x << " "; cout << endl;  // 1 2 3 4 5 6.... 
print(s);

pair<int,int> z = { 1, 3};
cout << z.first << " " << z.second << endl;   // 1 3
print(z);  // 1 3

string s1,s2,s3;
cout << s1 << " " << s2 << " " << s3 << endl;
print(s1,s2,s3);

bool flag = true;
if (flag) cout << "YES" << endl;
else cout << "NO" << endl;

print(flag) // same effect as above

// Even you can put any condition here .
print(1 > 0);  // YES
print(n&1);    // if n is odd output is YES otherwise NO


hope this will save time , happy cp ^ _ *

Полный текст и комментарии »

  • Проголосовать: нравится
  • +4
  • Проголосовать: не нравится

Автор SeniorBOTx, история, 3 года назад, По-английски

In step 3 , question number 1 : I write two codes very similar to each , As i am a newbie can you help me out to find what is difference in two code. I got ac in one submission and wrong answer in other.

Also if you don not know what difference between two code, give this blog a upvote so many people can see it.

[problem:307094A]

AC : [submission:157416384]

WA : [submission:157415655]

void solve() 
{
	int n, p; cin >> n >> p;
	int sum{}, ans = 0 , res{}, index{}, prefix{};

	vector<int> v(2 * n);
	rep(i, 0, n) {
		cin >> v[i];
		v[i + n] = v[i];
		sum += v[i];
	}

	if (sum < p) {
		res = p / sum * n;
		p %= sum;
	}
	if (p != 0) {
		ans = INT_MAX;
		for (int r = 0, l = 0; r < 2 * n ; r++ ) {
			prefix += v[r];
			while (l < n and prefix >= p) {
				if (ans > r - l + 1) {
					ans = r - l + 1;
					index = l ;
				}
				prefix -= v[l];
				l++;
			}
		}
	}
	cout << index + 1 << " " << ans  + res << endl;


}

WA :

void solve() 
{
	int n, p; cin >> n >> p;
	int sum{}, ans = LLONG_MAX , res{}, index{}, prefix{};

	vector<int> v(2 * n);
	rep(i, 0, n) {
		cin >> v[i];
		v[i + n] = v[i];
		sum += v[i];
	}

	if (sum < p) {
		res = p / sum * n;
		p %= sum;
	}

	for (int r = 0, l = 0; r < 2 * n ; r++ ) {
		prefix += v[r];
		while (l < n and prefix >= p) {
			if (ans > r - l + 1) {
				ans = r - l + 1;
				index = l + 1;
			}
			prefix -= v[l]; l++;
		}
	}

	cout << index << " " << ans  + res << endl;


}

[meanwhile this is my first time here]

Полный текст и комментарии »

  • Проголосовать: нравится
  • -1
  • Проголосовать: не нравится