[关闭]
@lawk97 2016-12-09T10:54:05.000000Z 字数 18790 阅读 1372

集训队第一次作业

(2016.11.28-2016.12.04)

作业


题目列表

A - Design Tutorial: Learn from Math

[CodeForces - 472A]


One way to create a task is to learn from math. You can generate some
random math statement or modify some theorems to get something new and
build a new task from that.

For example, there is a statement called the "Goldbach's conjecture".
It says: "each even number no less than four can be expressed as the
sum of two primes". Let's modify it. How about a statement like that:
"each integer no less than 12 can be expressed as the sum of two
composite numbers." Not like the Goldbach's conjecture, I can prove
this theorem.

You are given an integer n no less than 12, express it as a sum of two
composite numbers.

Input

The only line contains an integer n (12 ≤ n ≤ 106).

Output

Output two composite integers x and y (1 < x, y < n) such that
x + y = n. If there are multiple solutions, you can output any of
them.

  1. #include <cstdio>
  2. #define N 10
  3. int main()
  4. {
  5. int n,a,x=0,y=0;
  6. while(~scanf("%d",&n))
  7. {
  8. a=n%10;
  9. switch (a) {
  10. case 0:
  11. x=4;
  12. break;
  13. case 1:
  14. x=6;
  15. break;
  16. case 2:
  17. x=4;
  18. break;
  19. case 3:
  20. if(n<20)
  21. x=4;
  22. else
  23. x=8;
  24. break;
  25. case 4:
  26. x=4;
  27. break;
  28. case 5:
  29. if(n<20)
  30. x=6;
  31. else
  32. x=10;
  33. break;
  34. case 6:
  35. x=4;
  36. break;
  37. case 7:
  38. if(n<20)
  39. x=8;
  40. else
  41. x=12;
  42. break;
  43. case 8:
  44. x=4;
  45. break;
  46. case 9:
  47. x=4;
  48. break;
  49. default:
  50. break;
  51. }
  52. y=n-x;
  53. if(x>y)
  54. printf("%d %d\n",y,x);
  55. else
  56. printf("%d %d\n",x,y);
  57. }
  58. return 0;
  59. }

B - Design Tutorial: Learn from Life

[CodeForces 472B]


One way to create a task is to learn from life. You can choose some
experience in real life, formalize it and then you will get a new
task.

Let's think about a scene in real life: there are lots of people
waiting in front of the elevator, each person wants to go to a certain
floor. We can formalize it in the following way. We have n people
standing on the first floor, the i-th person wants to go to the fi-th
floor. Unfortunately, there is only one elevator and its capacity
equal to k (that is at most k people can use it simultaneously).
Initially the elevator is located on the first floor. The elevator
needs |a - b| seconds to move from the a-th floor to the b-th floor
(we don't count the time the people need to get on and off the
elevator).

What is the minimal number of seconds that is needed to transport all
the people to the corresponding floors and then return the elevator to
the first floor?

Input

The first line contains two integers n and k (1 ≤ n, k ≤ 2000) — the
number of people and the maximal capacity of the elevator.

The next line contains n integers: f1, f2, ..., fn (2 ≤ fi ≤ 2000),
where fi denotes the target floor of the i-th person.

Output

Output a single integer — the minimal time needed to achieve the goal.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,k,sum=0;
  8. int a[2005];
  9. scanf("%d%d",&n,&k);
  10. for(int i=0;i<n;i++)
  11. scanf("%d",&a[i]);
  12. sort(a,a+n);
  13. while(1)
  14. {
  15. if(n<=0)
  16. break;
  17. sum+=2*(a[n-1]-1);
  18. n-=k;
  19. }
  20. printf("%d\n",sum);
  21. return 0;
  22. }

C - Design Tutorial: Make It Nondeterministic

[CodeForces 472C]


A way to make a new task is to make it nondeterministic or
probabilistic. For example, the hard task of Topcoder SRM 595,
Constellation, is the probabilistic version of a convex hull.

