目录
Problem A:zcmu-4959 ly的新闹钟(思维+打表)
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 125 Solved: 27
[Submit][Status][Web Board]Description
最近ly买了一个新闹钟,每天盯着这个闹钟看,越看越喜欢....为了不让ly浪费时间,wjw决定给ly提出一个要求....从x点整到y点整,时针和分针总共会碰到几次?(虽然这个要求一样是在浪费时间.....)
Input
第一行输入一个整数T表示数据数量,接下来的T行,每行包含两个正整数 A , B ( 0 <= A < B <= 24 )
这里给出一个条件,时针和分针的碰撞在A点整的时候可以计数,在B点整的时候碰撞不计入答案
Output
每组数据输出一个整数表示A点整到B点整时针和分针的碰撞次数
Sample Input
2
14 17
5 9
Sample Output
3
4
【分析】据说是水题,,可是我想了很久,一直在纠结于算角度。。这道题打表找规律吧。
【代码】
#include
using namespace std;
double a[25];
int main()
{
/*******打表*******
a[0]=0;
for(int i=1;i<25;i++)
{
a[i]=a[i-1]+12.0/11;
cout<
Problem D: ly的二叉树(快速幂+卡特兰数)
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 83 Solved: 37
[Submit][Status][Web Board]Description
某一天,ly正在上数据结构课。老师在讲台上面讲着二叉树,ly在下面发着呆。
突然ly想到一个问题:对于一棵n个无编号节点的有根二叉树,有多少种形态呐?你能告诉她吗?Input
多组输入,处理到文件结束
每一组输入一行,一个正整数n(1≤n≤1000000),意义如题目所述。Output
每组数据输出一行,包含一个正整数表示答案,由于数字可能非常大,你只需要把最后的结果对1000000007取模即可。
Sample Input
3
Sample Output
5
【分析】
卡特兰数是组合数学中一个经常出现在各种计数问题中出现的数列。
卡特兰数一般公式:
另类递归式:
H(n) = ( ( 4*n-2 )/( n+1 ) )*H( n-1 );
快速幂:
int quickpow(int a,int b,int c) { int ans=1; a=a%c; while(b>0) { if(b%2==1) ans=(ans*a)%c; b=b/2; a=(a*a)%c; } return ans; }
【代码】
#include
using namespace std;
#define ll long long
const ll mod = 1000000007;
const int maxn = 1e6+5;
ll num[maxn];
ll quickpow(ll a,ll b)
{
ll sum=1;
while(b)
{
if(b&1)
sum=sum*a%mod;//,cout<<"sum="<>=1;
a=a*a%mod;
}
return sum;
}
void ff()
{
num[0]=num[1]=1;
for(int i=2;i
Problem E: 铺地毯(模拟)
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 218 Solved: 111
[Submit][Status][Web Board]Description
为了准备一个独特的颁奖典礼,组织者在会场的一片矩形区域(可看做是平面直角坐标系的第一象限)铺上一些矩形地毯,一共有n张地毯,编号从 1 到n。现在将这些地毯按照编号从小到大的顺序平行于坐标轴先后铺设,后铺的地毯覆盖在前面已经铺好的地毯之上。
地毯铺设完成后,组织者想知道覆盖地面某个点的最上面的那张地毯的编号。注意:在矩形地毯边界和四个顶点上的点也算被地毯覆盖。Input
输入共 n+2行。
第一行有一个整数n,表示总共有 n张地毯。
接下来的 n行中,第 i+1行表示编号 i的地毯的信息,包含四个正整数 a,b,g,k,每两个整数之间用一个空格隔开,分别表示铺设地毯的左下角的坐标(a,b)以及地毯在 x轴和 y轴方向的长度。
第 n+2 行包含两个正整数 x 和 y,表示所求的地面的点的坐标(x,y)。Output
输出共 1 行,一个整数,表示所求的地毯的编号;若此处没有被地毯覆盖则输出-1。
Sample Input
3
1 0 2 3
0 2 3 3
2 1 3 3
2 2
Sample Output
3
HINT
数据范围:
30% n<=2
50% 0<=a,b,g,k<=100
100% 0<=n<=10000, 0<=a,b,g,k<=100000
NOIP2011 DAY1 carpet
【分析】就模拟一下就好了,判断下坐标,从最后一块地毯开始遍历。
#include
using namespace std;
const int maxn=1e4+5;
struct node{
int x1,y1,x2,y2,x3,y3,x4,y4;
}a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
{
int aa,b,g,k;
scanf("%d%d%d%d",&aa,&b,&g,&k);
a[i].x1=aa,a[i].y1=b+k;
a[i].x2=aa+g,a[i].y2=b+k;
a[i].x3=aa+g,a[i].y3=b;
a[i].x4=aa,a[i].y4=b;
}
int x,y;
int flag=1;
scanf("%d%d",&x,&y);
for(int i=n;i>0;i--)
{
if((a[i].x1<=x&&a[i].y1>=y)&&(a[i].x2>=x&&a[i].y2>=y)&&(a[i].x3>=x&&a[i].y3<=y)&&(a[i].x4<=x&&a[i].y4<=y))
{
printf("%d\n",i);
flag=0;
break;
}
}
if(flag)printf("-1\n");
}
return 0;
}
G: Chord(模拟)
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 34 Solved: 23
[Submit][Status][Web Board]Description
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya studies music.
He has learned lots of interesting stuff. For example, he knows that there are 12 notes: C, C#, D, D#, E, F, F#, G, G#, A, B, H. He also knows that the notes are repeated cyclically: after H goes C again, and before C stands H. We will consider the C note in the row's beginning and the C note after the H similar and we will identify them with each other. The distance between the notes along the musical scale is measured in tones: between two consecutive notes there's exactly one semitone, that is, 0.5 tone. The distance is taken from the lowest tone to the uppest one, that is, the distance between C and E is 4 semitones and between E and C is 8 semitones
Vasya also knows what a chord is. A chord is an unordered set of no less than three notes. However, for now Vasya only works with triads, that is with the chords that consist of exactly three notes. He can already distinguish between two types of triads − major and minor.
Let's define a major triad. Let the triad consist of notes X, Y and Z. If we can order the notes so as the distance along the musical scale between X and Y equals 4 semitones and the distance between Y and Z is 3 semitones, then the triad is major. The distance between X and Z, accordingly, equals 7 semitones.
A minor triad is different in that the distance between X and Y should be 3 semitones and between Y and Z − 4 semitones.
For example, the triad "C E G" is major: between C and E are 4 semitones, and between E and G are 3 semitones. And the triplet "C# B F" is minor, because if we order the notes as "B C# F", than between B and C# will be 3 semitones, and between C# and F − 4 semitones.
Help Vasya classify the triad the teacher has given to him.
Input
The only line contains 3 space-separated notes in the above-given notation.
Output
Print "major" if the chord is major, "minor" if it is minor, and "strange" if the teacher gave Vasya some weird chord which is neither major nor minor. Vasya promises you that the answer will always be unambiguous. That is, there are no chords that are both major and minor simultaneously.
Examples
Input
C E G
Output
major
Input
C# B F
Output
minor
Input
A B H
Output
strange
【分析】两个音阶只能相差4或3.比较大小然后输出相应字符串。
【代码】
#include
using namespace std;
mapm;
int judge(int x,int y,int z)
{
if((y-x+12)%12==4 && (z-y+12)%12==3)return 1;//x,y,z
else if((z-x+12)%12==4 &&(y-z+12)%12==3)return 1;//x,z,y
else if((y-z+12)%12==4 && (x-y+12)%12==3)return 1;//z,y,x
else if((x-y+12)%12==4 && (z-x+12)%12==3)return 1;//y,x,z
else if((z-y+12)%12==4 && (x-z+12)%12==3)return 1;//y,z,x
else if((x-z+12)%12==4 && (y-x+12)%12==3)return 1;//z,x,y
else return 0;
}
int judge2(int x,int y,int z)
{
if((y-x+12)%12==3 && (z-y+12)%12==4)return 1;//x,y,z
else if((z-x+12)%12==3 &&(y-z+12)%12==4)return 1;//x,z,y
else if((x-z+12)%12==3 && (y-x+12)%12==4)return 1;//z,x,y
else if((x-y+12)%12==3 && (z-x+12)%12==4)return 1;//y,x,z
else if((z-y+12)%12==3 && (x-z+12)%12==4)return 1;//y,z,x
else if((y-z+12)%12==3 && (x-y+12)%12==4)return 1;//z,y,x
else return 0;
}
int main()
{
m["C"]=0;m["C#"]=1;m["D"]=2;m["D#"]=3;m["E"]=4;m["F"]=5;
m["F#"]=6;m["G"]=7;m["G#"]=8;m["A"]=9;m["B"]=10;m["H"]=11;
string s1,s2,s3;
while(cin>>s1>>s2>>s3)
{
int x=m[s1],y=m[s2],z=m[s3];
if(judge(x,y,z))printf("major\n");
else
{
if(judge2(x,y,z))printf("minor\n");
else printf("strange\n");
}
}
return 0;
}
Problem F: Cableway
Time Limit: 2 Sec Memory Limit: 256 MB
Submit: 36 Solved: 27
[Submit][Status][Web Board]Description
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A group of university students wants to get to the top of a mountain to have a picnic there. For that they decided to use a cableway.
A cableway is represented by some cablecars, hanged onto some cable stations by a cable. A cable is scrolled cyclically between the first and the last cable stations (the first of them is located at the bottom of the mountain and the last one is located at the top). As the cable moves, the cablecar attached to it move as well.
The number of cablecars is divisible by three and they are painted three colors: red, green and blue, in such manner that after each red cablecar goes a green one, after each green cablecar goes a blue one and after each blue cablecar goes a red one. Each cablecar can transport no more than two people, the cablecars arrive with the periodicity of one minute (i. e. every minute) and it takes exactly 30 minutes for a cablecar to get to the top.
All students are divided into three groups: r of them like to ascend only in the red cablecars, g of them prefer only the green ones and b of them prefer only the blue ones. A student never gets on a cablecar painted a color that he doesn't like,
The first cablecar to arrive (at the moment of time 0) is painted red. Determine the least time it will take all students to ascend to the mountain top.
Input
The first line contains three integers r, g and b (0≤r,g,b≤100). It is guaranteed that r+g+b>0, it means that the group consists of at least one student.
Output
Print a single number − the minimal time the students need for the whole group to ascend to the top of the mountain.
Examples
Input
1 3 2
Output
34
Input
3 2 1
Output
33
Note
Let's analyze the first sample.
At the moment of time 0 a red cablecar comes and one student from the r group get on it and ascends to the top at the moment of time 30.
At the moment of time 1 a green cablecar arrives and two students from the g group get on it; they get to the top at the moment of time 31.
At the moment of time 2 comes the blue cablecar and two students from the b group get on it. They ascend to the top at the moment of time 32.
At the moment of time 3 a red cablecar arrives but the only student who is left doesn't like red and the cablecar leaves empty.
At the moment of time 4 a green cablecar arrives and one student from the g group gets on it. He ascends to top at the moment of time 34.
Thus, all the students are on the top, overall the ascension took exactly 34 minutes.
#include
using namespace std;
int main()
{
int r,g,b;
while(~scanf("%d%d%d",&r,&g,&b))
{
int flag=1,ans=0;
if(r>=2)ans=30,r-=2;
else if(r==1)ans=30,r-=1;
else ans=30;
while(r!=0 || g!=0 ||b!=0)
{
if(flag%3==0)
{
if(r>=2)ans+=1,r-=2;
else if(r==1)ans+=1,r-=1;
else ans+=1;
flag++;
}
else if(flag%3==1)
{
if(g>=2)ans+=1,g-=2;
else if(g==1)ans+=1,g-=1;
else ans+=1;
flag++;
}
else
{
if(b>=2)ans+=1,b-=2;
else if(b==1)ans+=1,b-=1;
else ans+=1;
flag++;
}
}
printf("%d\n",ans);
}
return 0;
}
1757: 内部收益率
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 116 Solved: 57
[Submit][Status][Web Board]Description
Input
Output
Sample Input
1
-1 2
2
-8 6 9
0
Sample Output
1.00
0.50
HINT
【题意】就是算公式啦 ,注意的一点就是,这个IRR只有一个解,因为这个公式,IRR减小,式子会增大,所以是线性的,所以,只有一个解的。
【分析】3个函数,快速幂+计算式子的值+二分查找;这里定义一个eps用于精度运算。
#include
using namespace std;
const int maxn=1e5+5;
const double eps=1e-5;
double cf[15];
int t;
double quickpower(double a,int b)
{
double ans=1;
while(b)
{
if(b&1)ans*=a;
b/=2;
a*=a;
}
return ans;
}
double compute(double x)
{
double ans=0;
for(int i=1;i<=t;i++)
ans+=1.0*cf[i]/quickpower(x+1,i);
return ans;
}
double findd()
{
double l=-1;
double r=maxn;
while(l
Working in Beijing
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 2106 Accepted Submission(s): 901
Problem Description
Mr. M is an undergraduate student of FDU. He finds an intern position in Beijing, so that he cannot attend all the college activities. But in some conditions, he must come back to Shanghai on certain date. We can assume the important activities that Mr. M must attend are occupy a whole day. Mr. M must take flight to Shanghai before that day and leave after that day. On the other hand, Mr. M is absent in Beijing and he will lose his salary for his absent.
Sometimes the cost of flight is much higher than the loss of salary, so to save the cost on the travel, Mr. M can stay in Shanghai to wait for another important date before he back to Beijing.
Now, Mr. M knows all of the important date in the next year. Help him schedule his travel to optimize the cost.Input
The input contains several test cases. The first line of single integer indicates the number of test cases.
For each test case, the first line contains three integers: n, a and b, denoting the number of important events, the cost of a single flight from Beijing to Shanghai or Shanghai to Beijing and the salary for a single day stay in Beijing. (1 <= n <= 100000, 1 <= a <= 1000000000, 1 <= b <=100)
Next line contains n integers ti, denoting the time of the important events. You can assume the ti are in increasing order and they are different from each other. (0 <= ti <= 10000000)Output
For each test case, output a single integer indicating the minimum cost for this year.
Sample Input
2
1 10 10
5
5 10 2
5 10 15 65 70
Sample Output
Case #1: 30
Case #2: 74
Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup
【分析】给出需要需要回去的次数,单程路费,日薪 ,求一年中最少花费。给出的时间的序列只是记录的一个时间点,但是花费时间只有一天。注意路费×2,。sum在增加的时候,要取这些天的薪水和路费的最小值。嗯~理解题意吧,理解清楚了就好了。注意数据范围。
【代码】
#include
using namespace std;
const int maxn=1e5+5;
int t[maxn];
int main()
{
int T,ca=0;
scanf("%d",&T);
while(T--)
{
int n,a,b;
scanf("%d%d%d",&n,&a,&b);
for(int i=0;i
The Frog's Games(模拟+二分查找)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 9809 Accepted Submission(s): 4500
Problem Description
The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The width of the river is L (1<= L <= 1000000000). There are n (0<= n <= 500000) stones lined up in a straight line from one side to the other side of the river. The frogs can only jump through the river, but they can land on the stones. If they fall into the river, they
are out. The frogs was asked to jump at most m (1<= m <= n+1) times. Now the frogs want to know if they want to jump across the river, at least what ability should they have. (That is the frog's longest jump distance).Input
The input contains several cases. The first line of each case contains three positive integer L, n, and m.
Then n lines follow. Each stands for the distance from the starting banks to the nth stone, two stone appear in one place is impossible.Output
For each case, output a integer standing for the frog's ability at least they should have.
Sample Input
6 1 2
2
25 3 3
11
2
18
Sample Output
4
11
【分析】宽度为L的河,呈一条直线放置n块石头,最多条m次。给出从出发点(岸上!!!)到第i块石头的距离,要求输出青蛙在满足上述要求的条件下要到达对岸,求,最大跳跃距离的最小值。这个不是让跳的次数少,而是要跳的距离小。理解题意!!
【代码】
#include
using namespace std;
const int maxn=5e5+5;
int stone[maxn];
int L,m,n;
bool judge(int x)
{
int now=0,dis=0,i=1;
while(dism)return false;
}
return true;
}
int main()
{
while(~scanf("%d%d%d",&L,&n,&m))
{
for(int i=1;i<=n;i++)
scanf("%d",&stone[i]);
stone[n+1]=L;
sort(stone+1,stone+1+n);
int l=0,r=999999999,ans=0;
while(l<=r)
{
int mid=(l+r)/2;
if(judge(mid))
{
ans=mid;
r=mid-1;
}
else l=mid+1;
}
printf("%d\n",ans);
}
}
The kth great number(优先队列)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 14741 Accepted Submission(s): 5696
Problem Description
Xiao Ming and Xiao Bao are playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao what the kth great number is. Because the number written by Xiao Ming is too much, Xiao Bao is feeling giddy. Now, try to help Xiao Bao.
Input
There are several test cases. For each test case, the first line of input contains two positive integer n, k. Then n lines follow. If Xiao Ming choose to write down a number, there will be an " I" followed by a number that Xiao Ming will write down. If Xiao Ming choose to ask Xiao Bao, there will be a "Q", then you need to output the kth great number.
Output
The output consists of one integer representing the largest number of islands that all lie on one line.
Sample Input
8 3
I 1
I 2
I 3
Q
I 5
Q
I 4
Q
Sample Output
1
2
3
Hint
Xiao Ming won't ask Xiao Bao the kth great number when the number of the written number is smaller than k. (1=
【分析】求当前第k大的数是多少;
【代码】
#include
using namespace std;
int main()
{
int n,k;
while(~scanf("%d%d",&n,&k))
{
char ch;
priority_queue,greater >pq;
while(n--)
{
getchar();
scanf("%c",&ch);
if(ch=='I')
{
int x;
scanf("%d",&x);
pq.push(x);
if(pq.size()>k)pq.pop();
}
else printf("%d\n",pq.top());
}
}
return 0;
}
Hexadecimal View(模拟)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3101 Accepted Submission(s): 1254
Problem Description
Hexadecimal is very important and useful for computer programmers. You are requested to provide a hexadecimal view for given data. The hexadecimal view is made up of one or more rows. Every row except the last one represents 16 characters. Each row consists of three columns separated by a space:
* addr: the 4-digit hexadecimal beginning address of this row.
* dump: the hexadecimal representation of this row, separating every two characters by a whitespace. If there are less than 16 characters in the last row, pad it with spaces.
* text: the ASCII translation of this row, with uppercase characters converted to lowercase and lowercase characters converted to uppercase.
Use lowercase for the letter digits. See sample for more details.Input
There are multiple test cases. Each line is a test case. The line is made up of no less than 1 and no more than 4096 printable characters including spaces.
Output
For each test case, output its hexadecimal view. Do not output any extra spaces after the last character of text.
Sample Input
Hex Dump #include
printf("Hello, World!\n"); main = do getLine >>= print . sum . map read . words Sample Output
0000: 4865 7820 4475 6d70 hEX dUMP 0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE
0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w 0010: 6f72 6c64 215c 6e22 293b ORLD!\N"); 0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN 0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU 0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W 0030: 6f72 6473 ORDS Author
WU, Zejun
Source
2011 Asia Dalian Regional Contest
【分析】模拟题。读懂了就挺简单的吧。一共三列,第一列是4位的16进制地址,第二列,字符的ASCII码转换为16进制输出,固定16个字符,不够的话用空格来补,第三列是大小写转换。
【代码】
#include
using namespace std;
char s[50010];
char change(char a)
{
if(a>='a'&&a<='z')a=toupper(a);
else if(a>='A'&&a<='Z')a=tolower(a);
return a;
}
int main()
{
while(gets(s))
{
int len=strlen(s);
for(int i=0;i
Magic Bitwise And Operation(dfs 剪枝)
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1713 Accepted Submission(s): 707
Problem Description
Given n integers, your task is to pick k out of them so that the picked number are minimum when do bitwise “AND” among all of them.
For example, there are three integers 5, 6 and 7. You are asked to pick two of them. Your possible strategy is (5, 6), (5, 7) or (6, 7). The values when do bitwise and all the picked numbers together are as follows:
5 and 6 = 4
5 and 7 = 5
6 and 7 = 6
The smallest one is 4.Input
There are multiple test cases for this problem. The first line of the input contains an integer denoting the number of test cases.
For each test case, there are two integers in the first line: n and k, denoting the number of given integers and the number of integers you are asked to pick out. n <= 40
The second line contains the n integers. You may assume that all integers are small than 2^60.
Notes: There are about one thousand randomly generated test cases. Fortunately 90% of them are relatively small.Output
For each test case, output only one integer is the smallest possible value.
Sample Input
3 3 2 5 6 7 8 2 238 153 223 247 111 252 253 247 40 10 1143632830316675007 558164877202423550 1152356080752164603 1143911006781551605 1132655005501751263 1152919305583327167 1141662230660382702 862439259920596463 1151777428397603327 1008771132016295871 855666336963428351 1151795583225167807 1152634943314572791 1071856693060561407 1132650872803426303 1124211056982081471 1152917106425982911 1152815392070041535 1080863910568853481 288230371856350975 1080720560532488126 864686455262281727 576460673919991167 574191342855241589 1152233760050118651 1152921504605798263 1152912708241186815 1079738008506187487 1075796261476483027 1080854478820730879 1152885219917823999 1151725162940854259 1147529498501577715 571956602920235519 1134545630643616248 1152921218991521790 1152921496000052703 1142788250826440703 1151654831778151421 1152780747522637695
Sample Output
Case #1: 4
Case #2: 9
Case #3: 36028797086245424
Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Warmup
【分析】爆搜+剪枝;队友的代码
首先预处理一下,每个数与后面全部的数相与的结果。INF定义为各个位全为1。
主要的剪枝:
#include
using namespace std;
typedef long long ll;
const ll INF=(1ll<<61)-1;
ll a[55],b[55];
int vis[55];
int n,k;
ll ans=INF;
//预处理,处理每个数与后面的所有的数做相与运算的结果
void init()
{
for(int i=0;i=ans)return;//剪枝。如果当前结果与后面所有的数相与运算的结果还大于ans,退出搜索。
if(pos==n)return;
dfs(pos+1,num+1,x&a[pos]);//继续下一个数
dfs(pos+1,num,x);//不继续下一个数
}
int main()
{
int T,ca=0;
scanf("%d",&T);
while(T--)
{
ans=INF;
memset(vis,0,sizeof(vis));
scanf("%d%d",&n,&k);
for(int i=0;i