A - Okabe and Future Gadget Laboratory
Codeforces Round #420 (Div. 2)
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int main()
{
int n;
scanf("%d",&n);
ll a[55][55];
int flag=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%lld",&a[i][j]);
}
}
int x,y;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i][j]==1)
continue;
for(x=0;x<n;x++)
{
for(y=0;y<n;y++)
{
if(a[i][j]==a[x][j]+a[i][y])
break;
}
if(y!=n)
break;
}
if(x==n)
flag=0;
}
}
if(flag)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
return 0;
}
B - Okabe and Banana Trees
Codeforces Round #420 (Div. 2)
这个题我之前做过,它就是用一个等差数列来做,
首先,因为它一定是过坐标 (X,Y) 的,且(x与y都是整数)
大家看式子,式中是 y=-x/m+b; 只要我确保y是整数,x肯定是整数。枚举y的所有情况。
问题又来了,我一开始说是用等差数列,至于怎么用呢。
其实就是把y所在的枚举同时,内设一个循环来计算,
该循环是用 X代表在一条线上的所有x1+x2+x3....xn的值
而用 j×(x+1),j代表的是y值,(x+1)是个数。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int m,b;
scanf("%d%d",&m,&b);
ll maxz=0;
for(int y=0;y<=b;y++)
{
ll x=(b-y)*m;
ll X=((x)*(x+1))/2;
ll sum=0;
for(int j=0;j<=y;j++)
{
sum+=j*(x+1)+X;
}
maxz=max(sum,maxz);
}
printf("%lld\n",maxz);
return 0;
}
C - Okabe and Boxes
Codeforces Round #420 (Div. 2)
#include<stdio.h>
#include<stack>
using namespace std;
stack<int>st;
char op[10];
int main()
{
int n,x,ans,cur;
scanf("%d",&n);
// getchar();
cur=0;
ans=0;
for(int i=0;i<2*n;i++)
{
scanf("%s",op);
if(op[0]=='a')
{
scanf("%d",&x);
st.push(x);
}
else
{
cur++;
if(!st.empty()&&st.top()==cur)
{
st.pop();
}
else if(!st.empty())
{
ans++;
while(!st.empty())
st.pop();
}
}
}
printf("%d\n",ans);
return 0;
}
D - Before an Exam
Tomorrow Peter has a Biology exam. He does not like this subject much, but d days ago he learnt that he would have to take this exam. Peter's strict parents made him prepare for the exam immediately, for this purpose he has to study not less than minTimei and not more than maxTimei hours per each i-th day. Moreover, they warned Peter that a day before the exam they would check how he has followed their instructions.
So, today is the day when Peter's parents ask him to show the timetable of his preparatory studies. But the boy has counted only the sum of hours sumTime spent him on preparation, and now he wants to know if he can show his parents a timetable sсhedule with d numbers, where each number sсhedulei stands for the time in hours spent by Peter each i-th day on biology studies, and satisfying the limitations imposed by his parents, and at the same time the sum total of all schedulei should equal to sumTime.
The first input line contains two integer numbers d, sumTime (1 ≤ d ≤ 30, 0 ≤ sumTime ≤ 240) — the amount of days, during which Peter studied, and the total amount of hours, spent on preparation. Each of the following dlines contains two integer numbers minTimei, maxTimei (0 ≤ minTimei ≤ maxTimei ≤ 8), separated by a space — minimum and maximum amount of hours that Peter could spent in the i-th day.
In the first line print YES, and in the second line print d numbers (separated by a space), each of the numbers — amount of hours, spent by Peter on preparation in the corresponding day, if he followed his parents' instructions; or print NO in the unique line. If there are many solutions, print any of them.
题意很简单就是说,有个同学,它不喜欢学生物,导致要爸妈监督下学习,爸妈每天安排了规定的时间内复习,
但是同学只记得自己曾经复习的总时间,但是不知道每天复习的时间,要你去给出每天的时间。
还有一个不合法性,要是最少的总和比它大不行,要是最大的总和比它小也不行。
这题我的做法是先把全部时间设置为最大值,然后在把差值给一个一个减去,才得到最小值。
#include<bits/stdc++.h>
using namespace std;
typedef struct point
{
int x,y,index,output,diff;
} point;
int cmp1(point a, point b)
{
return a.diff>b.diff;
}
int cmp2(point a,point b)
{
return a.index<b.index;
}
int main()
{
int d,sumtime;
while(~scanf("%d%d",&d,&sumtime))
{
int flag=1;
point a[35];
int sum1=0,sum2=0;
for(int i=0; i<d; i++)
{
scanf("%d%d",&a[i].x,&a[i].y);
a[i].index=i;
a[i].diff=a[i].y-a[i].x;
sum1+=a[i].x;
sum2+=a[i].y;
}
if(sum2<sumtime||sum1>sumtime)
{
printf("NO\n");
flag=0;
}
else
{
printf("YES\n");
}
if(flag)
{
sum2-=sumtime;
sort(a,a+d,cmp1);
for(int i=0; i<d; i++)
{
if(sum2-a[i].diff>0)
{
sum2-=a[i].diff;
a[i].output=a[i].x;
}
else if(sum2-a[i].diff<=0)
{
a[i].output=a[i].y-sum2;
sum2=0;
}
else if(sum2==0)
{
a[i].output=a[i].y;
}
}
sort(a,a+d,cmp2);
for(int i=0; i<d; i++)
{
printf(i==0?"%d":" %d",a[i].output);
}
printf("\n");
}
}
return 0;
}
这题其实就是用户名重复就在其后加数字。
利用的map容器的做法进行标记。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
map<int,string>my;
map<string,int>vis;
int b[100005];
char a[100005][33];
scanf("%d",&n);
getchar();
queue<string>q;
for(int i=0;i<n;i++)
{
string a;
cin>>a;
q.push(a);
vis[a]=0;
my[i]=a;
}
int cnt=0;
while(!q.empty())
{
string s;
s=q.front();
q.pop();
if(vis[s]==0)
{
printf("OK\n");
vis[s]++;
}
else
{
vis[s]++;
cout<<s;
printf("%d\n",vis[s]-1);
}
cnt++;
}
return 0;
}
F - Robot Motion
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int dir[4][2]={
{-1,0}, //N
{0,1}, //E
{1,0}, //S
{0,-1} //W
};
int n,m,s;
int vis[1000][1000];
char G[1000][1000];
int main()
{
while(scanf("%d%d%d",&n,&m,&s),(n||m||s))
{
getchar();
memset(vis,-1,sizeof(vis));
memset(G,0,sizeof(G));
for(int i=0;i<n;i++)
{
scanf("%s",G[i]);
getchar();
}
int flag=1;
int x=0,y=s-1;
int step=0;
while(flag)
{
if(x<0||y<0||x>=n||y>=m)
{
printf("%d step(s) to exit\n",step);
flag=0;
break;
}
if(vis[x][y]!=-1)
{
printf("%d step(s) before a loop of %d step(s)\n",vis[x][y],step-vis[x][y]);
flag=0;
break;
}
vis[x][y]=step;
step++;
switch(G[x][y])
{
case 'N':x+=dir[0][0], y+=dir[0][1];break;
case 'E':x+=dir[1][0], y+=dir[1][1];break;
case 'S':x+=dir[2][0], y+=dir[2][1];break;
case 'W':x+=dir[3][0], y+=dir[3][1];break;
}
}
}
return 0;
}