You're right, our tests also proved there's something wrong with (our?) g++-3.3. I think it's not exactly that they made some great improvements in 3.4: 2.95 has similar speed as 3.4.
Anyway, I was planning to switch spoj C/C++ compilers soon, probably also using stlport instead of STL from gcc (but it still needs some testing).
I found this thread rather interesting on this topic:
daniweb.com/techtalkforums/thread40450.html28
I found this to be the most interesting answers:
">and scanf() and printf() from their prototype know what kind of variables to expect.
And in what way do you think that the runtime type selection from a format string with scanf/printf is faster than selecting an overloaded function at compile time?"
any comments about this thread?
My logic tells me that iostream should be faster than printf, since how the arguments are parsed(run-time vs compile-time).
and to back it up: quora.com/C++-programming-la ... ME&share=117
I almost always use printf/scanf, because they are a lot faster. When my code gets TLE and the time complexity of the solution is similar to the complexity of the input, I use getchar_unlocked() and parse the input by myself. Even scanf -- faster than cin -- is still too slow. In SPOJ BR I got 0.5s in a problem with scanf, 0.17s with getchar and 0.02s with getchar_unlocked().
It means that, if the input is O(n²) and the expected solution is O(n²), maybe you can solve it in O(n² lg n) time just by parsing the input yourself (so, you don't spend much time with the input and get a lot of extra time for the solution). PS: I don't use atoi. Converting to integer WHILE reading the input is faster... for each algarism c you read, you update your integer to: x = 10*x + (c-'0').
Dobry wieczór. Brałem pod uwagę wszystko co było napisane na forum. Ktoś pomoże?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
int ile,x,y,z,i;
float miesiac;
scanf("%d",&ile);
for(i=0;i<ile;i++){
scanf("%d",&x);
scanf("%d",&y);
scanf("%d",&z);
miesiac=x+y;
miesiac=miesiac-z*y;
miesiac=miesiac/(z-1);
miesiac=miesiac*12;
miesiac=abs(miesiac);
miesiac=round(miesiac);
printf("%.0f",miesiac);
}
return 0;
}
1) jeżeli dane są całkowite i wynik ma by całkowity, to obliczenia wykonujemy na liczbach całkowitych (powtarzam to już po raz tysięczny chyba )
2) wzór jest zły (niedużo[color=#FF4000], trzeba trochę poprawic, gdy liczymy na liczbach całkowitych[/color])
3) czy oglądałeś wyście programu dla danych testowych - z przekierowanym wejściem z pliku, lub np. na http://ideone.com8 ?
Please stop using archaic code for the wrong reasons.
// never use endl instead of '\n' since endl forces a useless but expensive flush
#define endl '\n'
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); // NULL if you're old fashioned.
}
Voila! cin/cout is faster than scanf/printf. But if you really need speed I/O, cin.read() and parse it yourself.
Hi; I am trying to use std::cin.readsome()
for very fast input. Unfortunately the solution is working on my terminal but not on some judging servers. The same solutions transated to fread()
from cstdio
worked perfectly…
namespace FI {
const int L = 1 << 15 | 1;
char buf[L], *front, *back;
void nextChar(char &);
template <typename T>void nextNumber(T &);
}
void FI::nextChar(char &c) {
if(front == back) std::cin.readsome(buf, L), back = (front=buf) + std::cin.gcount();
c = !std::cin.gcount() ? (char)EOF : *front++;
}
template<typename T>void FI::nextNumber(T &x) {
char c; int f = 1;
for(nextChar(c); c>'9'||c<'0'; nextChar(c)) if(c == '-') f = -1;
for(x=0; c>='0'&& c<='9'; nextChar(c)) x = x*10+c-'0';
x *= f;
}