A:Gena’s Code
Problem Description
It’s the year 4527 and the tanks game that we all know and love still exists. There also exists Great Gena’s code, written in 2016. The problem this code solves is: given the number of tanks that go into the battle from each country, find their product. If it is turns to be too large, then the servers might have not enough time to assign tanks into teams and the whole game will collapse!
There are exactly n distinct countries in the world and the i-th country added ai tanks to the game. As the developers of the game are perfectionists, the number of tanks from each country is beautiful. A beautiful number, according to the developers, is such number that its decimal representation consists only of digits ‘1’ and ‘0’, moreover it contains at most one digit ‘1’. However, due to complaints from players, some number of tanks of one country was removed from the game, hence the number of tanks of this country may not remain beautiful.
Your task is to write the program that solves exactly the same problem in order to verify Gena’s code correctness. Just in case.
Input
The first line of the input contains the number of countries n (1 ≤ n ≤ 100 000). The second line contains n non-negative integers ai without leading zeroes — the number of tanks of the i-th country.
It is guaranteed that the second line contains at least n - 1 beautiful numbers and the total length of all these number’s representations doesn’t exceed 100 000.
Output
Print a single number without leading zeroes — the product of the number of tanks presented by each country.
Examples
input
3
5 10 1
output
50
input
4
1 1 10 11
output
110
input
5
0 3 1 100 1
output
0
Note
In sample 1 numbers 10 and 1 are beautiful, number 5 is not not.
In sample 2 number 11 is not beautiful (contains two ‘1’s), all others are beautiful.
In sample 3 number 3 is not beautiful, all others are beautiful.
代码
#include <iostream>
#include <stack>
#include <queue>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
const int MAXN=100010;
char s[MAXN],perfect[MAXN];
bool Bea()
{
if(strlen(s)==1)
return true;
for(int i=1;i<strlen(s);i++)
if(s[i]!='0')
return false;
return true;
}
int main()
{
int n,zero=0,isZero=0;
scanf("%d",&n);
strcpy(perfect,"1");
while(n--)
{
scanf("%s",s);
if(s[0]=='0')
{
isZero=1;
break;
}
else if(s[0]=='1'&&Bea())
{
zero+=strlen(s)-1;
continue;
}
else
strcpy(perfect,s);
}
if(isZero)
printf("0");
else
{
printf("%s",perfect);
for(int i=0;i<zero;i++)
printf("0");
}
printf("\n");
return 0;
}
总结
这道题不算难,因为保证一串数字中至多只有一个not beautiful的数据,所以就是直接找出来这个数据,然后求出所有beautiful 数据带有的 0 的个数就可以了。很高兴自己碰到这个题目没有一上来就直接拿数据相乘,不然又是爆数据。这里使用了字符串来找寻not beautiful数据,并且通过字符串长度来获取beautiful数据的0的个数。
B:FatMouse’ Trade
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean.
The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1’s. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
20 3
25 18
24 15
15 10
-1 -1
Sample Output
13.333
31.500
代码
#include<iostream>
#include<stack>
#include<cstring>
#include<vector>
#include<cstdio>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct shiwu
{
int j,f;
}s[1010];
bool cmp(shiwu a,shiwu b)
{
return a.j*b.f>b.j*a.f;
}
int main ()
{
int n,m;
while (scanf("%d%d",&m,&n)!=EOF)
{
if(m==-1&&n==-1)
return 0;
memset(s,0,sizeof(s));
for(int i=0;i<n;i++)
{
scanf ("%d%d",&s[i].j,&s[i].f);
}
sort(s,s+n,cmp);
double Count=0;
for (int i=0;i<n&&m>0;i++)
{
if(m>=s[i].f)
{
Count+=s[i].j;
m-=s[i].f;
continue;
}
else
{
Count+=(1.0*m/s[i].f)*(1.0*s[i].j);
break;
}
}
printf ("%.3lf\n",Count);
}
return 0;
}
总结
这是一道贪心算法的题目,运用结构体排序,然后依次换取就好了。这道题WA了2次,第一次发现没有return 0;
,最后再补上了if(m==-1&&n==-1)return 0;
本以为加了EOF就可以,以后碰到特判还是得特判一下。
D:Meteor Shower
Description
Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroyed by a meteor) . She is currently grazing at the origin in the coordinate plane and wants to move to a new, safer location while avoiding being destroyed by meteors along her way.
The reports say that M meteors (1 ≤ M ≤ 50,000) will strike, with meteor i will striking point (Xi, Yi) (0 ≤ Xi ≤ 300; 0 ≤ Yi ≤ 300) at time Ti (0 ≤ Ti ≤ 1,000). Each meteor destroys the point that it strikes and also the four rectilinearly adjacent lattice points.
Bessie leaves the origin at time 0 and can travel in the first quadrant and parallel to the axes at the rate of one distance unit per second to any of the (often 4) adjacent rectilinear points that are not yet destroyed by a meteor. She cannot be located on a point at any time greater than or equal to the time it is destroyed).
Determine the minimum time it takes Bessie to get to a safe place.
Input
- Line 1: A single integer: M
- Lines 2..M+1: Line i+1 contains three space-separated integers: Xi, Yi, and Ti
Output
- Line 1: The minimum time it takes Bessie to get to a safe place or -1 if it is impossible.
Sample Input
4
0 0 2
2 1 2
1 1 2
0 3 5
Sample Output
5
代码
#include <iostream>
#include <stack>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdio>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
#define Max 310
int map[Max][Max];
int flag[Max][Max];
int dir[4][2]={{-1,0},{0,-1},{0,1},{1,0}};
int M;
struct point
{
int x,y,t;
}s[50010];
bool cmp(point a,point b)
{
return a.t<b.t;
}
int bfs()
{
queue <point> Q;
while (!Q.empty())
Q.pop();
point p;
p.x=0;
p.y=0;
p.t=0;
Q.push(p);
flag[p.x][p.y]=1;
while (!Q.empty())
{
point now=Q.front();
Q.pop();
if(now.t>=map[now.x][now.y])
continue;
for (int i=0;i<4;i++)
{
int nx=now.x+dir[i][0];
int ny=now.y+dir[i][1];
int t=now.t+1;
if(nx>=0&&ny>=0&&!flag[nx][ny]&&map[nx][ny]==-1)
return t;
else if(nx>=0&&ny>=0&&!flag[nx][ny]&&t<map[nx][ny])
{
point next;
next.x=nx;
next.y=ny;
next.t=t;
Q.push(next);
flag[nx][ny]=1;
}
}
}
return -1;
}
int main ()
{
scanf ("%d",&M);
memset (map,-1,sizeof(map));
memset (flag,0,sizeof (flag));
for (int i=0;i<M;i++)
{
scanf ("%d%d%d",&s[i].x,&s[i].y,&s[i].t);
}
sort (s,s+M,cmp);
for (int i=0;i<M;i++)
{
if(map[s[i].x][s[i].y]==-1)
map[s[i].x][s[i].y]=s[i].t;
for (int j=0;j<4;j++)
{
int nx=s[i].x+dir[j][0];
int ny=s[i].y+dir[j][1];
if(nx>=0&&ny>=0&&map[nx][ny]==-1)
map[nx][ny]=s[i].t;
}
}
int ans=bfs();
printf ("%d\n",ans);
return 0;
}
总结
楞逼了这么多天终于用bfs做出来题目了,使用一个map二维数组来存储流星降下位置的时间,再用一个flag二维数组存储是否经过该地址,然后队列搜索。好吧,这道题目其实是比赛结束后,思考了一个中午才提交的……
F:Mike and Cellphone
Description
While swimming at the beach, Mike has accidentally dropped his cellphone into the water. There was no worry as he bought a cheap replacement phone with an old-fashioned keyboard. The keyboard has only ten digital equal-sized keys, located in the following way:
Together with his old phone, he lost all his contacts and now he can only remember the way his fingers moved when he put some number in. One can formally consider finger movements as a sequence of vectors connecting centers of keys pressed consecutively to put in a number. For example, the finger movements for number “586” are the same as finger movements for number “253”:
Mike has already put in a number by his “finger memory” and started calling it, so he is now worrying, can he be sure that he is calling the correct number? In other words, is there any other number, that has the same finger movements?
Input
The first line of the input contains the only integer n (1 ≤ n ≤ 9) — the number of digits in the phone number that Mike put in.
The second line contains the string consisting of n digits (characters from ‘0’ to ‘9’) representing the number that Mike put in.
Output
If there is no other phone number with the same finger movements and Mike can be sure he is calling the correct number, print “YES” (without quotes) in the only line.
Otherwise print “NO” (without quotes) in the first line.
Sample Input
Input
3
586Output
NO
Input
2
09Output
NO
Input
9
123456789Output
YES
Input
3
911Output
YES
代码
#include<iostream>
#include<stack>
#include<cstring>
#include<vector>
#include<cstdio>
#include <cmath>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
struct node
{
int x,y;
}a[9];
int main()
{
node a[]={3,1,0,0,0,1,0,2,1,0,1,1,1,2,2,0,2,1,2,2};
int d[4][3]={1,2,3,4,5,6,7,8,9,-1,0,-1};
int n;
int b[10][2];
char c[10];
scanf("%d",&n);
int k=0;
scanf("%s",c);
for(int i=1;i<n;i++)
{
b[k][0]=a[c[i]-'0'].x-a[c[i-1]-'0'].x;
b[k][1]=a[c[i]-'0'].y-a[c[i-1]-'0'].y;
k++;
}
int i,j,t=0;
for(i=0;i<=9;i++)
{
int s=i;
for(j=0;j<k;j++)
{
int nx=a[s].x+b[j][0];
int ny=a[s].y+b[j][1];
if(nx>=0&&nx<4&&ny>=0&&ny<3&&d[nx][ny]>=0)
s=d[nx][ny];
else
break;
}
if(j>=k)
t++;
}
if(t>1)
printf("NO\n");
else
printf("YES\n");
return 0;
}
总结
这道题其实也不算很难,开个数组来存每次移动的方向,然后搜索一下按照这种移动方法能否实现超过一次的移动。然后整理了一下了大神的思路,发现一种更简洁的代码。
#include <iostream>
#include <cstdio>
using namespace std;
const int maxn = 9 + 5;
int a[maxn];
int b[maxn];
char s[maxn];
int main()
{
int n;
scanf("%d", &n) ;
scanf("%s", s);
int l = 0, u = 0, r = 0, d = 0;
for(int i = 0; i < n; ++i)
{
if(s[i] == '0')
l = r = d = 1;
if(s[i] == '1' || s[i] == '4' || s[i] == '7')
l = 1;
if(s[i] == '3' || s[i] == '6' || s[i] == '9')
r = 1;
if(s[i] == '1' || s[i] == '2' || s[i] == '3')
u = 1;
if(s[i] == '7' || s[i] == '9')
d = 1;
}
if(u && d && l && r)
puts("YES\n");
else
puts("NO\n");
return 0;
}