CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Compete » January 2010 (Contest XII) » Circle of towers

Circle of towers

Problem code: L4

  • All Submissions

All submissions for this problem are available.

Two friends live in a small city. The city is built in the form of a circular road and skyscrapers surrounding the road. But one day the two friends quarreled with each other, and although they remained in the city, they decided to live in apartments located as far as possible from each other, or more precisely, so as to maximize the travel time between them. All the skyscrapers are arranged in a circle around the road (so you can travel in a cycle as follows: 1 <-> 2 <-> 3 <-> ... <-> n <-> 1), and the travel time between two neighbouring buildings is 1. The travel time between two adjacent floors of a skyscraper is also 1, and the friends can choose to live in any floor of the building, from the 0-th to the k-th, assuming that the skyscraper in question has k floors. Thus, the travel time from the j-th floor to ground level is j time units.

Input

First, 1 <= n <= 106, the number of skyscrapers. The following numbers, 1 <=  h1 <=109,...1 <= hn <= 109, are the heights of the buildings, given in clockwise order.

Output

Output exactly one number, the maximal possible travel time.

Example

Input:
4 1 2 3 4

Output:
8
Input:
10 1 7 4 3 2 9 2 3 4 5

Output:
20

Please note: the time limit for this problem is extremely strict!


Author: admin
Date Added: 15-12-2009
Time Limit: 1 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, ERL, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

Symbols broken in the input

triplem @ 1 Jan 2010 03:29 PM

Symbols broken in the input section here too.

does the friend move

thechamp @ 1 Jan 2010 04:54 PM

does the friend move clockwise or anticlockwise movement is also allowed?

because in second test case, if friend moves from '7 to 9' in clockwise direction then time is 20

while if he moves from '7 to 9' in anticlockwise then time taken is 22.

Broken symbols fixed. @Abhay

admin @ 1 Jan 2010 06:18 PM

Broken symbols fixed.

@Abhay Please read the problem statement.

"0-th to the k-th, assuming

pieguy @ 1 Jan 2010 06:18 PM

"0-th to the k-th, assuming that the skyscraper in question has k floors"

 

There's a bit of a cultural boundary when dealing with floor numberings.  Some countries label the ground level the 0th floor, some label it the 1st floor.  And in my country, at least, the ground floor is included in the floor count, so a 4 floor building has a 1st, 2nd, 3rd, and 4th floor (but no 0th).

Well what about the 13th

hexstr @ 1 Jan 2010 08:49 PM

Well what about the 13th floor?  In America we don't typically have a 13th floor.  ;)

I'm a bit confused by the

hallinan @ 2 Jan 2010 02:55 AM

I'm a bit confused by the first example. If they both lived on the 4th floor in the two '4' buildings, wouldn't the travel time be 9? It would take 4 units of time to get from the 4th floor to the 0th, then 1 more to get to the neighboring building, then 4 more to get from the 0th to the 4th.

What am I missing?

Ah wait, I just realized that

hallinan @ 2 Jan 2010 03:43 AM

Ah wait, I just realized that the first number is the number of buildings.

Hi,   I am getting a time

anirudhsaraf @ 2 Jan 2010 09:08 AM

Hi,

 

I am getting a time limit exceeded in the below code - all it does is read the input. Is this the correct way to take input? I read the faq and can't figure out how else to take the input?

 

THanks for the help !

 

 

#include<iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<set>
#include <stdlib.h>
#include <cstdlib>


using namespace std;

struct Data
{
long height;
long index;

};

Data d[1000000];
int main()
{

long nTotal = 0;

scanf("%ld",&nTotal);

for(long i = 0; i < nTotal; i++)
{   
scanf("%ld", &d[i].height);
d[i].index = i;
}



cout<<10;
}

I too have a question similar

devvrr @ 2 Jan 2010 09:30 AM

I too have a question similar to Anirudh Saraf. Is there a solution to this problem that can be accepted by the judge, which just uses scanf for reading the integers?

Doesn't look like it. Having

triplem @ 2 Jan 2010 09:38 AM

