您的位置:首页 > 其它

Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined)

2016-10-09 12:03 274 查看
A. Checking the Calendar

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

You are given names of two days of the week.

Please, determine whether it is possible that during some
non-leap year the first day of some month was equal to the first day of the week you are given, while the first day of
the next month was equal to the second day of the week you are given.
Both months should belong to one year.

In this problem, we consider the Gregorian calendar to be used. The number of months in this calendar is equal to 12. The number of days in months during any non-leap year is:
31, 28,
31, 30, 31,
30, 31, 31,
30, 31, 30,
31.

Names of the days of the week are given with lowercase English letters: "monday", "tuesday", "wednesday", "thursday",
"friday", "saturday", "sunday".

Input
The input consists of two lines, each of them containing the name of exactly one day of the week. It's guaranteed that each string in the input is from the set "monday", "tuesday",
"wednesday", "thursday", "friday", "saturday", "sunday".

Output
Print "YES" (without quotes) if such situation is possible during some non-leap year. Otherwise, print "NO" (without quotes).

Examples

Input
monday
tuesday


Output
NO


Input
sunday
sunday


Output
YES


Input
saturday
tuesday


Output
YES


Note
In the second sample, one can consider February 1 and March 1 of year 2015. Both these days were Sundays.

In the third sample, one can consider July 1 and August 1 of year 2017. First of these two days is Saturday, while the second one is Tuesday.

题意:假设没有瑞年给出你上个月1号是星期几,问这个月一号可不可能是给出的。

题解:水题,姿势优美即可。

代码:

#include<bits/stdc++.h>
using namespace std;
map<string ,int >mp;
int main()
{
string str1,str2;
mp["monday"]=1;
mp["tuesday"]=2;
mp["wednesday"]=3;
mp["thursday"]=4;
mp["friday"]=5;
mp["saturday"]=6;
mp["sunday"]=7;
cin>>str1>>str2;
if(str1==str2||mp[str1]-mp[str2]==4||mp[str1]-mp[str2]==5||mp[str1]-mp[str2]==-2||mp[str1]-mp[str2]==-3)
{
cout<<"YES"<<endl;
}
else
cout<<"NO"<<endl;
}


B. Batch Sort

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

You are given a table consisting of n rows and
m columns.

Numbers in each row form a permutation of integers from
1 to m.

You are allowed to pick two elements in one row and swap them, but
no more than once for each row. Also, no more than once you are allowed to pick two columns and swap them. Thus, you are allowed to perform from
0 to n + 1 actions in total.
Operations can be performed in any order.

You have to check whether it's possible to obtain the identity permutation
1, 2, ..., m in each row. In other words, check if one can perform some of the operation following the given rules and make each row sorted in increasing order.

Input
The first line of the input contains two integers n and
m (1 ≤ n, m ≤ 20) — the number of rows and the number of columns in the given table.

Each of next n lines contains
m integers — elements of the table. It's guaranteed that numbers in each line form a permutation of integers from
1 to m.

Output
If there is a way to obtain the identity permutation in each row by following the given rules, print "YES" (without quotes) in the only line of the output. Otherwise, print "NO"
(without quotes).

Examples

Input
2 4
1 3 2 4
1 3 4 2


Output
YES


Input
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3


Output
NO


Input
3 6
2 1 3 4 5 6
1 2 4 3 5 6
1 2 3 4 6 5


Output
YES


Note
In the first sample, one can act in the following way:

Swap second and third columns. Now the table is
1 2 3 4
1 4 3 2

In the second row, swap the second and the fourth elements. Now the table is
1 2 3 4
1 2 3 4

题意:给你一个n*m的矩阵,每行最多可以交换两个元素,最多可以交换任意两列,问最后能否形成一个每行都单调递增的矩阵

题解:枚举任意两列,交换,记录每行需要交换的元素的个数,可以为0,2,判断是否可行。交换回去。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int a[50][50];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
int ok=0;
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
int buxing2=0;
for(int k=0;k<n;k++)