Let's try to make a new task. Firstly we will use the following task.
There are n people, sort them by their name. It is just an ordinary
sorting problem, but we can make it more interesting by adding
nondeterministic element. There are n people, each person will use
either his/her first name or last name as a handle. Can the
lexicographical order of the handles be exactly equal to the given
permutation p?

More formally, if we denote the handle of the i-th person as hi, then
the following condition must hold: 此处输入图片的描述.

Input

The first line contains an integer n (1 ≤ n ≤ 105) — the number of
people.

The next n lines each contains two strings. The i-th line contains
strings fi and si (1 ≤ |fi|, |si| ≤ 50) — the first name and last name
of the i-th person. Each string consists only of lowercase English
letters. All of the given 2n strings will be distinct.

The next line contains n distinct integers: p1, p2, ..., pn (1 ≤ pi ≤ n).

Output

If it is possible, output "YES", otherwise output "NO".

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <iostream>
  7. using namespace std;
  8. int n,flag=1;
  9. char f[100005][55],s[100005][55];
  10. string a,b,now;
  11. int main()
  12. {
  13. scanf("%d",&n);
  14. for(int i=0;i<n;i++)
  15. {
  16. scanf("%s %s",f[i],s[i]);
  17. }
  18. for(int i=0;i<n;i++)
  19. {
  20. int x;
  21. scanf("%d",&x);
  22. a=f[x-1];
  23. b=s[x-1];
  24. if(i==0)
  25. {
  26. if(a.compare(b)<0)
  27. now=a;
  28. else
  29. now=b;
  30. }
  31. else
  32. {
  33. if(now.compare(a)>=0&&now.compare(b)>=0)
  34. {
  35. flag=0;
  36. break;
  37. }
  38. else if(now.compare(a)>=0&&now.compare(b)<0)
  39. {
  40. now=b;
  41. }
  42. else if(now.compare(a)<0&&now.compare(b)>=0)
  43. {
  44. now=a;
  45. }
  46. else if(now.compare(a)<0&&now.compare(b)<0)
  47. {
  48. if(a.compare(b)<0)
  49. now=a;
  50. else
  51. now=b;
  52. }
  53. }
  54. }
  55. if(flag)
  56. printf("YES\n");
  57. else
  58. printf("NO\n");
  59. return 0;
  60. }

E - MUH and Sticks C

[CodeForces 471A]


Two polar bears Menshykov and Uslada from the St.Petersburg zoo and
elephant Horace from the Kiev zoo got six sticks to play with and
assess the animals' creativity. Menshykov, Uslada and Horace decided
to make either an elephant or a bear from those sticks. They can make
an animal from sticks in the following way:

Four sticks represent the animal's legs, these sticks should have the
same length. Two remaining sticks represent the animal's head and
body. The bear's head stick must be shorter than the body stick. The
elephant, however, has a long trunk, so his head stick must be as long
as the body stick. Note that there are no limits on the relations
between the leg sticks and the head and body sticks. Your task is to
find out which animal can be made from the given stick set. The zoo
keeper wants the sticks back after the game, so they must never be
broken, even bears understand it.

Input

The single line contains six space-separated integers li (1 ≤ li ≤ 9)
— the lengths of the six sticks. It is guaranteed that the input is
such that you cannot make both animals from the sticks.

Output

If you can make a bear from the given set, print string "Bear"
(without the quotes). If you can make an elephant, print string
"Elephant" (wıthout the quotes). If you can make neither a bear nor an
elephant, print string "Alien" (without the quotes).

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int x,i;
  8. int a[6];
  9. for(int i=0;i<6;i++)
  10. {
  11. scanf("%d",&a[i]);
  12. }
  13. sort(a,a+6);
  14. for(i=0;i<3;i++)
  15. {
  16. if(a[i]==a[i+1]&&a[i]==a[i+2]&&a[i]==a[i+3])
  17. {
  18. a[i]=a[i+1]=a[i+2]=a[i+3]=-1;
  19. break;
  20. }
  21. }
  22. if(i==3)
  23. printf("Alien\n");
  24. else
  25. {
  26. sort(a,a+6);
  27. if(a[4]==a[5])
  28. printf("Elephant\n");
  29. else
  30. printf("Bear\n");
  31. }
  32. return 0;
  33. }