Doesn't look like it. Having such a strict time limit is a bit silly, really. Takes all the emphasis off the algorithm and puts it on rather terrible hacking.

hi i am also facing the same

ramthegreatcv @ 2 Jan 2010 10:49 AM

hi i am also facing the same problem.Can anyone suggest us a way out.

Hi, i think the cause of your

Muhammed Hedayet @ 2 Jan 2010 12:23 PM

Hi, i think the cause of your input testing code to give tle is, your code does not read multiple test cases

Is it supposed to read

anirudhsaraf @ 2 Jan 2010 01:34 PM

Is it supposed to read multiple test cases? It doesn't say how multiple test cases are given?

even linear time algo is

krater @ 2 Jan 2010 05:10 PM

even linear time algo is giving tle,so may b the algo shud be in logn order,right??

There are one test case per

rustinpiece @ 2 Jan 2010 08:46 PM

There are one test case per one test file. But the numbers can be given in multiple lines. I got WA assuming that there would be one line input. I think it would be better if the problem description mentioned that.

me too getting TLE, in

thechamp @ 2 Jan 2010 10:50 PM

me too getting TLE, in problem statement multiple test cases are not mention..should that be tried?

i think numbers given in

krater @ 3 Jan 2010 12:35 AM

i think numbers given in multiple lines wont b a problem since each number <=10^9.

@Tasnim: thanks, I hadn't

jcomeau_ictx @ 3 Jan 2010 01:34 AM

@Tasnim: thanks, I hadn't thought of that possibility

@Iaxman: not a problem with C/scanf, but definitely a problem if expecting all input on one line!

to all complaining about

izulin @ 3 Jan 2010 03:14 AM

to all complaining about strict timelimits - I have very stron reason to give them so tight, but I will explain it only *after* contest.

Until then - just live with it.

There is never a good reason

triplem @ 3 Jan 2010 03:19 AM

There is never a good reason for a time limit like this ;) But I guess we'll have to discuss that after the contest.

but however strict the time

anirudhsaraf @ 3 Jan 2010 04:08 AM

but however strict the time limit - we atleast need to read in all the numbers? I am getting a TLE if i just use scanf to read in the numbers and not even bother storing them separately or anything !On my machine I can read in 10^6 numbers in less than a second - but i still get a TLE when i submit it !

I could not understand the

sppraveen @ 3 Jan 2010 05:32 AM

I could not understand the problem. Can someone explain the output for sample cases.The way I understand is , two persons should live at two places such that the distance is max(either from clockwise or anticlockwise). Am I right. I still cant understand the problem.

Got the problem!!!

sppraveen @ 3 Jan 2010 05:34 AM

Got the problem!!!

I'm getting a TLE reading the

danielcs @ 3 Jan 2010 06:54 AM
I'm getting a TLE reading the numbers too. How are we supposed to do this at all?

@Writer: on problems where

pieguy @ 3 Jan 2010 08:58 AM

@Writer: on problems where the desired solution is fast enough that reading input becomes a bottleneck, a good alternative to having the user read all the data from stdin is to supply a function that generates the input.  See http://www.topcoder.com/stat?c=problem_statement&pm=10717 for a recent topcoder example, where just 4 integers define a grid of size up to 350*350.

The time limit says 1s, but

procrastinator @ 3 Jan 2010 10:14 AM

The time limit says 1s, but the successful submissions all have much longer in the time column. If the time column doesn't tell us the truth, how can we judge how close we are to being fast enough?

You haven't read the FAQ, I

triplem @ 3 Jan 2010 10:24 AM

You haven't read the FAQ, I presume.

Read it and forgot it

procrastinator @ 3 Jan 2010 03:07 PM

Read it and forgot it already! Sorry.

 

So there's really no way of telling how much you need to optimise? That really ruins the puzzle for me. :( Apart from making it a very artificial excercise, it means you can't even know that there _is_ a sufficiently fast solution in the language you're using. Suspicious that all of the successful submissions so far are in C/C++ - but since we don't know what we're aiming at, how could we know if we ought switch languages?

I have time limit exceeded

