今天水了一次题,他们真的好厉害啊,做的好快,而且都会的样子。
通过这次做题,推测以后的题型大概是这样的,一道DP,一道广搜/深搜,一道图论,一道贪心,一道最短路径,可能还会存在二叉树的问题等,另外就是一道模拟与一道签到题。
自身还存在很多不足,拉下了很多东西,接下来根据题型慢慢补一补。
说一说今天的题目:
POJ-2387
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.
Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1…N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.
Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input
-
Line 1: Two integers: T and N
-
Lines 2…T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1…100.
Output -
Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
最短路径问题:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
#define MAX 9999999
int u , v ,n, dis[1111],vis[1111],ma[1111][1111];
using namespace std ;
void dijk()
{
int k , mini;
for(int i = 1 ; i <=v;i++)
{
dis[i]=ma[1][i];
}
for(int i = 1 ;i<=v;i++)
{
mini=MAX;
for(int j = 1 ; j<=v;j++)
{
if(!vis[j]&&dis[j]<mini)
{
mini=dis[j];
k=j;
}
}
vis[k]=1;
for(int j=1 ;j<=v;j++)
{
if(dis[j]>dis[k]+ma[k][j])
{
dis[j]=dis[k]+ma[k][j];
}
}
}
}
int main()
{
while(cin>>u>>v)
{
n=0;
for(int i = 0 ; i <=v;i++)
{
for(int j = 0 ; j <=v;j++)
{
ma[i][j]=MAX;
}
ma[i][i]=0;
vis[i]=0;
dis[i]=MAX;
}
for(int i = 1 ;i<=u;i++)
{
int a , b , len;
cin>>a>>b>>len;
n=max(max(n,a),b);
if(ma[a][b]>len)
{
ma[a][b]=ma[b][a]=len;
}
}
dijk();
printf("%d\n",dis[v]);
}
return 0 ;
}
POJ-2388
FJ is surveying his herd to find the most average cow. He wants to know how much milk this ‘median’ cow gives: half of the cows give as much or more than the median; half give as much or less.
Given an odd number of cows N (1 <= N < 10,000) and their milk output (1…1,000,000), find the median amount of milk given such that at least half the cows give the same amount of milk or more and at least half give the same or less.
Input
-
Line 1: A single integer N
-
Lines 2…N+1: Each line contains a single integer that is the milk output of one cow.
Output -
Line 1: A single integer that is the median milk output.
Sample Input
5
2
4
1
3
5
Sample Output
3
Hint
INPUT DETAILS:
Five cows with milk outputs of 1…5
OUTPUT DETAILS:
1 and 2 are below 3; 4 and 5 are above 3.
这是一道水题。。。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
int a[10001];
using namespace std;
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
sort(a+1,a+n+1);
cout<<a[(n+1)/2];
return 0;
}
POJ2390
Farmer John made a profit last year! He would like to invest it well but wonders how much money he will make. He knows the interest rate R (an integer between 0 and 20) that is compounded annually at his bank. He has an integer amount of money M in the range 100…1,000,000. He knows how many years Y (range: 0…400) he intends to invest the money in the bank. Help him learn how much money he will have in the future by compounding the interest for each year he saves. Print an integer answer without rounding. Answers for the test data are guaranteed to fit into a signed 32 bit integer.
Input
- Line 1: Three space-separated integers: R, M, and Y
Output - Line 1: A single integer that is the number of dollars FJ will have after Y years.
Sample Input
5 5000 4
Sample Output
6077
Hint
INPUT DETAILS:
5% annual interest, 5000 money, 4 years
OUTPUT DETAILS:
Year 1: 1.05 * 5000 = 5250
Year 2: 1.05 * 5250 = 5512.5
Year 3: 1.05 * 5512.50 = 5788.125
Year 4: 1.05 * 5788.125 = 6077.53125
The integer part of 6077.53125 is 6077.
一道小的模拟,也是水题吧。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
int main()
{
int R,M,Y;
double sum=0;
cin>>R>>M>>Y;
double r;
r=1+(R*1.0/100);
if(Y==0)
{
cout<<M<<endl;
return 0;
}
sum=r*M;
for(int i=1;i<=Y-1;i++)
{
sum=r*sum;
}
cout<<int(sum)<<endl;
return 0;
}
CodeForces - 140D
As Gerald sets the table, Alexander sends the greeting cards, and Sergey and his twins create an army of clone snowmen, Gennady writes a New Year contest.
The New Year contest begins at 18:00 (6.00 P.M.) on December 31 and ends at 6:00 (6.00 A.M.) on January 1. There are n problems for the contest. The penalty time for each solved problem is set as the distance from the moment of solution submission to the New Year in minutes. For example, the problem submitted at 21:00 (9.00 P.M.) gets penalty time 180, as well as the problem submitted at 3:00 (3.00 A.M.). The total penalty time is calculated as the sum of penalty time for all solved problems. It is allowed to submit a problem exactly at the end of the contest, at 6:00 (6.00 A.M.).
Gennady opened the problems exactly at 18:00 (6.00 P.M.) and managed to estimate their complexity during the first 10 minutes of the contest. He believes that writing a solution for the i-th problem will take ai minutes. Gennady can submit a solution for evaluation at any time after he completes writing it. Probably he will have to distract from writing some solution to send the solutions of other problems for evaluation. The time needed to send the solutions can be neglected, i.e. this time can be considered to equal zero. Gennady can simultaneously submit multiple solutions. Besides, he can move at any time from writing one problem to another, and then return to the first problem from the very same place, where he has left it. Thus the total solution writing time of the i-th problem always equals ai minutes. Of course, Gennady does not commit wrong attempts, and his solutions are always correct and are accepted from the first attempt. He can begin to write the solutions starting from 18:10 (6.10 P.M.).
Help Gennady choose from the strategies that help him solve the maximum possible number of problems, the one with which his total penalty time will be minimum.
Input
The first line contains an integer n (1 ≤ n ≤ 100) — the number of the problems. The next line contains n space-separated integers ai (1 ≤ ai ≤ 720) — each number shows how much time in minutes Gennady will spend writing a solution to the problem.
Output
Print two integers — the number of problems Gennady will solve and the total penalty time considering that he chooses the optimal strategy.
Examples
Input
3
30 330 720
Output
2 10
Note
In the sample, one of Gennady’s possible optimal strategies is as follows. At 18:10 (6:10 PM) he begins to write the first problem and solves it in 30 minutes (18:40 or 6.40 P.M.). At 18:40 (6.40 P.M.) he begins to write the second problem. There are 320 minutes left before the New Year, so Gennady does not have the time to finish writing the second problem before the New Year. At 0:00 (12.00 A.M.) he distracts from the second problem, submits the first one, and returns immediately to writing the second problem. At 0:10 (0.10 A.M.), he completes the solution for the second problem, submits it and gets 10 minute penalty time. Note that as the total duration of the contest is 720 minutes and Gennady has already spent 10 minutes on reading the problems, he will not have time to solve the third problem during the contest. Yes, such problems happen to exist.
简单贪心题:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<set>
#include<map>
#include<vector>
using namespace std;
int main()
{
int R,M,Y;
double sum=0;
cin>>R>>M>>Y;
double r;
r=1+(R*1.0/100);
if(Y==0)
{
cout<<M<<endl;
return 0;
}
sum=r*M;
for(int i=1;i<=Y-1;i++)
{
sum=r*sum;
}
cout<<int(sum)<<endl;
return 0;
}