F - MUH and Important Things

[CodeForces - 471B]


It's time polar bears Menshykov and Uslada from the zoo of St.
Petersburg and elephant Horace from the zoo of Kiev got down to
business. In total, there are n tasks for the day and each animal
should do each of these tasks. For each task, they have evaluated its
difficulty. Also animals decided to do the tasks in order of their
difficulty. Unfortunately, some tasks can have the same difficulty, so
the order in which one can perform the tasks may vary.

Menshykov, Uslada and Horace ask you to deal with this nuisance and
come up with individual plans for each of them. The plan is a sequence
describing the order in which an animal should do all the n tasks.
Besides, each of them wants to have its own unique plan. Therefore
three plans must form three different sequences. You are to find the
required plans, or otherwise deliver the sad news to them by stating
that it is impossible to come up with three distinct plans for the
given tasks.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of
tasks. The second line contains n integers h1, h2, ..., hn
(1 ≤ hi ≤ 2000), where hi is the difficulty of the i-th task. The
larger number hi is, the more difficult the i-th task is.

Output

In the first line print "YES" (without the quotes), if it is possible
to come up with three distinct plans of doing the tasks. Otherwise
print in the first line "NO" (without the quotes). If three desired
plans do exist, print in the second line n distinct integers that
represent the numbers of the tasks in the order they are done
according to the first plan. In the third and fourth line print two
remaining plans in the same form.

If there are multiple possible answers, you can print any of them.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. struct task
  6. {
  7. int o,d;
  8. }a[2005];
  9. int n,ans=1;
  10. task t1,t2,t3;
  11. int cmp(task a,task b)
  12. {
  13. return a.d<b.d;
  14. }
  15. void pri()
  16. {
  17. printf("%d",a[0].o);
  18. for(int j=1;j<n;j++)
  19. {
  20. printf(" %d",a[j].o);
  21. }
  22. printf("\n");
  23. }
  24. int main()
  25. {
  26. scanf("%d",&n);
  27. for(int i=0;i<n;i++)
  28. {
  29. a[i].o=i+1;
  30. scanf("%d",&a[i].d);
  31. }
  32. sort(a,a+n,cmp);
  33. for(int i=0;i<n;i++)
  34. {
  35. if( (i+1<n&&a[i].d==a[i+1].d)
  36. && (i+2<n&&a[i].d==a[i+2].d) )
  37. {
  38. printf("YES\n");
  39. pri();
  40. t1=a[i],t2=a[i+1],t3=a[i+2];
  41. a[i]=t2,a[i+1]=t1;
  42. pri();
  43. a[i]=t1,a[i+1]=t2;
  44. a[i]=t3,a[i+2]=t1;
  45. pri();
  46. return 0;
  47. }
  48. if( (i+1<n&&a[i].d==a[i+1].d)
  49. && (i+2<n&&a[i].d!=a[i+2].d) )
  50. {
  51. for(int j=i+2;j<n;j++)
  52. {
  53. if(j+1<n&&a[j].d==a[j+1].d)
  54. {
  55. printf("YES\n");
  56. pri();
  57. t1=a[i],t2=a[i+1];
  58. a[i]=t2,a[i+1]=t1;
  59. pri();
  60. a[i]=t1,a[i+1]=t2;
  61. t1=a[j],t2=a[j+1];
  62. a[j]=t2,a[j+1]=t1;
  63. pri();
  64. return 0;
  65. }
  66. }
  67. break;
  68. }
  69. }
  70. printf("NO\n");
  71. return 0;
  72. }