newsifer @ 3 Jan 2010 05:56 PM

I have time limit exceeded even in scanning the input

how can i optimse the code than?

can anyone get an idea.

If we travel only

crazysaikat @ 3 Jan 2010 07:06 PM

If we travel only clockwise, then in 1st test case, output should be 10 as if 1st friend stays in building 4, and other in 3, then 4 one has to travel from 4 to 3 in clockwise direction, making it 4+1+1+1+3 = 10. Please someone help.

TLE TLE TLE

thechamp @ 3 Jan 2010 07:43 PM

TLE TLE TLE

@Saikat Debnath Worst

nokia @ 3 Jan 2010 08:51 PM

@Saikat Debnath

Worst possible description of the problem i think. We can understand whatever we want. May be between towers you have to consider smallest distance. 3+1+4=8. The circle is biderectional i think.Anyways this is just a guess.

I think this is the worst

balakrishnan_v @ 4 Jan 2010 12:16 AM
I think this is the worst problem from all the contest so far. I spend half a day just to fix the IO. This is not fun at all

@ Balakrishnan: Did you

anirudhsaraf @ 4 Jan 2010 03:09 AM

@ Balakrishnan: Did you manage to fix the IO though ?

@David Stolp great idea, I'm

izulin @ 4 Jan 2010 06:39 AM

@David Stolp

great idea, I'm familiar with TopCoder. But with random input it's hard to generate some 'special' testcases.

But I'll try to follow the idea. Anyway, "time limit is extremely strict" should give point..

two solutions to 1 test case

izulin @ 4 Jan 2010 06:43 AM

two solutions to 1 test case are:

one friend on top of 4, one on top of 3 (with distance 3+1+4),

or one on top of 4, one on top of 2 (with distance 2+1+4).

 

yes, circle is bidirectional

Hi Przemysław, Does it make

seda @ 4 Jan 2010 09:18 PM

Hi Przemysław,

Does it make any sense to make my solution any faster?

At the moment it works fine, but perhaps it could be made a little faster...

 

Is there any faster way of

Flopsy @ 6 Jan 2010 12:25 PM

Is there any faster way of getting the input than scanf()?

Is there any faster way of

Flopsy @ 6 Jan 2010 12:26 PM

Is there any faster way of getting the input than scanf()?

scanf gets TLE. Some people

triplem @ 6 Jan 2010 12:45 PM

scanf gets TLE. Some people have got accepted. You draw the logical conclusion.

@Johann Ly : First try the

crazysaikat @ 6 Jan 2010 07:55 PM

@Johann Ly :

First try the problem "Enormous Input Output Test on Codechef". This will help you a lot.

I am getting Runtime Error :

crazysaikat @ 6 Jan 2010 08:29 PM

I am getting Runtime Error : Other. Plz help me to correct it.

need to have return 0; at the

devvrr @ 7 Jan 2010 02:40 PM

need to have return 0; at the end of main()

@Stephen Merriman  Some got

gautamverma @ 8 Jan 2010 12:20 AM

@Stephen Merriman

 Some got AC on Scanf and some TLE.

  How could both??

   Is there any other way to speed up IO?

@ Codechef Admin I have

msjaiswal @ 8 Jan 2010 02:01 AM

@ Codechef Admin

I have solved Circle of towers with 2nd fastest speed. But still cannot see my ranking. Can u please check if some problem is there or tell me if I am mistaken abt something????

 

 

Thanks in advance!

@ Admin ... problem solved by

msjaiswal @ 8 Jan 2010 02:10 AM

@ Admin ... problem solved by resubmitting the solution.

Thanks anyways.

hai... Im getting runtime

vivekmp @ 8 Jan 2010 10:32 PM

hai...

Im getting runtime error. But I think I took all precautions.(even checked the bounds on arrays).

Im doing it in JAVA and have tried for many inputs. I tried for large values also and not getting any errors on my pc. I named the file Main.java.

Can some 1 tell me what may be the reason??

"Please note: the time limit

vexorian @ 9 Jan 2010 09:18 PM

"Please note: the time limit for this problem is extremely strict!"

 