eb24
{
swap(a[k][i],a[k][j]);
}
for(int ii=0;ii<n;ii++)
{
int flag=0;
for(int jj=0;jj<m;jj++)
{
if(a[ii][jj]!=jj+1)flag++;
}
if(flag==0||flag==2)continue;
else buxing2=1;
}
for(int k=0;k<n;k++)
{
swap(a[k][i],a[k][j]);
}
if(buxing2==1)continue;
else ok=1;
}
}
if(ok==1)printf("YES\n");
else printf("NO\n");
}
}

C. Ray Tracing

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

There are k sensors located in the rectangular room of size
n × m meters. The
i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle.

Opposite corners of the room are located at points (0, 0) and
(n, m). Walls of the room are parallel to coordinate axes.

At the moment 0, from the point
(0, 0) the laser ray is released in the direction of point
(1, 1). The ray travels with a speed of

meters per second. Thus, the
ray will reach the point (1, 1) in exactly one second after the start.

When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.

For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print
 - 1 for such sensors.

Input
The first line of the input contains three integers n,
m and k (2 ≤ n, m ≤ 100 000,
1 ≤ k ≤ 100 000) — lengths of the room's walls and the number of sensors.

Each of the following k lines contains two integers
xi and
yi (1 ≤ xi ≤ n - 1,
1 ≤ yi ≤ m - 1) — coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.

Output
Print k integers. The
i-th of them should be equal to the number of seconds when the ray first passes through the point where the
i-th sensor is located, or
 - 1 if this will never happen.

Examples

Input
3 3 4
1 1
1 2
2 1
2 2


Output
1
-1
-1
2


Input
3 4 6
1 1
2 1
1 2
2 2
1 3
2 3


Output
1
-1
-1
25
-1


Input
7 4 5
1 3
2 2
5 1
5 3
4 3


Output
13
2
9
5
-1


Note
In the first sample, the ray will consequently pass through the points
(0, 0), (1, 1), (2, 2),
(3, 3). Thus, it will stop at the point
(3, 3) after 3 seconds.

题意:一束光从0.0点进入,速度每秒sqrt(2),遇到墙壁会进行反射,但如果走到四个角会射出。给你一个n*m(右上角),k次提问,没有经过输出-1

题解:

大神们都会一种叫”拓展欧几里得“。。还给出了一道非常相近的题目HDU5114,然而我不会。。。 我就写了一个dfs模拟路径,然后卡在了1e5*1e5上了,有路过的大神可以帮看看都没有能优化的地方了。。

代码(不能AC):

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <bits/stdc++.h>
using namespace std;
map<pair<int,int>,int> vis;
int n,m,k,x,y;
void dfs(int x,int y,int t,int flag)
{
int fx,fy;
if(vis[make_pair(x,y)]==0)
{
vis[make_pair(x,y)]=t;
}
if((x==0&&y==0)||(x==n&&y==0)||(x==0&&y==m)||(x==n&&y==m))
return ;
if(flag==1)
{
fx=x+1,fy=y+1;
}
if(flag==2)
{
fx=x+1,fy=y-1;
}
if(flag==3)
{
fx=x-1,fy=y-1;
}
if(flag==4)
{
fx=x-1,fy=y+1;
}
if(fy>m)
{
if(flag==1)
dfs(++x,--y,++t,2);
else if(flag==4)
dfs(--x,--y,++t,3);
}
else if(fx>n)
{
if(flag==2)
dfs(--x,--y,++t,3);
else if(flag==1)
dfs(--x,++y,++t,4);
}
else if(fy<0)
{
if(flag==3)
dfs(--x,++y,++t,4);
else if(flag==2)
dfs(++x,++y,++t,1);
}
else if(fx<0)
{
if(flag==4)
dfs(++x,++y,++t,1);
else if(flag==3)
dfs(++x,--y,++t,2);
}
else
dfs(fx,fy,++t,flag);
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
vis.clear();
dfs(1,1,1,1);
while(k--)
{
scanf("%d%d",&x,&y);
if(vis[make_pair(x,y)]==0)
printf("-1\n");
else
printf("%d\n",vis[make_pair(x,y)]);

}
}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