【总结:说在前面】
好久不更新博客了~终于等到暑假有空打一场CF!
这是教育场,所以题目其实都很水吧,至少我看了A~E都Too Simple啊。。。4道题随便暴力就过了。。。
不过好久不写代码,熟练度直线下滑,WA了好几次,细节还是要考虑全面的!
我想去睡觉了,所以也就先简单写写感受贴贴代码吧,有时间再补坑,就不提供翻译了。。。
感觉没啥好说的,随便xjb搞搞,小学生入门都会吧。。。
#include<iostream>
#include<cstdio>
using namespace std;
long long n,k,a,x,y,z;
int main()
{
scanf("%I64d%I64d",&n,&k);
a=n/2;
x=a/(k+1);y=x*k;z=n-x-y;
printf("%I64d %I64d %I64d\n",x,y,z);
return 0;
}
n children are standing in a circle and playing a game. Children's numbers in clockwise order form a permutation a1, a2, ..., an of length n. It is an integer sequence such that each integer from 1 to n appears exactly once in it.
The game consists of m steps. On each step the current leader with index i counts out ai people in clockwise order, starting from the next person. The last one to be pointed at by the leader becomes the new leader.
You are given numbers l1, l2, ..., lm — indices of leaders in the beginning of each step. Child with number l1 is the first leader in the game.
Write a program which will restore a possible permutation a1, a2, ..., an. If there are multiple solutions then print any of them. If there is no solution then print -1.
The first line contains two integer numbers n, m (1 ≤ n, m ≤ 100).
The second line contains m integer numbers l1, l2, ..., lm (1 ≤ li ≤ n) — indices of leaders in the beginning of each step.
Print such permutation of n numbers a1, a2, ..., an that leaders in the game will be exactly l1, l2, ..., lm if all the rules are followed. If there are multiple solutions print any of them.
If there is no permutation which satisfies all described conditions print -1.
4 5
2 3 1 4 4
3 1 2 4
3 3
3 1 2
-1
Let's follow leadership in the first example:
- Child 2 starts.
- Leadership goes from 2 to 2 + a2 = 3.
- Leadership goes from 3 to 3 + a3 = 5. As it's greater than 4, it's going in a circle to 1.
- Leadership goes from 1 to 1 + a1 = 4.
- Leadership goes from 4 to 4 + a4 = 8. Thus in circle it still remains at 4.
也是一个非常水的暴力题,不过细节要注意,有坑,我掉了两次。。。
样例具有迷惑,题目开始看错了。。。
#include<iostream>
#include<cstdio>
using namespace std;
int n,m,x,cnt;
int a[105],b[105],c[105];
int vis[105];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
if(i==1)continue;
int x=a[i-1],y=a[i];
if(y<=x)y+=n;
b[a[i-1]]=y-x;
if(vis[y-x] && vis[y-x]!=a[i-1])
{
cout<<-1;
return 0;
}
if(b[a[i-1]] && b[a[i-1]]!=y-x)
{
cout<<-1;
return 0;
}
vis[y-x]=a[i-1];
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
c[++cnt]=i;
}
}
cnt=0;
for(int i=1;i<=n;i++)
{
if(!b[i])
{
b[i]=c[++cnt];
}
if(!b[i])
{
cout<<-1;
return 0;
}
}
for(int i=1;i<=n;i++)printf("%d ",b[i]);
return 0;
}
C. Sofa Thief
Yet another round on DecoForces is coming! Grandpa Maks wanted to participate in it but someone has stolen his precious sofa! And how can one perform well with such a major loss?
Fortunately, the thief had left a note for Grandpa Maks. This note got Maks to the sofa storehouse. Still he had no idea which sofa belongs to him as they all looked the same!
The storehouse is represented as matrix n × m. Every sofa takes two neighbouring by some side cells. No cell is covered by more than one sofa. There can be empty cells.
Sofa A is standing to the left of sofa B if there exist two such cells a and b that xa < xb, a is covered by A and b is covered by B. Sofa Ais standing to the top of sofa B if there exist two such cells a and b that ya < yb, a is covered by A and b is covered by B. Right and bottom conditions are declared the same way.
Note that in all conditions A ≠ B. Also some sofa A can be both to the top of another sofa B and to the bottom of it. The same is for left and right conditions.
The note also stated that there are cntl sofas to the left of Grandpa Maks's sofa, cntr — to the right, cntt — to the top and cntb — to the bottom.
Grandpa Maks asks you to help him to identify his sofa. It is guaranteed that there is no more than one sofa of given conditions.
Output the number of Grandpa Maks's sofa. If there is no such sofa that all the conditions are met for it then output -1.
The first line contains one integer number d (1 ≤ d ≤ 105) — the number of sofas in the storehouse.
The second line contains two integer numbers n, m (1 ≤ n, m ≤ 105) — the size of the storehouse.
Next d lines contains four integer numbers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m) — coordinates of the i-th sofa. It is guaranteed that cells (x1, y1) and (x2, y2) have common side, (x1, y1) ≠ (x2, y2) and no cell is covered by more than one sofa.
The last line contains four integer numbers cntl, cntr, cntt, cntb (0 ≤ cntl, cntr, cntt, cntb ≤ d - 1).
Print the number of the sofa for which all the conditions are met. Sofas are numbered 1 through d as given in input. If there is no such sofa then print -1.
2
3 2
3 1 3 2
1 2 2 2
1 0 0 1
1
3
10 10
1 2 1 1
5 5 6 5
6 4 5 4
2 1 2 0
2
2
2 2
2 1 1 1
1 2 2 2
1 0 0 0
-1
Let's consider the second example.
- The first sofa has 0 to its left, 2 sofas to its right ((1, 1) is to the left of both (5, 5) and (5, 4)), 0 to its top and 2 to its bottom (both 2nd and 3rd sofas are below).
- The second sofa has cntl = 2, cntr = 1, cntt = 2 and cntb = 0.
- The third sofa has cntl = 2, cntr = 1, cntt = 1 and cntb = 1.
So the second one corresponds to the given conditions.
In the third example
- The first sofa has cntl = 1, cntr = 1, cntt = 0 and cntb = 1.
- The second sofa has cntl = 1, cntr = 1, cntt = 1 and cntb = 0.
And there is no sofa with the set (1, 0, 0, 0) so the answer is -1.
此题就是先记录每个沙发上下左右的沙发数量,类似前缀和思路O(N)搞定,很简单。后面就是扫一下,要注意一个沙发如果x1<x2此时在此沙发前的会多一个自己,要去掉,其他方向类似,然后就没了。。。
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int d,n,m,l,r,t,b,cnt;
int x1[100005],yy1[100005],x2[100005],y2[100005];
int lef[100005],rig[100005],top[100005],bot[100005];
int main()
{
scanf("%d%d%d",&d,&n,&m);
for(int i=1;i<=d;i++)
{
scanf("%d%d%d%d",&x1[i],&yy1[i],&x2[i],&y2[i]);
lef[min(x1[i],x2[i])]++;
rig[max(x1[i],x2[i])]++;
top[min(yy1[i],y2[i])]++;
bot[max(yy1[i],y2[i])]++;
}
scanf("%d%d%d%d",&l,&r,&t,&b);
for(int i=1;i<=n+1;i++)
{
lef[i]+=lef[i-1];
}
for(int i=1;i<=m+1;i++)
{
top[i]+=top[i-1];
}
for(int i=n+1;i>=1;i--)
{
rig[i-1]+=rig[i];
}
for(int i=m+1;i>=1;i--)
{
bot[i-1]+=bot[i];
}
for(int i=1;i<=d;i++)
{
int ll=0,rr=0,tt=0,bb=0;
ll+=lef[max(x1[i],x2[i])-1];
if(x1[i]!=x2[i])ll--;
rr+=rig[min(x1[i],x2[i])+1];
if(x1[i]!=x2[i])rr--;
tt+=top[max(yy1[i],y2[i])-1];
if(yy1[i]!=y2[i])tt--;
bb+=bot[min(yy1[i],y2[i])+1];
if(yy1[i]!=y2[i])bb--;
if(l==ll && r==rr && t==tt && b==bb)
{
printf("%d\n",i);
return 0;
}
}
printf("%d\n",-1);
return 0;
}
D. Multicolored Cars
Alice and Bob got very bored during a long car trip so they decided to play a game. From the window they can see cars of different colors running past them. Cars are going one after another.
The game rules are like this. Firstly Alice chooses some color A, then Bob chooses some color B (A ≠ B). After each car they update the number of cars of their chosen color that have run past them. Let's define this numbers after i-th car cntA(i) and cntB(i).
- If cntA(i) > cntB(i) for every i then the winner is Alice.
- If cntB(i) ≥ cntA(i) for every i then the winner is Bob.
- Otherwise it's a draw.
Bob knows all the colors of cars that they will encounter and order of their appearance. Alice have already chosen her color A and Bob now wants to choose such color B that he will win the game (draw is not a win). Help him find this color.
If there are multiple solutions, print any of them. If there is no such color then print -1.
The first line contains two integer numbers n and A (1 ≤ n ≤ 105, 1 ≤ A ≤ 106) – number of cars and the color chosen by Alice.
The second line contains n integer numbers c1, c2, ..., cn (1 ≤ ci ≤ 106) — colors of the cars that Alice and Bob will encounter in the order of their appearance.
Output such color B (1 ≤ B ≤ 106) that if Bob chooses it then he will win the game. If there are multiple solutions, print any of them. If there is no such color then print -1.
It is guaranteed that if there exists any solution then there exists solution with (1 ≤ B ≤ 106).
4 1
2 1 4 2
2
5 2
2 2 4 5 3
-1
3 10
1 2 3
4
Let's consider availability of colors in the first example:
- cnt2(i) ≥ cnt1(i) for every i, and color 2 can be the answer.
- cnt4(2) < cnt1(2), so color 4 isn't the winning one for Bob.
- All the other colors also have cntj(2) < cnt1(2), thus they are not available.
In the third example every color is acceptable except for 10.
这题就是纯粹暴力带比较解决,还没有C难,和B差不多难度,没什么坑,自己手滑写炸了一次555。。。
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;
int n,m,x;
int cnt[1000005];
bool vis[1000005];
queue<int>q,qt;
bool f,turn;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x!=m)
{
cnt[x]++;
if(!f && !vis[x])q.push(x);
if(!vis[x])vis[x]=1;
}
else
{
f=1;
cnt[m]++;
if(!turn)
{
while(!q.empty())
{
int v=q.front();
q.pop();
if(cnt[v]>=cnt[m])qt.push(v);
}
if(qt.empty())
{
cout<<-1<<endl;
return 0;
}
turn=1;
}
else
{
while(!qt.empty())
{
int v=qt.front();
qt.pop();
if(cnt[v]>=cnt[m])q.push(v);
}
if(q.empty())
{
cout<<-1<<endl;
return 0;
}
turn=0;
}
}
}
if(!q.empty())printf("%d\n",q.front()); else printf("%d\n",qt.front());
return 0;
}
E. Card Game Again
E题算是唯一有技术含量的题,是双指针+数学乘法原理,1A!注意要long long啊!
对于每次 : ans = ans+ (n - end) * (start - previousStart)
双指针维护start , end 即可.也不算难.
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
using namespace std;
int n,k,lef,rig,last,i;
int a[100005];
long long ans=0;
int main()
{
scanf("%d%d",&n,&k);
lef=1,rig=1,last=0;
for(i=1;i<=n;i++)scanf("%d",&a[i]);
while(rig<=n)
{
long long p=1;
for(i=lef;i<=n;i++)
{
p*=a[i];
p%=(long long)k;
if(p==0)break;
}
rig=i;
p=1;
for(i=rig;i>=1;i--)
{
p*=a[i];
p%=(long long)k;
if(p==0)break;
}
lef=i;
ans+=(long long)(lef-last)*(long long)(n-rig+1);
last=lef;
lef++;
rig++;
}
printf("%I64d\n",ans);
return 0;
}
睡觉去了~~~~~~~~~~