G - MUH and House of Cards

[CodeForces - 471C]


Polar bears Menshykov and Uslada from the zoo of St. Petersburg and
elephant Horace from the zoo of Kiev decided to build a house of
cards. For that they've already found a hefty deck of n playing cards.
Let's describe the house they want to make:

The house consists of some non-zero number of floors. Each floor
consists of a non-zero number of rooms and the ceiling. A room is two
cards that are leaned towards each other. The rooms are made in a row,
each two adjoining rooms share a ceiling made by another card. Each
floor besides for the lowest one should contain less rooms than the
floor below. Please note that the house may end by the floor with more
than one room, and in this case they also must be covered by the
ceiling. Also, the number of rooms on the adjoining floors doesn't
have to differ by one, the difference may be more.

While bears are practicing to put cards, Horace tries to figure out
how many floors their house should consist of. The height of the house
is the number of floors in it. It is possible that you can make a lot
of different houses of different heights out of n cards. It seems that
the elephant cannot solve this problem and he asks you to count the
number of the distinct heights of the houses that they can make using
exactly n cards.

Input

The single line contains integer n (1 ≤ n ≤ 1012) — the number of
cards.

Output

Print the number of distinct heights that the houses made of exactly n
cards can have.

Hint

In the first sample you can build only these two houses (remember, you
must use all the cards):

此处输入图片的描述
Thus, 13 cards are enough only for two floor houses, so the answer is
1.

The six cards in the second sample are not enough to build any house.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int main()
  8. {
  9. long long n;
  10. int ans=0;
  11. scanf("%lld",&n);
  12. for(long long i=1;i*(3*i+1)/2<=n;i++)
  13. {
  14. if((n-2*i)%3==0)
  15. ans++;
  16. }
  17. printf("%d\n",ans);
  18. return 0;
  19. }

I - George and Accommodation

[CodeForces - 467A]


George has recently entered the BSUCP (Berland State University for
Cool Programmers). George has a friend Alex who has also entered the
university. Now they are moving into a dormitory.

George and Alex want to live in the same room. The dormitory has n
rooms in total. At the moment the i-th room has pi people living in it
and the room can accommodate qi people in total (pi ≤ qi). Your task
is to count how many rooms has free place for both George and Alex.

Input

The first line contains a single integer n (1 ≤ n ≤ 100) — the number
of rooms.

The i-th of the next n lines contains two integers pi and qi
(0 ≤ pi ≤ qi ≤ 100) — the number of people who already live in the
i-th room and the room's capacity.

Output

Print a single integer — the number of rooms where George and Alex can
move in.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,x,y,sum=0;
  8. scanf("%d",&n);
  9. for(int i=0;i<n;i++)
  10. {
  11. scanf("%d%d",&x,&y);
  12. if(y-x>=2)
  13. sum++;
  14. }
  15. printf("%d\n",sum);
  16. return 0;
  17. }

J - Fedor and New Game

[CodeForces - 467B]


After you had helped George and Alex to move in the dorm, they went to
help their friend Fedor play a new computer game «Call of Soldiers 3».

The game has (m + 1) players and n types of soldiers in total. Players
«Call of Soldiers 3» are numbered form 1 to (m + 1). Types of soldiers
are numbered from 0 to n - 1. Each player has an army. Army of the
i-th player can be described by non-negative integer xi. Consider
binary representation of xi: if the j-th bit of number xi equal to
one, then the army of the i-th player has soldiers of the j-th type.

Fedor is the (m + 1)-th player of the game. He assume that two players
can become friends if their armies differ in at most k types of
soldiers (in other words, binary representations of the corresponding
numbers differ in at most k bits). Help Fedor and count how many
players can become his friends.

Input

The first line contains three integers n, m, k (1 ≤ k ≤ n ≤ 20;
1 ≤ m ≤ 1000).

