SlideShare a Scribd company logo
Problem C: Vito's
                                      family


Background


The world-known gangster Vito Deadstone is moving to New York. He has a very big family
there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is
trying to find a house close to them.


Problem


Vito wants to minimize the total distance to all of them and has blackmailed you to write a
program that solves his problem.


Input


The input consists of several test cases. The first line contains the number of test cases.


For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the

street numbers (also integers)                        where they live ( 0 < si < 30000 ). Note that

several relatives could live in the same street number.


Output


For each test case your program must write the minimal sum of distances from the optimal
Vito's house to each one of his relatives. The distance between two street numbers si and sj is
dij= |si-sj|.

Sample Input


2
224
3246


Sample Output


2
4



                                            DNA
                                            Sorting
One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of
order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure
is 5, since D is greater than four letters to its right and E is greater than one letter to its right.
This measure is called the number of inversions in the sequence. The sequence ``AACEDGG''
has only one inversion (E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6
inversions (it is as unsorted as can be--exactly the reverse of sorted).



You are responsible for cataloguing a sequence of DNA strings (sequences containing only the
four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but
rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the
same length.


Input


The first line of the input is an integer M, then a blank line followed by M datasets. There is a
blank line between datasets.


The first line of each dataset contains two integers: a positive integer n (               ) giving


the length of the strings; and a positive integer m (                 ) giving the number of strings.

These are followed by m lines, each containing a string of length n.


Output


For each dataset, output the list of input strings, arranged from ``most sorted'' to ``least
sorted''. If two or more strings are equally sorted, list them in the same order they are in the
input file.


Sample Input


1


10 6
AACATGAAGG
TTTTGGCCAA
TTTGGCCAAA
GATCAGATTT
CCCGGGGGGA
ATCGATGCAT


Sample Output
CCCGGGGGGA
AACATGAAGG
GATCAGATTT
ATCGATGCAT
TTTTGGCCAA
TTTGGCCAAA

                                         Searching
                                         Quickly


Background


Searching and sorting are part of the theory and practice of computer science. For example,
binary search provides a good example of an easy-to-understand algorithm with sub-linear

complexity. Quicksort is an efficient             [average case] comparison based sort.


KWIC-indexing is an indexing method that permits efficient ``human search'' of, for example, a
list of titles.


The Problem


Given a list of titles and a list of ``words to ignore'', you are to write a program that generates a
KWIC (Key Word In Context) index of the titles. In a KWIC-index, a title is listed once for each
keyword that occurs in the title. The KWIC-index is alphabetized by keyword.


Any word that is not one of the ``words to ignore'' is a potential keyword.


For example, if words to ignore are ``the, of, and, as, a'' and the list of titles is:


Descent of Man
The Ascent of Man
The Old Man and The Sea
A Portrait of The Artist As a Young Man


A KWIC-index of these titles might be given by:


                 a portrait of the ARTIST as a young man
                            the ASCENT of man
                               DESCENT of man
                      descent of MAN
                    the ascent of MAN
                        the old MAN and the sea
a portrait of the artist as a young MAN
                           the OLD man and the sea
                             a PORTRAIT of the artist as a young man
              the old man and the SEA
      a portrait of the artist as a YOUNG man


The Input


The input is a sequence of lines, the string :: is used to separate the list of words to ignore
from the list of titles. Each of the words to ignore appears in lower-case letters on a line by
itself and is no more than 10 characters in length. Each title appears on a line by itself and may
consist of mixed-case (upper and lower) letters. Words in a title are separated by whitespace.
No title contains more than 15 words.


There will be no more than 50 words to ignore, no more than than 200 titles, and no more than
10,000 characters in the titles and words to ignore combined. No characters other than 'a'-'z',
'A'-'Z', and white space will appear in the input.


The Output


The output should be a KWIC-index of the titles, with each title appearing once for each
keyword in the title, and with the KWIC-index alphabetized by keyword. If a word appears more
than once in a title, each instance is a potential keyword.


The keyword should appear in all upper-case letters. All other words in a title should be in
lower-case letters. Titles in the KWIC-index with the same keyword should appear in the same
order as they appeared in the input file. In the case where multiple instances of a word are
keywords in the same title, the keywords should be capitalized in left-to-right order.


Case (upper or lower) is irrelevant when determining if a word is to be ignored.


The titles in the KWIC-index need NOT be justified or aligned by keyword, all titles may be
listed left-justified.


Sample Input


is
the
of
and
as
a
but
::
Descent of Man
The Ascent of Man
The Old Man and The Sea
A Portrait of The Artist As a Young Man
A Man is a Man but Bubblesort IS A DOG


Sample Output


a portrait of the ARTIST as a young man
the ASCENT of man
a man is a man but BUBBLESORT is a dog
DESCENT of man
a man is a man but bubblesort is a DOG
descent of MAN
the ascent of MAN
the old MAN and the sea
a portrait of the artist as a young MAN
a MAN is a man but bubblesort is a dog
a man is a MAN but bubblesort is a dog
the OLD man and the sea
a PORTRAIT of the artist as a young man
the old man and the SEA
a portrait of the artist as a YOUNG man

                                           Satellites


The Problem


The radius of earth is 6440 Kilometer. There are many Satellites and Asteroids moving around
the earth. If two Satellites create an angle with the center of earth, can you find out the
distance between them? By distance we mean both the arc and chord distances. Both
satellites are on the same orbit. (However, please consider that they are revolving on a circular
path rather than an elliptical path.)
The Input


The input file will contain one or more test cases.


Each test case consists of one line containing two-integer s and a and a string "min" or
"deg". Here s is the distance of the satellite from the surface of the earth and a is the angle
that the satellites make with the center of earth. It may be in minutes ( ‘ ) or in degrees ( 0 ).
Remember that the same line will never contain minute and degree at a time.


The Output


For each test case, print one line containing the required distances i.e. both arc distance
and chord distance respectively between two satellites in Kilometer. The distance will be a
floating-point value with six digits after decimal point.


Sample Input


500 30 deg
700 60 min
200 45 deg


Sample Output


3633.775503 3592.408346
124.616509 124.614927
5215.043805 5082.035982
Polygon Inside A
                                           Circle


The Problem


Consider a polygon of equal sides inside a circle as shown in the figure below.




                       Figure: The regular polygon inside a circle


Given the radius of the circle and number of sides. You have to find the area of the polygon.


The Input


In each line there will be two numbers indicating the radius `r' (0<r<20000) and the number of
sides of the polygon `n' (2<n<20000) respectively. Input is terminated by `EOF'.


The Output


Output the area in each line. The number must be rounded to the third digit after the decimal
point.


Sample Input


2 2000
10 3000


Sample Output
12.566
314.159

More Related Content

PPTX
Regular Expressions 101 Introduction to Regular Expressions
PPTX
Introduction to Regular Expressions
PPTX
Regular expressions
PPTX
String functions
PPTX
Regular expressions
PPTX
PPT
Regular expressions
PPTX
Chapter3pptx__2021_12_23_22_52_54.pptx
Regular Expressions 101 Introduction to Regular Expressions
Introduction to Regular Expressions
Regular expressions
String functions
Regular expressions
Regular expressions
Chapter3pptx__2021_12_23_22_52_54.pptx

Viewers also liked (8)

DOC
Sources
PPT
Acmicpcseminar4
PPT
Data Structure 1
PPT
Data Structure 3
PPTX
Back to-school movies for parents
PPT
2007 Icpc3
PPT
2007 Icpc2
PPT
Acmicpcseminar3
Sources
Acmicpcseminar4
Data Structure 1
Data Structure 3
Back to-school movies for parents
2007 Icpc3
2007 Icpc2
Acmicpcseminar3
Ad

Similar to Problemset (20)

PDF
14-Strings-In-Python strings with oops .pdf
PDF
Python data handling
PPTX
String in programming language in c or c++
PDF
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
PPTX
"Strings in Python - Presentation Slide"
PPTX
PDF
07-Strings( string Functions and parameters C).pdf
PDF
Cours_Fortran_Chapitre3_Tableaux_ehtp.pdf
DOCX
For this assignment, download the A6 code pack. This zip fil.docx
PPTX
Array and string
PPTX
string manipulation in python ppt for grade 11 cbse
PDF
Strings in c mrs.sowmya jyothi
PDF
stringsinpython-181122100212.pdf
PPTX
Python Strings.pptx
PDF
Json demo
PPT
Crypto
PPT
JAVA PROGRAMMING : Data types
PDF
accenture Advanced coding questiosn for online assessment preparation
DOCX
Classical crypto techniques
14-Strings-In-Python strings with oops .pdf
Python data handling
String in programming language in c or c++
python1uhaibueuhERADGAIUSAERUGHw9uSS.pdf
"Strings in Python - Presentation Slide"
07-Strings( string Functions and parameters C).pdf
Cours_Fortran_Chapitre3_Tableaux_ehtp.pdf
For this assignment, download the A6 code pack. This zip fil.docx
Array and string
string manipulation in python ppt for grade 11 cbse
Strings in c mrs.sowmya jyothi
stringsinpython-181122100212.pdf
Python Strings.pptx
Json demo
Crypto
JAVA PROGRAMMING : Data types
accenture Advanced coding questiosn for online assessment preparation
Classical crypto techniques
Ad

Problemset

  • 1. Problem C: Vito's family Background The world-known gangster Vito Deadstone is moving to New York. He has a very big family there, all of them living in Lamafia Avenue. Since he will visit all his relatives very often, he is trying to find a house close to them. Problem Vito wants to minimize the total distance to all of them and has blackmailed you to write a program that solves his problem. Input The input consists of several test cases. The first line contains the number of test cases. For each test case you will be given the integer number of relatives r ( 0 < r < 500) and the street numbers (also integers) where they live ( 0 < si < 30000 ). Note that several relatives could live in the same street number. Output For each test case your program must write the minimal sum of distances from the optimal Vito's house to each one of his relatives. The distance between two street numbers si and sj is dij= |si-sj|. Sample Input 2 224 3246 Sample Output 2 4 DNA Sorting
  • 2. One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instance, in the letter sequence ``DAABEC'', this measure is 5, since D is greater than four letters to its right and E is greater than one letter to its right. This measure is called the number of inversions in the sequence. The sequence ``AACEDGG'' has only one inversion (E and D)--it is nearly sorted--while the sequence ``ZWQM'' has 6 inversions (it is as unsorted as can be--exactly the reverse of sorted). You are responsible for cataloguing a sequence of DNA strings (sequences containing only the four letters A, C, G, and T). However, you want to catalog them, not in alphabetical order, but rather in order of ``sortedness'', from ``most sorted'' to ``least sorted''. All the strings are of the same length. Input The first line of the input is an integer M, then a blank line followed by M datasets. There is a blank line between datasets. The first line of each dataset contains two integers: a positive integer n ( ) giving the length of the strings; and a positive integer m ( ) giving the number of strings. These are followed by m lines, each containing a string of length n. Output For each dataset, output the list of input strings, arranged from ``most sorted'' to ``least sorted''. If two or more strings are equally sorted, list them in the same order they are in the input file. Sample Input 1 10 6 AACATGAAGG TTTTGGCCAA TTTGGCCAAA GATCAGATTT CCCGGGGGGA ATCGATGCAT Sample Output
  • 3. CCCGGGGGGA AACATGAAGG GATCAGATTT ATCGATGCAT TTTTGGCCAA TTTGGCCAAA Searching Quickly Background Searching and sorting are part of the theory and practice of computer science. For example, binary search provides a good example of an easy-to-understand algorithm with sub-linear complexity. Quicksort is an efficient [average case] comparison based sort. KWIC-indexing is an indexing method that permits efficient ``human search'' of, for example, a list of titles. The Problem Given a list of titles and a list of ``words to ignore'', you are to write a program that generates a KWIC (Key Word In Context) index of the titles. In a KWIC-index, a title is listed once for each keyword that occurs in the title. The KWIC-index is alphabetized by keyword. Any word that is not one of the ``words to ignore'' is a potential keyword. For example, if words to ignore are ``the, of, and, as, a'' and the list of titles is: Descent of Man The Ascent of Man The Old Man and The Sea A Portrait of The Artist As a Young Man A KWIC-index of these titles might be given by: a portrait of the ARTIST as a young man the ASCENT of man DESCENT of man descent of MAN the ascent of MAN the old MAN and the sea
  • 4. a portrait of the artist as a young MAN the OLD man and the sea a PORTRAIT of the artist as a young man the old man and the SEA a portrait of the artist as a YOUNG man The Input The input is a sequence of lines, the string :: is used to separate the list of words to ignore from the list of titles. Each of the words to ignore appears in lower-case letters on a line by itself and is no more than 10 characters in length. Each title appears on a line by itself and may consist of mixed-case (upper and lower) letters. Words in a title are separated by whitespace. No title contains more than 15 words. There will be no more than 50 words to ignore, no more than than 200 titles, and no more than 10,000 characters in the titles and words to ignore combined. No characters other than 'a'-'z', 'A'-'Z', and white space will appear in the input. The Output The output should be a KWIC-index of the titles, with each title appearing once for each keyword in the title, and with the KWIC-index alphabetized by keyword. If a word appears more than once in a title, each instance is a potential keyword. The keyword should appear in all upper-case letters. All other words in a title should be in lower-case letters. Titles in the KWIC-index with the same keyword should appear in the same order as they appeared in the input file. In the case where multiple instances of a word are keywords in the same title, the keywords should be capitalized in left-to-right order. Case (upper or lower) is irrelevant when determining if a word is to be ignored. The titles in the KWIC-index need NOT be justified or aligned by keyword, all titles may be listed left-justified. Sample Input is the of and as a but
  • 5. :: Descent of Man The Ascent of Man The Old Man and The Sea A Portrait of The Artist As a Young Man A Man is a Man but Bubblesort IS A DOG Sample Output a portrait of the ARTIST as a young man the ASCENT of man a man is a man but BUBBLESORT is a dog DESCENT of man a man is a man but bubblesort is a DOG descent of MAN the ascent of MAN the old MAN and the sea a portrait of the artist as a young MAN a MAN is a man but bubblesort is a dog a man is a MAN but bubblesort is a dog the OLD man and the sea a PORTRAIT of the artist as a young man the old man and the SEA a portrait of the artist as a YOUNG man Satellites The Problem The radius of earth is 6440 Kilometer. There are many Satellites and Asteroids moving around the earth. If two Satellites create an angle with the center of earth, can you find out the distance between them? By distance we mean both the arc and chord distances. Both satellites are on the same orbit. (However, please consider that they are revolving on a circular path rather than an elliptical path.)
  • 6. The Input The input file will contain one or more test cases. Each test case consists of one line containing two-integer s and a and a string "min" or "deg". Here s is the distance of the satellite from the surface of the earth and a is the angle that the satellites make with the center of earth. It may be in minutes ( ‘ ) or in degrees ( 0 ). Remember that the same line will never contain minute and degree at a time. The Output For each test case, print one line containing the required distances i.e. both arc distance and chord distance respectively between two satellites in Kilometer. The distance will be a floating-point value with six digits after decimal point. Sample Input 500 30 deg 700 60 min 200 45 deg Sample Output 3633.775503 3592.408346 124.616509 124.614927 5215.043805 5082.035982
  • 7. Polygon Inside A Circle The Problem Consider a polygon of equal sides inside a circle as shown in the figure below. Figure: The regular polygon inside a circle Given the radius of the circle and number of sides. You have to find the area of the polygon. The Input In each line there will be two numbers indicating the radius `r' (0<r<20000) and the number of sides of the polygon `n' (2<n<20000) respectively. Input is terminated by `EOF'. The Output Output the area in each line. The number must be rounded to the third digit after the decimal point. Sample Input 2 2000 10 3000 Sample Output