#include<iostream>
#include<algorithm>
using namespace std;

int num[100005];

int main()
{
int n,flag=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>num[i];
sort(num+1, num+n+1);
for(int i=n-2;i>=1;i--)
{
if(num[i]+num[i+1]>num[i+2])
{
flag=1;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}

TLE！！！

#include<stdio.h>
#include<algorithm>

int num[100005];

int main()
{
int n,flag=0;
scanf("%d", &n);
for(int i=1;i<=n;i++)
scanf("%d", &num[i]);
sort(num+1, num+n+1);
for(int i=n-2;i>=1;i--)
{
if(num[i]+num[i+1]>num[i+2])
{
flag=1;
break;
}
}
if(flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}

#include<iostream>
#include<algorithm>
using namespace std;

int num[100005];

int main()
{
ios::sync_with_stdio(false);
int n,flag=0;
cin>>n;
for(int i=1;i<=n;i++)
cin>>num[i];
sort(num+1, num+n+1);
for(int i=n-2;i>=1;i--)
{
if(num[i]+num[i+1]>num[i+2])
{
flag=1;
break;
}
}
if(flag)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
return 0;
}

## Stream

stream，也就是大家熟知的流，什么是流呢？我在stdio.h文档找到了以下的一段介绍

This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system. Streams are an abstraction to interact with these in an uniform way; All streams have similar properties independently of the individual characteristics of the physical media they are associated with.

Input and output, whether to or from physical devices such as terminals and tape drives, or whether to or from files supported on structured storage devices, are mapped into logical data streams, whose properties are more uniform than their various inputs and outputs. Two forms of mapping are supported: text streams and binary streams.
A text stream consists of one or more lines. A line in a text stream consists of zero or more characters plus a terminating new-line character. (The only exception is that in some implementations the last line of a file does not require a terminating new-line character.) Unix adopted a standard internal format for all text streams. Each line of text is terminated by a new-line character. That’s what any program expects when it reads text, and that’s what any program produces when it writes text. (This is the most basic convention, and if it doesn’t meet the needs of a text-oriented peripheral attached to a Unix machine, then the fix-up occurs out at the edges of the system. Nothing in between needs to change.) The string of characters that go into, or come out of a text stream may have to be modified to conform to specific conventions. This results in a possible difference between the data that go into a text stream and the data that come out. For instance, in some implementations when a space-character precedes a new-line character in the input, the space character gets removed out of the output. In general, when the data only consists of printable characters and control characters like horizontal tab and new-line, the input and output of a text stream are equal.
Compared to a text stream, a binary stream is pretty straight forward. A binary stream is an ordered sequence of characters that can transparently record internal data. Data written to a binary stream shall always equal the data that gets read out under the same implementation. Binary streams, however, may have an implementation-defined number of null characters appended to the end of the stream. There are no further conventions which need to be considered.
Nothing in Unix prevents the program from writing arbitrary 8-bit binary codes to any open file, or reading them back unchanged from an adequate repository. Thus, Unix obliterated the long-standing distinction between text streams and binary streams.

When a C program starts its execution the program automatically opens three standard streams named stdin, stdout, and stderr. These are attached for every C program.
The first standard stream is used for input buffering and the other two are used for output. These streams are sequences of bytes.

## Buffer

A buffer is a block of memory where data is accumulated before being physically read or written to the associated file or device. Streams can be either fully buffered, line buffered or unbuffered. On fully buffered streams, data is read/written when the buffer is filled, on line buffered streams this happens when a new-line character is encountered, and on unbuffered streams characters are intended to be read/written as soon as possible.

Buffer翻译过来的话就是缓冲区，根据输入和输出，缓冲区可以分为输入缓冲和输出缓冲，它其实就是一块内存而已，输入缓冲区用来暂存从外设输入进来的内容，输出缓冲区就是用来暂存要输出在外设的内容。

## 测试C/C++标准输入输出的速度

scanf/cin和printf/cout都是带缓冲的，当调用scanf/cin时，它们会先去看看缓冲区有没有数据，若有的话会先从缓冲区读入，若缓冲区空的话，就会等待输入，printf/cout同理

$yes 1111111111111111 | head -1000000 > data.txt ### scanf vs cin 测试文件分别如下 //testcin.cpp #include<iostream> using namespace std; int main() { char buffer[16]; while(cin>>buffer) ; return 0; } //testscanf.cpp #include<stdio.h> int main() { char buffer[16]; while(scanf("%s", buffer)!=EOF) ; return 0; } 编译 $ g++ testcin.cpp -o testcin.out
$g++ testscanf.cpp -o testscanf.out 运行 $ time ./testcin.out < data.txt
$time ./testscanf.out < data.txt 结果如下 很明显scanf比cin快很多 我又测试了一下关闭同步的cin的速度，测试代码如下 //testcin.cpp #include<iostream> using namespace std; int main() { ios::sync_with_stdio(false); char buffer[16]; while(cin>>buffer) ; return 0; } 测试结果如下 可以看到关闭同步的cin和scanf速度差不多，这是因为C++为了兼容C，使得scanf和cin可以同时使用，所以让cin和scanf保持同步，这就使得cin花了一些时间在处理同步上。当使用cin而不使用scanf时，可以关闭同步来提高cin的速度，若在混用scanf和cin时关闭了同步，就可能会导致一些错乱，不建议这么做。 我在网上找到了别人在不同的系统和编译器环境下测试的结果，测试数据是55MB的文件。 ### printf vs cout 其实输出和输入差不多，不过也做个简单的测试吧 //testprintf.cpp #include<stdio.h> int main() { int n = 1000000; while(n--) printf("1"); return 0; } //testcout.cpp #include<iostream> using namespace std; int main() { int n = 1000000; while(n--) cout<<"1"; return 0; } 编译 $ g++ testprintf.cpp -o testprintf.out
$g++ testcout.cpp -o testcout.out 运行 $ time ./testprintf.out > print.txt
\$ time ./testcout.out > cout.txt

//testcout.cpp
#include<iostream>
using namespace std;

int main()
{
ios::sync_with_stdio(flase);
int n = 1000000;
while(n--)
cout<<"1";
return 0;
}

## 算法竞赛的奇技淫巧——快读快写

//针对正负整数的快读快写
int s = 0,w = 1;
char ch = getchar();
while (ch <= '0' || ch > '9'){
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') s = s * 10 + ch - '0' , ch = getchar();
return s * w;
}

inline void Quick_Write(int x){
if (x < 0){
putchar('-');
re = -re;
}
if (x > 9) Quick_Write(x/10);
putchar(x%10 + '0');
}