The i-th of the next (m + 1) lines contains a single integer xi
(1 ≤ xi ≤ 2n - 1), that describes the i-th player's army. We remind
you that Fedor is the (m + 1)-th player.

Output

Print a single integer — the number of Fedor's potential friends.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,m,k,ans=0;
  8. int a[1005];
  9. scanf("%d%d%d",&n,&m,&k);
  10. for(int i=0;i<=m;i++)
  11. scanf("%d",&a[i]);
  12. for(int i=0;i<m;i++)
  13. {
  14. int ha=a[m]^a[i];
  15. int sum=0;
  16. while(ha>0)
  17. {
  18. sum+=(ha&1);
  19. ha=ha>>1;
  20. }
  21. if(sum<=k)
  22. ans++;
  23. }
  24. printf("%d\n",ans);
  25. return 0;
  26. }

K - George and Job

[CodeForces - 467C]


The new ITone 6 has been released recently and George got really keen
to buy it. Unfortunately, he didn't have enough money, so George was
going to work as a programmer. Now he faced the following problem at
the work.

Given a sequence of n integers p1, p2, ..., pn. You are to choose k
pairs of integers:

[l1, r1], [l2, r2], ..., [lk, rk]
(1 ≤ l1 ≤ r1 < l2 ≤ r2 < ... < lk ≤ rk ≤ n; ri - li + 1 = m),  in such
a way that the value of sum is maximal possible. Help George to cope
with the task.

Input

The first line contains three integers n, m and k
(1 ≤ (m × k) ≤ n ≤ 5000). The second line contains n integers
p1, p2, ..., pn (0 ≤ pi ≤ 109).

Output

Print an integer in a single line — the maximum possible value of sum.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <queue>
  6. using namespace std;
  7. int n,m,k;
  8. long long ans=0;
  9. long long a[5005],b[5005],v[5005][5005];
  10. int main()
  11. {
  12. scanf("%d%d%d",&n,&m,&k);
  13. scanf("%lld",&a[1]);
  14. for(int i=2;i<=n;i++)
  15. {
  16. int x;
  17. scanf("%d",&x);
  18. a[i]=a[i-1]+x;
  19. }
  20. b[m]=a[m];
  21. for(int i=m;i<=n;i++)
  22. b[i]=a[i]-a[i-m];
  23. memset(v, 0, sizeof(0));
  24. for(int i=1;i<=k;i++)
  25. {
  26. long long xmax=0;
  27. for(int j=i*m;j<=n;j++)
  28. {
  29. xmax=max( v[i-1][j-m]+b[j],xmax);
  30. v[i][j]=xmax;
  31. }
  32. }
  33. printf("%lld\n",v[k][n]);
  34. return 0;
  35. }

M - Cheap Travel

[CodeForces - 466A]


Ann has recently started commuting by subway. We know that a one ride
subway ticket costs a rubles. Besides, Ann found out that she can buy
a special ticket for m rides (she can buy it several times). It costs
b rubles. Ann did the math; she will need to use subway n times. Help
Ann, tell her what is the minimum sum of money she will have to spend
to make n rides?

Input

The single line contains four space-separated integers n, m, a, b
(1 ≤ n, m, a, b ≤ 1000) — the number of rides Ann has planned, the
number of rides covered by the m ride ticket, the price of a one ride
ticket and the price of an m ride ticket.

Output

Print a single integer — the minimum sum in rubles that Ann will need
to spend.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. int main()
  6. {
  7. int n,m,a,b,sum=0;
  8. scanf("%d%d%d%d",&n,&m,&a,&b);
  9. if(m>=n)
  10. {
  11. sum=n*a<b?n*a:b;
  12. }
  13. else
  14. {
  15. if(a<=1.0*b/m)
  16. sum=n*a;
  17. else
  18. {
  19. sum+=(n/m*b);
  20. sum+= (n-n/m*m)*a < b ? (n-n/m*m)*a : b;
  21. }
  22. }
  23. printf("%d\n",sum);
  24. return 0;
  25. }
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int n,m,a,b;
  8. int main()
  9. {
  10. scanf("%d%d%d%d",&n,&m,&a,&b);
  11. int sum,ans=n*a;
  12. for(int i=0;i<=n/m+1;i++)
  13. {
  14. if(n>=i*m)
  15. sum=i*b+(n-i*m)*a;
  16. else
  17. sum=i*b;
  18. ans=ans<=sum?ans:sum;
  19. }
  20. printf("%d\n",ans);
  21. return 0;
  22. }

