Comparison between Cin&Cout and Scanf&Printf and C++ versions

Правка en2, от Adhami, 2018-02-13 22:01:19

I used 600B - Queries about less or equal elements to test those and used the same code but with change in input and output streams.

  Cin And Cout   Scanf And Printf
 GNU C++ 5.1.0  358ms Submission:35214275  155ms Submission:35214230
 GNU C++11 5.1.0  343ms Submission:35214311  156ms Submission:35214295
 GNU C++14 6.4.0  187ms Submission:35214315  186ms Submission:35214352
 GNU C++17 7.2.0  187ms Submission:35214359  187ms Submission:35214322

Looks like that scanf&printf is almost the same but cin&cout is slower under c++14.

Don't use cin&cout under c++14 to avoid TLE.

Scanf And Printf Code

#include <bits/stdc++.h>
using namespace std;
int n,m,val;
vector<int> a;
int main() {
	scanf("%d%d",&n,&m);
	for(int i = 0;i < n;i++){
		scanf("%d",&val);
		a.push_back(val);
	}
	sort(a.begin(),a.end());
	for(int i = 0;i < m;i++){
		int b;
		scanf("%d",&b);
		printf("%d ",upper_bound(a.begin(),a.end(),b)-a.begin());
	}
	return 0;
}

Cin And Cout Code

#include <bits/stdc++.h>
using namespace std;
int n,m,val;
vector<int> a;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin >> n >> m;
	for(int i = 0;i < n;i++){
		cin >> val;
		a.push_back(val);
	}
	sort(a.begin(),a.end());
	for(int i = 0;i < m;i++){
		int b;
		cin >> b;
		cout << upper_bound(a.begin(),a.end(),b)-a.begin() << " ";
	}
	return 0;
}
Теги cin.tie, ios_base

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en3 Английский Adhami 2018-02-15 00:18:27 58
en2 Английский Adhami 2018-02-13 22:01:19 148
en1 Английский Adhami 2018-02-13 21:25:39 2189 Initial revision (published)