比赛地址http://acm.sdibt.edu.cn/vjudge/contest/view.action?cid=2172#overview
A Height Ordering
Mrs. Chambers always has her class line up in height order (shortest at the front of the line). Every
September a new class of exactly 20 3rd graders arrive, all of different height. For the first few days it
takes a long time to get the kids in height order, since no one knows where they should be in the line.
Needless to say, there is quite a bit of jockeying around. This year Mrs. Chambers decided to try a new
method to minimize this ordering chaos. One student would be selected to be the first person in line.
Then, another student is selected and would find the first person in the line that is taller than him, and
stand in front of that person, thereby causing all the students behind him to step back to make room.
If there is no student that is taller, then he would stand at the end of the line. This process continues,
one student at-a-time, until all the students are in line, at which point the students will be lined up in
height order.
For this problem, you will write a program that calculates the total number of steps taken back
during the ordering process for a given class of students.
Input
The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by
20 non-negative unique integers separated by a single space. The 20 integers represent the height (in
millimeters) of each student in the class.
Output
For each data set there is one line of output. The single output line consists of the data set number,
K, followed by a single space followed by total number of steps taken back.
Input
4
1 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
2 919 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900
3 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 900
4 918 917 916 915 914 913 912 911 910 909 908 907 906 905 904 903 902 901 900 919
Output
1 0
2 190
3 19
4 171
题意:相邻位置可以交换,要求从大到小排序,问需要调换几次
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int c=1e9+7;
ll dp[20];
int main()
{
ll T,i,j,k,a[22],e,t;
cin>>T;
while(T--)
{
cin>>e;
ll ans=0;
for(i=0;i<20;i++)
cin>>a[i];
for(i=0;i<20;i++)
{
for(j=i+1;j<20;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
ans++;
}
}
}
cout<
D Happy Happy Prime Prime
RILEY VASHTEE: [reading from display] Find the next number in the sequence:
313 331 367 ...? What?
THE DOCTOR: 379.
MARTHA JONES: What?
THE DOCTOR: It’s a sequence of happy primes — 379.
MARTHA JONES: Happy what?
THE DOCTOR: Any number that reduces to one when you take the sum of the square of
its digits and continue iterating it until it yields 1 is a happy number. Any number that
doesn’t, isn’t. A happy prime is both happy and prime.
THE DOCTOR: I dunno, talk about dumbing down. Don’t they teach recreational mathe-
matics anymore?
Excerpted from “Dr. Who”, Episode 42 (2007).
The number 7 is certainly prime. But is it happy?
7 → 7 2 = 49
49 → 4 2 + 9 2 = 97
97 → 9 2 + 7 2 = 130
130 → 1 2 + 3 2 = 10
10 → 1 2 + 0 2 = 1
It is happy :-). As it happens, 7 is the smallest happy prime. Please note that for the purposes of
this problem, 1 is not prime.
For this problem you will write a program to determine if a number is a happy prime.
Input
The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by the
happy prime candidate, m, (1 ≤ m ≤ 10000).
Output
For each data set there is a single line of output. The single output line consists of the data set number,
K, followed by a single space followed by the candidate, m, followed by a single space, followed by ‘YES’
or ‘NO’, indicating whether m is a happy prime.
题意:找快乐素数,所谓快乐素数,首先要是素数,其次需要满足是快乐数,就是各个位数的平方最后的和是1.
Input
4
1 1
2 7
3 383
4 1000
Output
1 1 NO
2 7 YES
3 383 YES
4 1000 NO
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int c=1e9+7;
ll a[10100]={0};
void prim()//素数打表
{
ll i,j;
for(i=2;i<10100;i++)
{
if(a[i]==0)
{
for(j=i+i;j<10100;j+=i)
a[j]=1;
}
}
a[1]=1;
}
int main()
{
ll T,i,j,k,e,t,x,sum=0,xx;
cin>>T;
prim();
while(T--)
{
cin>>e;
cin>>x;
if(a[x]==1)//不是素数直接NO
{
cout<=10100)//不加会时间超限
break;
while(1)
{
if(xx==0)
{
break;
}
ll y=xx%10;
sum+=y*y;
xx=xx/10;
}
xx=sum;
sum=0;
m++;
}
// cout<
F A Rational Sequence
An infinite full binary tree labeled by positive rational numbers is defined by:
• The label of the root is 1/1.
• The left child of label p/q is p/(p+q).
• The right child oflabel p/q is (p+q)/q.
The top of the tree is shown in the following figure:
A rational sequence is defined by doing a level order (breadth first) traversal of the tree (indicated
by the light dashed line). So that:
F (1) = 1/1, F (2) = 1/2, F (3) = 2/1, F (4) = 1/3, F (5) = 3/2, F (6) = 2/3, . . .
Write a program which takes as input a rational number, p/q, in lowest terms and finds the next
rational number in the sequence. That is, if F (n) = p/q, then the result is F (n + 1).
Input
The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, which is then
followed by a space, then the numerator of the fraction, p, followed immediately by a fonward slash (/),
followed immediately by the denominator of the fraction, q. Both p and q will be relatively prime and
0 ≤ p, q ≤ 2147483647.
Output
For each data set there is a single line of output. It contains the data set number, K, followed by a
single space which is then followed by the numerator of the fraction, followed immediately by a forward
slash (‘/’) followed immediately by the denominator of the fraction. Inputs will be chosen such that
neither the numerator nor the denominator will overflow a 32-bit integer.
Input
5
1 1/1
2 1/3
3 5/2
4 2178309/1346269
5 1/10000000
Output
1 1/2
2 3/2
3 2/5
4 1346929/1860498
5 10000000/9999999
题意:找下一个结点的值,每个节点有左右两个值(p/q),一个节点的左孩子是(p/(p+q)),右孩子是((p+q)/q);
思路:找规律,先判断是父节点的左孩子还是右孩子,左孩子简单一点,右孩子往上找父节点,推出来
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAX=1e8+7;
int main()
{
ll T,i,j,x,y,e,a,b;
char c;
cin>>T;
while(T--)
{
cin>>e>>x>>c>>y;
if(x==y&&y==1)
{
cout<y)
{
/*找规律,往上一直找父节点,直到有x
以上是比赛过程中做出来的。
B Islands in the Data Stream
Given a sequence of integers a1, a2, a3, …, an, an island in the sequence is a contiguous subsequence for which each element is greater than the elements immediately before and after the subsequence. In the examples below, each island in the sequence has a bracket below it. The bracket for an island contained within another island is below the bracket of the containing island. Write a program that takes as input a sequence of 12 non-negative integers and outputs the number of islands in the sequence.
Input
The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently. Each data set consists of a single line of input. It contains the data set number, K, followed by 12 non-negative integers separated by a single space. The first and last integers in the sequence will be ‘0’.
Output
For each data set there is one line of output. The single output line consists of the data set number, K, followed by a single space followed by the number of islands in the sequence.
Sample Input
4
1 0 0 1 1 2 2 1 1 0 1 2 0
2 0 1 2 4 3 1 3 4 5 2 1 0
3 0 1 2 4 4 1 0 2 4 1 0 0
4 0 1 2 3 4 5 6 7 8 9 10 0
Sample Output
1 4
2 8
3 6
4 10
题意:求岛屿的个数,岛屿要满足的条件是在边界的值大于两边的值。
思路:按岛屿的长度来暴力,找到岛屿的起点和终点,再看是否满足条件。
第一组样例:112211 22 12 2
第二组样例:1243134521 243 43 4 3452 345 45 5
第三组样例:12441 244 44 241 24 4
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
G Growing Rectangular Spiral
A growing rectangular spiral is a connected sequence of straight-
line segments starting at the origin. The first segment goes right
(positive x direction). The next segment goes up (positive y di-
rection). The next segment goes left (negative x direction). The
next segment goes down (negative y direction) and the sequence
of directions repeats. Each segment has integer length and each
segment is at least one unit longer than the previous segment. In
the spiral on the right, the segment lengths are 1, 2, 4, 6, 7, 9,
11, 12, 15, 20.
Write a program to determine the shortest growing rectangu-
lar spiral (in total length) that ends at a given integer point (x, y)
in the first quadrant or determine that there is no such spiral.
Input
The first line of input contains a single integer P , (1 ≤ P ≤ 1000), which is the number of data sets
that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input consisting of three space separated decimal integers.
The first integer is the data set number. The next two integers are the x and y coordinates of the
desired end point (1 ≤ x ≤ 10000, 1 ≤ y ≤ 10000).
Output
For each data set there is a single line of output. If there is no spiral solution, the line consists of the
data set number, a single space and ‘NO PATH’ (without the quotes). If there is a solution, the line
consists of the data set number, a single space, the number of segments in the solution, a single space,
followed by the lengths of the segments in order, separated by single spaces. The input data will be
chosen so that no path requires more than 22 segments.
Sample Input
3
1 1 1
2 3 5
3 8 4
Sample Output
1 NO PATH
2 2 3 5
3 6 1 2 3 9 10 11
题意 :正如上图所示,要求进行螺旋状画图,长度是递增的,现在给出第一象限的一个点,问可以从什么长度递增而来。
思路:按照给定点(x,y)x=y,x
#include
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int MAX=1e6+7;
int main()
{
ll T,ee,a,b,c,x,y,m;
cin>>T;
while(T--)
{
cin>>ee>>x>>y;
if(x==y)
cout<y)
{
a=x-y+5;
b=x+2;
c=x+3;
m=6;
if(a>3&&b>a&&c>b)
{
cout<