这次5.1打了一场个人赛,已经连赛了三周了,有点疲惫感觉,可能自己太水了,每次都有点小紧张。
这次只解出来三道题,然而有一道按位与按位或的水题不知道思路实在是做题太少,还有就是第一题区间DP,也消耗了不少的时间,但是没有成功的写出来,还是不够熟练啊。
下面写报告
A. System Administrator
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarpus is a system administrator. There are two servers under his strict guidance — a and b. To stay informed about the servers‘ performance, Polycarpus executes commands "ping a" and "ping b". Each ping command sends exactly ten packets to the server specified in the argument of the command. Executing a program results in two integers x and y (x + y = 10; x, y ≥ 0). These numbers mean that x packets successfully reached the corresponding server through the network and y packets were lost.
Today Polycarpus has performed overall n ping commands during his workday. Now for each server Polycarpus wants to know whether the server is "alive" or not. Polycarpus thinks that the server is "alive", if at least half of the packets that we send to this server reached it successfully along the network.
Help Polycarpus, determine for each server, whether it is "alive" or not by the given commands and their results.
Input
The first line contains a single integer n (2 ≤ n ≤ 1000) — the number of commands Polycarpus has fulfilled. Each of the following n lines contains three integers — the description of the commands. The i-th of these lines contains three space-separated integers ti, xi, yi (1 ≤ ti ≤ 2; xi, yi ≥ 0; xi + yi = 10). If ti = 1, then the i-th command is "ping a", otherwise the i-th command is "ping b". Numbers xi, yi represent the result of executing this command, that is, xi packets reached the corresponding server successfully and yi packets were lost.
It is guaranteed that the input has at least one "ping a" command and at least one "ping b" command.
Output
In the first line print string "LIVE" (without the quotes) if server a is "alive", otherwise print "DEAD" (without the quotes).
In the second line print the state of server b in the similar format.
Sample test(s)
Input
21 5 52 6 4
Output
LIVELIVE
Input
31 0 102 0 101 10 0
Output
LIVEDEAD
Note
Consider the first test case. There 10 packets were sent to server a, 5 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall there were 10 packets sent to server b, 6 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network.
Consider the second test case. There were overall 20 packages sent to server a, 10 of them reached it. Therefore, at least half of all packets sent to this server successfully reached it through the network. Overall 10 packets were sent to server b, 0 of them reached it. Therefore, less than half of all packets sent to this server successfully reached it through the network.
这题是我第一个看的题,可能是运气好,刚刚好碰上了最简单的,丢包过去,看返回的包多还是没返回的包多判断死活的状态,就是简单的一累加看哪个大就好
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 int main() 9 { 10 int n,x,y,m,sumxa=0,sumxb=0,sumya=0,sumyb=0; 11 scanf("%d",&n); 12 while(n--) 13 { 14 scanf("%d%d%d",&m,&x,&y); 15 if(m==1) 16 { 17 sumxa+=x; 18 sumya+=y; 19 } 20 else 21 { 22 sumxb+=x; 23 sumyb+=y; 24 } 25 } 26 if(sumxa>=sumya) 27 { 28 printf("LIVE\n"); 29 } 30 else 31 printf("DEAD\n"); 32 if(sumxb>=sumyb) 33 { 34 printf("LIVE\n"); 35 } 36 else 37 printf("DEAD\n"); 38 printf("\n"); 39 return 0; 40 }
B. Internet Address
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Vasya is an active Internet user. One day he came across an Internet resource he liked, so he wrote its address in the notebook. We know that the address of the written resource has format:
<protocol>://<domain>.ru[/<context>]
where:
- <protocol> can equal either "http" (without the quotes) or "ftp" (without the quotes),
- <domain> is a non-empty string, consisting of lowercase English letters,
- the /<context> part may not be present. If it is present, then <context> is a non-empty string, consisting of lowercase English letters.
If string <context> isn‘t present in the address, then the additional character "/" isn‘t written. Thus, the address has either two characters "/" (the ones that go before the domain), or three (an extra one in front of the context).
When the boy came home, he found out that the address he wrote in his notebook had no punctuation marks. Vasya must have been in a lot of hurry and didn‘t write characters ":", "/", ".".
Help Vasya to restore the possible address of the recorded Internet resource.
Input
The first line contains a non-empty string that Vasya wrote out in his notebook. This line consists of lowercase English letters only.
It is guaranteed that the given string contains at most 50 letters. It is guaranteed that the given string can be obtained from some correct Internet resource address, described above.
Output
Print a single line — the address of the Internet resource that Vasya liked. If there are several addresses that meet the problem limitations, you are allowed to print any of them.
Sample test(s)
Input
httpsunrux
Output
http://sun.ru/x
Input
ftphttprururu
Output
ftp://http.ru/ruru
Note
In the second sample there are two more possible answers: "ftp://httpruru.ru" and "ftp://httpru.ru/ru".
这题wa了一次,原因是ftp,http后面如果紧跟着ru。其实是不能马上加.ru/的。因为要保证domain那块要有内容。。坑爹。。。
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 char s[100]; 9 int main() 10 { 11 int n,p=0,q=0,num=0,j,i,t1,t2; 12 scanf("%s",s); 13 if(s[0]==‘h‘) 14 {printf("http://");p=4;} 15 else 16 {printf("ftp://");p=3;} 17 q=p; 18 while((s[q]!=‘r‘||s[q+1]!=‘u‘)||p==q) 19 { 20 q++; 21 } 22 for(i=p;i<=q-1;i++) 23 printf("%c",s[i]); 24 printf(".ru"); 25 if(s[q+2]!=‘\0‘) 26 { 27 printf("/"); 28 q=q+2; 29 while(s[q]!=‘\0‘) 30 { 31 printf("%c",s[q++]); 32 } 33 } 34 printf("\n"); 35 return 0; 36 }
E. Mishap in Club
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarpus just has been out of luck lately! As soon as he found a job in the "Binary Cat" cafe, the club got burgled. All ice-cream was stolen.
On the burglary night Polycarpus kept a careful record of all club visitors. Each time a visitor entered the club, Polycarpus put down character "+" in his notes. Similarly, each time a visitor left the club, Polycarpus put character "-" in his notes. We know that all cases of going in and out happened consecutively, that is, no two events happened at the same time. Polycarpus doesn‘t remember whether there was somebody in the club at the moment when his shift begun and at the moment when it ended.
Right now the police wonders what minimum number of distinct people Polycarpus could have seen. Assume that he sees anybody coming in or out of the club. Each person could have come in or out an arbitrary number of times.
Input
The only line of the input contains a sequence of characters "+" and "-", the characters are written one after another without any separators. The characters are written in the order, in which the corresponding events occurred. The given sequence has length from 1 to 300 characters, inclusive.
Output
Print the sought minimum number of people
Sample test(s)
Input
+-+-+
Output
1
Input
---
Output
3 这题刚开始sb了。。还想算店内人员加上多跑出来的人。没有意识到。多跑出来的人也是可以再跑回去的。所以这题最好看作是店内和店外两部分,然后分别计数就好。从外面跑进去,外面-1,里面+1,否则反之。如果店内空的时候还冒出来一个人就外面+1,同理外面空的时候还有人跑进去就里面+1 下附代码
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 char s[100]; 9 int main() 10 { 11 int i=0,num=0,club=0,mark1=0,mark2=0; 12 13 scanf("%s",s); 14 while(s[i]!=‘\0‘) 15 { 16 if(s[i]==‘+‘) 17 { 18 if(num>0) 19 num--; 20 club++; 21 } 22 else 23 { 24 if(club>0) 25 {club--;num++;} 26 else 27 num++; 28 } 29 i++; 30 } 31 num+=club; 32 printf("%d\n",num); 33 return 0; 34 }
下面开始都是没做出来的题目了。。先放一道比较水的按位与按位或
D. Restoring Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently Polycarpus has learned the "bitwise AND" operation (which is also called "AND") of non-negative integers. Now he wants to demonstrate the school IT teacher his superb manipulation with the learned operation.
For that Polycarpus came to school a little earlier and wrote on the board a sequence of non-negative integers a1, a2, ..., an. He also wrote a square matrix b of size n × n. The element of matrix b that sits in the i-th row in the j-th column (we‘ll denote it as bij) equals:
- the "bitwise AND" of numbers ai and aj (that is, bij = ai & aj), if i ≠ j;
- -1, if i = j.
Having written out matrix b, Polycarpus got very happy and wiped a off the blackboard. But the thing is, the teacher will want this sequence to check whether Polycarpus‘ calculations were correct. Polycarus urgently needs to restore the removed sequence of integers, or else he won‘t prove that he can count correctly.
Help Polycarpus, given matrix b, restore the sequence of numbers a1, a2, ..., an, that he has removed from the board. Polycarpus doesn‘t like large numbers, so any number in the restored sequence mustn‘t exceed 109.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the size of square matrix b. Next n lines contain matrix b. The i-th of these lines contains n space-separated integers: the j-th number represents the element of matrix bij. It is guaranteed, that for all i (1 ≤ i ≤ n) the following condition fulfills: bii = -1. It is guaranteed that for all i, j (1 ≤ i, j ≤ n; i ≠ j) the following condition fulfills: 0 ≤ bij ≤ 109, bij = bji.
Output
Print n non-negative integers a1, a2, ..., an (0 ≤ ai ≤ 109) — the sequence that Polycarpus wiped off the board. Separate the numbers by whitespaces.
It is guaranteed that there is sequence a that satisfies the problem conditions. If there are multiple such sequences, you are allowed to print any of them.
Sample test(s)
Input
1-1
Output
0
Input
3-1 18 018 -1 00 0 -1
Output
18 18 0
Input
4-1 128 128 128128 -1 148 160128 148 -1 128128 160 128 -1
Output
128 180 148 160
Note
If you do not know what is the "bitwise AND" operation please read: http://en.wikipedia.org/wiki/Bitwise_operation
这题只是对操作要特别理解清楚,&操作是a和b都是1,结果才是1,|操作是a和b都是0,结果才是0,剩下情况都是1。
所以矩阵其实是 -1 1&2 1&3 1&4这样
然后每行的结果有1的位。那么原来的数字上肯定有1(因为&操作要同时是1才是1),所以用|操作来还原,多次还原位数然后重叠在一起,就得到一组可能的解(然而并不是所有解,存在多组解)
下附代码
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 char s[5001]; 9 int a[500][500]={0}; 10 int b[501]={0}; 11 int main() 12 { 13 int n,l,r,c,num=0,p=0,q=0,ans=0,i,t,j,min1=5005,max1=-1; 14 scanf("%d",&n); 15 for(i=0;i<n;i++) 16 for(j=0;j<n;j++) 17 { 18 scanf("%d",&a[i][j]); 19 if(i!=j) 20 b[i]|=a[i][j]; 21 } 22 for(i=0;i<n-1;i++) 23 printf("%d ",b[i]); 24 printf("%d\n",b[n-1]); 25 return 0; 26 }
G - Log Stream Analysis
Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 245F
Description
You‘ve got a list of program warning logs. Each record of a log stream is a string in this format:
" 2012-MM-DD HH:MM:SS:MESSAGE" (without the quotes).
String "MESSAGE" consists of spaces, uppercase and lowercase English letters and characters "!", ".", ",", "?". String "2012-MM-DD" determines a correct date in the year of 2012. String "HH:MM:SS" determines a correct time in the 24 hour format.
The described record of a log stream means that at a certain time the record has got some program warning (string "MESSAGE" contains the warning‘s description).
Your task is to print the first moment of time, when the number of warnings for the last n seconds was not less than m.
Input
The first line of the input contains two space-separated integers n and m(1 ≤ n, m ≤ 10000).
The second and the remaining lines of the input represent the log stream. The second line of the input contains the first record of the log stream, the third line contains the second record and so on. Each record of the log stream has the above described format. All records are given in the chronological order, that is, the warning records are given in the order, in which the warnings appeared in the program.
It is guaranteed that the log has at least one record. It is guaranteed that the total length of all lines of the log stream doesn‘t exceed 5·106 (in particular, this means that the length of some line does not exceed 5·106 characters). It is guaranteed that all given dates and times are correct, and the string ‘MESSAGE" in all records is non-empty.
Output
If there is no sought moment of time, print -1. Otherwise print a string in the format "2012-MM-DD HH:MM:SS" (without the quotes) — the first moment of time when the number of warnings for the last n seconds got no less than m.
Sample Input
Input
60 32012-03-16 16:15:25: Disk size is2012-03-16 16:15:25: Network failute2012-03-16 16:16:29: Cant write varlog2012-03-16 16:16:42: Unable to start process2012-03-16 16:16:43: Disk size is too small2012-03-16 16:16:53: Timeout detected
Output
2012-03-16 16:16:43
Input
1 22012-03-16 23:59:59:Disk size2012-03-17 00:00:00: Network2012-03-17 00:00:01:Cant write varlog
Output
-1
Input
2 22012-03-16 23:59:59:Disk size is too sm2012-03-17 00:00:00:Network failute dete2012-03-17 00:00:01:Cant write varlogmysq
Output
2012-03-17 00:00:00 这题主要是在比赛的时候不知道gets是读一整行包括空格的,所以导致不会做。同时这个英文理解能力也很伤啊,这题是说当前事件与第一个事件相隔时间超过n,且此时队列中至少有m个事件,才输出结果,否则-1先全部转换成秒,然后比较。wa了一次,是因为数组开小了。。。汗。。注意!不符合条件的需要不断的出队,直到符合条件的为止。 举第一个为例:60 32012-03-16 16:15:25: Disk size is2012-03-16 16:15:25: Network failute 入队然后读到2012-03-16 16:16:29: Cant write varlog时,第一个不符合,出队,第二个也不符合出队,然后继续读2012-03-16 16:16:42: Unable to start process2012-03-16 16:16:43: Disk size is too small读到第三个的时候,符合两个条件便输出2012-03-16 16:16:43
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 int month[15]={0,31,29,31,30,31,30,31,31,30,31,30};//2月只有29哦 9 char s[10000000]; 10 queue<long long>q; 11 int main() 12 { 13 14 long long int time=0,Month=0,Date=0,Hour=0,Minute=0,Second=0; 15 int n,m,i; 16 scanf("%d%d",&n,&m); 17 getchar(); 18 while(gets(s)!=NULL) 19 { 20 Month=(s[5]-48)*10+(s[6]-48); 21 Date=(s[8]-48)*10+(s[9]-48); 22 Hour=(s[11]-48)*10+(s[12]-48); 23 Minute=(s[14]-48)*10+(s[15]-48); 24 Second=(s[17]-48)*10+(s[18]-48); 25 for(i=1;i<Month;i++) 26 Date+=month[i]; 27 time=Date*86400+Hour*3600+Minute*60+Second; 28 q.push(time); 29 while(q.front()+n<=time)//不符合条件的需要不断的出队 30 {q.pop();} 31 if(q.size()>=m) 32 {printf("2012-%c%c-%c%c %c%c:%c%c:%c%c\n",s[5],s[6],s[8],s[9],s[11],s[12],s[14],s[15],s[17],s[18]); 33 return 0;} 34 } 35 printf("-1\n"); 36 return 0; 37 }
C. Game with Coins
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Two pirates Polycarpus and Vasily play a very interesting game. They have n chests with coins, the chests are numbered with integers from 1 to n. Chest number i has ai coins.
Polycarpus and Vasily move in turns. Polycarpus moves first. During a move a player is allowed to choose a positive integer x (2·x + 1 ≤ n) and take a coin from each chest with numbers x, 2·x, 2·x + 1. It may turn out that some chest has no coins, in this case the player doesn‘t take a coin from this chest. The game finishes when all chests get emptied.
Polycarpus isn‘t a greedy scrooge. Polycarpys is a lazy slob. So he wonders in what minimum number of moves the game can finish. Help Polycarpus, determine the minimum number of moves in which the game can finish. Note that Polycarpus counts not only his moves, he also counts Vasily‘s moves.
Input
The first line contains a single integer n (1 ≤ n ≤ 100) — the number of chests with coins. The second line contains a sequence of space-separated integers: a1, a2, ..., an (1 ≤ ai ≤ 1000), where ai is the number of coins in the chest number i at the beginning of the game.
Output
Print a single integer — the minimum number of moves needed to finish the game. If no sequence of turns leads to finishing the game, print -1.
Sample test(s)
Input
11
Output
-1
Input
31 2 3
Output
3
Note
In the first test case there isn‘t a single move that can be made. That‘s why the players won‘t be able to empty the chests.
In the second sample there is only one possible move x = 1. This move should be repeated at least 3 times to empty the third chest.
此题误以为很困难的数论题目,原来只是贪心而已,首先n不能小于3,也不能是偶数,因为小于3就没法选定一个i,偶数的话,没法把最大那个偶数的箱子拿走东西。
然后要从最大的开始贪心,就2i+1是最大的奇数,2i是最大的偶数,把这两个先拿光然后逐渐递减下来,这样才是最少的步骤。
注意!。当拿到只剩下1.2.3箱子的时候,拿完2.3.在1箱子可能还有剩余!!。所以还要加上1箱子中的数量。因为这个wa了一次。。
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 int a[105]; 9 int main() 10 { 11 int n,time=0,i,t; 12 scanf("%d",&n); 13 if(n<3||!(n%2)) 14 {printf("-1\n");return 0;}//n是奇数 15 for(i=1;i<=n;i++) 16 scanf("%d",&a[i]); 17 while(n>=3) 18 { 19 if(a[n]>0||a[n-1]>0) 20 { 21 t=max(a[n],a[n-1]); 22 time+=t; 23 a[(n-1)/2]-=t; 24 } 25 n=n-2; 26 } 27 if(a[1]>0) 28 time+=a[1];//这步很关键!!。a[1]有可能不为0 29 printf("%d\n",time); 30 return 0; 31 }
下面就是最最坑爹的DP了。比赛的时候提交了十来次,一直卡在第五个测试样例。。。原来是思路错误。
刚开始的时候想两个循环。。当然。效率低下。然后改成了从中间往外面找。分奇偶来判断回文。这招不错。。但是中间错误了好久。。一直以为要存在a[i]里面(该点位置有多少个回文),
后来发现。这简直是扯淡。。因为限定了区间,你不知道a[i]里面的回文是从哪里区间开始到哪里结束的。
所以思路才到了区间上。。然而没有用DP。太慢了。。
重要的是dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+is[i][j]
is[i][j]是从i到j区间是不是回文,dp[i][j]就是在这个区间里有多少回文
下附代码
1 #include<iostream> 2 #include<stdio.h> 3 #include<string.h> 4 #include<math.h> 5 #include<queue> 6 #include<algorithm> 7 using namespace std; 8 char s[5005]; 9 int a[105],dp[5005][5005]={0},is[5005][5005]={0}; 10 int main() 11 { 12 int n,t,i,j,l,r,len; 13 scanf("%s",s); 14 t=strlen(s); 15 for(i=0;i<t;i++)//中间 16 { 17 for(j=0;i+j<t&&i-j>=0;j++)//两侧 18 { 19 if(s[i+j]==s[i-j]) 20 is[i-j][i+j]=1; 21 else 22 break; 23 } 24 for(j=0;i+j+1<t&&i-j>=0;j++) 25 { 26 if(s[i+j+1]==s[i-j]) 27 is[i-j][i+j+1]=1; 28 else 29 break; 30 }//分别是奇偶的回文 31 dp[i][i]=1;//顺带赋值一个点上的dp 32 } 33 for(len=1;len<=t;len++)//身子的距离 34 for(i=0;i<t-len;i++)//头的位置 35 { 36 j=len+i;//尾巴的位置 37 dp[i][j]=dp[i+1][j]+dp[i][j-1]-dp[i+1][j-1]+is[i][j]; 38 } 39 scanf("%d",&n); 40 while(n--) 41 { 42 scanf("%d%d",&l,&r); 43 printf("%d\n",dp[l-1][r-1]); 44 } 45 return 0; 46 }
下面一题应该是整个比赛最难的了吧。。找朋友关系的图论。
G. Suggested Friends
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Polycarpus works as a programmer in a start-up social network. His boss gave his a task to develop a mechanism for determining suggested friends. Polycarpus thought much about the task and came to the folowing conclusion.
Let‘s say that all friendship relationships in a social network are given as m username pairs ai, bi (ai ≠ bi). Each pair ai, bi means that users ai and bi are friends. Friendship is symmetric, that is, if ai is friends with bi, then bi is also friends with ai. User y is a suggested friend for user x, if the following conditions are met:
- x ≠ y;
- x and y aren‘t friends;
- among all network users who meet the first two conditions, user y has most of all common friends with user x. User z is a common friend of user x and user y (z ≠ x, z ≠ y), if x and z are friends, and y and z are also friends.
Your task is to help Polycarpus to implement a mechanism for determining suggested friends.
Input
The first line contains a single integer m (1 ≤ m ≤ 5000) — the number of pairs of friends in the social network. Next m lines contain pairs of names of the users who are friends with each other. The i-th line contains two space-separated names ai and bi (ai ≠ bi). The users‘ names are non-empty and consist of at most 20 uppercase and lowercase English letters.
It is guaranteed that each pair of friends occurs only once in the input. For example, the input can‘t contain x, y and y, x at the same time. It is guaranteed that distinct users have distinct names. It is guaranteed that each social network user has at least one friend. The last thing guarantees that each username occurs at least once in the input.
Output
In the first line print a single integer n — the number of network users. In next n lines print the number of suggested friends for each user. In the i-th line print the name of the user ci and the number of his suggested friends di after a space.
You can print information about the users in any order.
Sample test(s)
Input
5Mike GeraldKate MikeKate TankGerald TankGerald David
Output
5Mike 1Gerald 1Kate 1Tank 1David 2
Input
4valera vanyavalera edikpasha valeraigor valera
Output
5valera 0vanya 3edik 3pasha 3igor 3
Note
In the first test case consider user David. Users Mike and Tank have one common friend (Gerald) with David. User Kate has no common friends with David. That‘s why David‘s suggested friends are users Mike and Tank.
无力吐槽,图论渣渣。。
有能力的时候下附代码。。
暂时到这里。