N - Wonder Room

[CodeForces - 466B]


The start of the new academic year brought about the problem of
accommodation students into dormitories. One of such dormitories has a
a × b square meter wonder room. The caretaker wants to accommodate
exactly n students there. But the law says that there must be at least
6 square meters per student in a room (that is, the room for n
students must have the area of at least 6n square meters). The
caretaker can enlarge any (possibly both) side of the room by an
arbitrary positive integer of meters. Help him change the room so as
all n students could live in it and the total area of the room was as
small as possible.

Input

The first line contains three space-separated integers n, a and b
(1 ≤ n, a, b ≤ 10^9) — the number of students and the sizes of the
room.

Output

Print three integers s, a1 and b1 (a ≤ a1; b ≤ b1) — the final area of
the room and its sizes. If there are multiple optimal solutions, print
any of them.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <string>
  6. using namespace std;
  7. int main()
  8. {
  9. long long flag=0,a,b,b1,n,xm;
  10. long long aa,bb,ans=1000000000000000000;
  11. scanf("%lld%lld%lld",&n,&a,&b);
  12. aa=a;
  13. bb=b;
  14. n*=6;
  15. if(a*b>=n)
  16. ans=a*b;
  17. if(a>b)
  18. {
  19. flag=1;
  20. swap(a, b);
  21. }
  22. xm=ceil(sqrt(n));
  23. for(long long i=a;i<=xm;i++)
  24. {
  25. if(b*i>=ans)
  26. break;
  27. b1=ceil(1.0*n/i);
  28. b1=b1>b?b1:b;
  29. if(i*b1<ans)
  30. {
  31. aa=i;
  32. bb=b1;
  33. ans=i*b1;
  34. }
  35. }
  36. if(flag)
  37. swap(aa, bb);
  38. printf("%lld\n%lld %lld\n",ans,aa,bb);
  39. return 0;
  40. }

O - Number of Ways

[CodeForces - 466C]


You've got array a[ 1 ], a[ 2 ], ..., a[n], consisting of n integers.
Count the number of ways to split all the elements of the array into
three contiguous parts so that the sum of elements in each part is the
same.

More formally, you need to find the number of such pairs of indices
i, j (2 ≤ i ≤ j ≤ n - 1), that 此处输入图片的描述.

Input

The first line contains integer n (1 ≤ n ≤ 5·105), showing how many
numbers are in the array. The second line contains n integers a1,
a2, ..., a[n] (|a[i]| ≤  109) — the elements of array a.

Output

Print a single integer — the number of ways to split the array into
three parts with the same sum.

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. using namespace std;
  5. long long a,sum[1000000];
  6. long long n,s=0,ans=0;
  7. int main()
  8. {
  9. scanf("%lld",&n);
  10. for(long long i=0;i<n;i++)
  11. {
  12. scanf("%lld",&a);
  13. if(i==0)
  14. {
  15. sum[i]=a;
  16. continue;
  17. }
  18. sum[i]=sum[i-1]+a;
  19. }
  20. if(sum[n-1]%3==0)
  21. {
  22. for(long long i=0;i<n-1;i++)
  23. {
  24. if(sum[i]==1.0*sum[n-1]/3*2)
  25. ans+=s;
  26. if(sum[i]==1.0*sum[n-1]/3)
  27. s++;
  28. }
  29. }
  30. printf("%lld\n",ans);
  31. return 0;
  32. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注