Codechef Carvans question

Source: Internet
Author: User

In fact, this question is to calculate a number smaller than the previous one in the array, plus the first number.

It sounds easy.

However, big data is used here, and more than 4 MB of data is waiting for input.

If you are not familiar with input and output, it is time-out.


Original question: http://www.codechef.com/problems/CARVANS/


Two methods are used here, which is the best method I think at present:

1 use gechar

2 Use fread

The first method is better to use, but the second method is really a headache for me. Finally, we finally got it done.


It was stuck until now because the program did not properly process the mix up. Must be processed separately. One function is used to process input and output, and the other is used to process logic.

Mix up is a headache, because there are space characters, line breaks, input ends, three cases will be messy.

The lesson is that the program structure is not well divided. The more you get, the more headaches you have. Don't underestimate any "easy" questions.


In addition, the data here exceeded the value of 1 <30, so I randomly used 1 <30 to replace the integer maximum value. The answer is always wrong.

1. The program processed by getcha is as follows:

# Include
 
  
// Lesson: Do not use 1 <30 as the integer maximum value int Carvans () {auto scanInt = [] () {char c = getchar (); while (c <'0' | '9' <c) {c = getchar ();} int num = 0; while ('0' <= c & c <= '9') {num = (num <3) + (num <1) + c-'0 '; c = getchar () ;}return num ;}; int T = scanInt (); while (T --) {int N = scanInt (); int curMin = 2147483647, a = 0, ans = 0; // The data in this question is actually greater than 1 <30for (int I = 0; I <N; I ++) {a = scanInt (); if (a <= curMin) {curMin = a; ans ++ ;}} printf ("% d \ n", ans) ;}return 0 ;}
 


2 The following describes how to use fread to accelerate the process. However, if a few details are not properly handled, there will be bugs. If the logic is mixed, it will be easier to generate bugs, it is still quite difficult.

The following program should have no bugs.

# Include
 
  
Char BufferCarvans [4096]; int idCar = 0, Ccar = 0; int getFromBufferCar () {int num = 0; bool first = true; while (true) {if (idCar> = Ccar) {idCar = 0; Ccar = fread (BufferCarvans, 1, 4096, stdin);} if (Ccar = 0) return num; // read data-free, returns the last while (first & idCar <Ccar & (BufferCarvans [idCar] <'0' | BufferCarvans [idCar]> '9') idCar ++; while (idCar <Ccar & BufferCarvans [idCar]> = '0' & BufferCarvans [idCar] <= '9') {num = (num <3) + (num <1) + BufferCarvans [idCar]-'0'; idCar ++; first = false ;} if (idCar <Ccar & (BufferCarvans [idCar] <'0' | BufferCarvans [idCar]> '9') {return num ;}} return 0 ;} int Carvans_3 () {int T, n = 0, a = 0, curMin = 0; scanf ("% d \ n", & T); while (T --) {n = getFromBufferCar (); curMin = getFromBufferCar (); int ans = 1; for (int I = 1; I <n; I ++) {a = getFromBufferCar (); if (a <= curMin) {curMin = a; ans ++ ;}} printf ("% d \ n", ans) ;}return 0 ;}
 




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.