My first impression from the problem is that it is gonna be like those extremely lame OJ questions in which you also have to do lower-level optimization for no reason ... Strike one...

TLE: i have an O(n) algo, is

piyushcsiitd @ 9 Jan 2010 10:21 PM

TLE: i have an O(n) algo, is a better algo expected?

I strongly believe O(n) would

sppraveen @ 9 Jan 2010 10:36 PM

I strongly believe O(n) would definitely run within time. But I still dint find one O(n) solution

@piyush: If you can imagine

izulin @ 9 Jan 2010 11:31 PM

@piyush: If you can imagine sub-linear algorithm - cool.

why am i getting this

tejaswi5646 @ 10 Jan 2010 01:02 AM

why am i getting this error

/sources/tested.c:2:18: error: conio.h: No such file or directory

 

what is the correct header

FAQ

triplem @ 10 Jan 2010 02:43 AM

FAQ

i have O(n) algo and i am

piyushcsiitd @ 10 Jan 2010 06:09 AM

i have O(n) algo and i am using scanf, then what's problem ?

Are there multiple input cases ?

I hope if my algo is by chance wrong somewhere, it would give me err, and not TLE !!

 

Even I wrote an algo in O(n)

vivekmp @ 10 Jan 2010 09:26 AM

Even I wrote an algo in O(n) time in JAVA. Im getting Runtime error . Is there any way to find out the exact error?? In my PC, i generated long random inputs, and the program worked very fast with out any errors!

 

No, you will not be told the

triplem @ 10 Jan 2010 10:02 AM

No, you will not be told the exact error. You have to figure that out yourself.

From the problem , I

vivekmp @ 10 Jan 2010 10:41 AM

From the problem , I understand that the whole input has to be read from a single line. Is this true?

is there a function in c++ to

magiix @ 10 Jan 2010 02:28 PM

is there a function in c++ to calculate my running time for a certain testcase?

@Admin: is my algo giving a

goti @ 10 Jan 2010 07:07 PM

@Admin:

is my algo giving a better solution !!!

or

i have over-looked some special test case ?

:P

@admin   (..continuation of

goti @ 10 Jan 2010 07:34 PM

@admin

 

(..continuation of previous post)

or

i have missed out something big

 

your comments would be of help

thanks

@admin no sweat, missed out

goti @ 10 Jan 2010 10:43 PM

@admin

no sweat,

missed out on a "small" technicallity :D

Hi guys,how do we contact

conan13 @ 15 Jan 2010 11:15 AM

Hi guys,how do we contact users here?

You can't.

triplem @ 15 Jan 2010 02:32 PM

You can't.

Is this a variant of some

sppraveen @ 15 Jan 2010 08:04 PM

Is this a variant of some algorithm or is there any related problems like this one? I dint get a clue on the approach.

Or I just missed the easy one?

# include

gauthamchellu @ 15 Jan 2010 11:03 PM

  1. # include <stdio.h>
  2. int main()
  3. {
  4. long int n,i,j;
  5. long int *h,max=1;
  6. scanf("%ld",&n);
  7. if(n<1 || n>1000000)
  8. return 0;
  9. h = malloc(n);
  10. for(i=0;i<n;i++)
  11. {
  12. scanf("%ld",&h[i]);
  13. if(h[i]<1 || h[i]>1000000000)
  14. return 0;
  15. }
  16. for(i=0;i<n-1;i++)
  17. for(j=i+1;j<n;j++)
  18. if(((j-i)+h[i]+h[j])>max)
  19. max=((j-i)+h[i]+h[j]);
  20. printf("%ld",max);
  21. return 0;
  22. }
    codechef admin, can u explain by what means this code gives wrong answer?

You haven't included cases

triplem @ 16 Jan 2010 02:32 AM

You haven't included cases where you go from tower N to tower 1 in one minute.

hey.. can we get the test

CodepaCer @ 16 Jan 2010 04:26 PM

hey.. can we get the test cases now as the contest is over!!?

dsfdf

aj2011it @ 24 Jan 2010 01:45 PM

dsfdf

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold
CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com