tle c++_避开TLE的C ++技巧

tle c++

Time Lime Exceeded or TLE can be a significant cause of frustration for competitive coders. If you are getting one, you probably need to change your approach, but if you are stuck in a coding challenge and can’t think of any other method to the problem, you can try using these techniques to get around those sneaky test cases.

超出时间限制或TLE可能是导致竞争性编码人员感到沮丧的重要原因。 如果您正在尝试一种方法,则可能需要更改方法,但是如果您遇到了编码难题,并且无法想到解决问题的任何其他方法,则可以尝试使用这些技术来解决那些狡猾的测试用例。

使您的I / O最快 (Make your I/O fast(est))

C++ developers usually get accustomed to using std::cin and std::cout for standard I/O operations, but they aren’t the fastest ones out there. If you are feeling lazy to change your code, you can just add the statements given below and enjoy the speed boost.

C ++开发人员通常习惯于将std :: cinstd :: cout用于标准的I / O操作,但并不是最快的。 如果您懒于更改代码,则只需添加下面给出的语句即可享受速度的提升。

The first statement disables the sync between C and C++ style I/O, so this may cause unknown errors if you combine them. For competitive coding, it’s usually not required to use both so you can use it without any worries.

第一条语句禁用C和C ++样式I / O之间的同步,因此,如果将它们组合在一起,可能会导致未知错误。 对于竞争性编码,通常不需要同时使用两者,因此您可以毫无后顾之忧地使用它。

The second statement unties std::cin with std::cout. Whenever one of the streams is used, the other is flushed, but when you untie them, they no longer flush each other, which results in better performance. Of course, you can always use your old C friends, scanf, and printf. They are faster than std::cin and std::cout by about three times, but when compared to unsynchronized std::cin and std::cout they don’t give much of a performance boost.

第二条语句将std :: cinstd :: cout结合在一起。 每当使用其中一个流时,都会刷新另一个流,但是当您解开它们时,它们将不再彼此刷新,这将导致更好的性能。 当然,您始终可以使用旧的C朋友, scanfprintf 。 它们比std :: cinstd :: cout快大约三倍,但是与不同步的std :: cinstd :: cout相比,它们的性能提升不多。

Want it to be even more fast? Are you taking integer input? Oh, then you are in luck! Just copy this function, and you can take integer inputs in the fastest, thread-safe way.

希望它更快? 您要输入整数吗? 哦,那你真幸运! 只需复制此函数,即可以最快的线程安全方式获取整数输入。

The function given above reads each character and converts them into an integer. This method is free from a lot of system calls that scanf has to make, like deducing the type of input.

上面给出的函数读取每个字符并将其转换为整数。 此方法免除了scanf必须进行的许多系统调用,例如推断输入类型。

You must be wondering why I wrote “the fastest, thread-safe way.” Well, because there is a faster way, but that makes it thread-unsafe. Want to use it? Replace getchar() with getchar_unlocked() and experience the beast. In competitive programming environments, you don’t need to worry about being thread-safe unless you are doing concurrency related tasks, so go ahead with it!

您一定想知道为什么我写了“最快,线程安全的方法”。 好吧,因为有一种更快的方法,但是这使它成为线程不安全的。 要使用吗? 用getchar_unlocked()替换getchar()并体验野兽。 在竞争性编程环境中,除非您正在执行与并发相关的任务,否则不必担心线程安全,因此请继续!

How do you believe what I say without any proof? Well, here it is. The different methods were benchmarked, and the results are presented below. If you want to look at the code used in the benchmark, I have uploaded it to a GitHub repository, which you can find here.

没有任何证据,您如何相信我所说的话? 好吧,这是。 对不同的方法进行了基准测试,结果在下面给出。 如果您想查看基准测试中使用的代码,我已将其上载到GitHub存储库,可以在此处找到。

cin vs. cin_wo_sync vs. scanf vs. fastscan vs. fastscan_unlocked
Comparison of std::cin vs. other input methods
std :: cin与其他输入法的比较

更快的向量 (Faster Vectors)

While vectors are fantastic because of their expandability, but the same attribute makes them slow too. std::vector is implemented as a dynamic array, which means that each time it needs to increase the size of the array, it creates another array double the size and then copy t

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值