这次考试共五个题。
1.三角塔:
本题不难,通过等差数列即可求出每层的个数。
解决时间:15分。
2.修改数字:
通过统计每个数字出现次数,优先将出现次数少的数字修改,即可得出答案。
代码:
#include<bits/stdc++.h>
using namespace std;
int n,k;
int a[200010],vis[200010];
int ans;
int main(){
ios::sync_with_stdio(false);
//输入及记录数据
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
vis[a[i]]+=1;
}
//排序
sort(vis,vis+200000,greater<int>());
//统计
while(vis[k]!=0){
ans+=vis[k];
k+=1;
}
cout<<ans;
return 0;
}
解决时间:30分
3:移动骑士。
一道有手就行 很简单的BFS,八个方向,管你会不会,搜就完了~~
如果你不会就看一下我的代码:
#include<bits/stdc++.h>
using namespace std;
int n,x1,y1,x2,y2,vis[1000][1000];
int dx[8]={1,2,2,1,-1,-2,-2,-1};
int dy[8]={2,1,-1,-2,2,1,-1,-2};
struct node{
int x_num;
int y_num;
int step;
node(int x_num_,int y_num_,int step_){
x_num=x_num_;
y_num=y_num_;
step=step_;
}
};
queue<node> s;
int f(int sx,int sy){
node a = node(sx,sy,0);
s.push(a);
vis[sx][sy]=1;
while(!s.empty()){
node now=s.front();
s.pop();
for(int i=0;i<8;i++){
int nx=now.x_num+dx[i];
int ny=now.y_num+dy[i];
if(nx==x2&&ny==y2){
return now.step+1;
}
if(nx>=0&&nx<n){
if(ny>=0&&ny<n){
if(vis[nx][ny]==0){
vis[nx][ny]=1;
s.push(node(nx,ny,now.step+1));
}
}
}
}
}
}
int main(){
cin>>n>>x1>>y1>>x2>>y2;
cout<<f(x1,y1);
return 0;
}
解决时间:30分。
还有45分钟。
4:数糖纸。
99%的人一看到这道题就一定会想到用暴力解。
那么:
评测说明 : 1s 256MB
再看数据:
对于100%的数据:
1
<
=
N
<
=
1
0
6
,
0
<
=
C
i
<
=
1
0
9
1<=N<= 10^6 , 0<=C_i<= 10^9
1<=N<=106,0<=Ci<=109
暴力解法时间复杂度:
O
(
n
3
)
O(n^3)
O(n3)
所以不能用暴力解,会TLE。
正确方法应该用队解,每输入一个数就判断它是不是出现在队里,如果是,就将它前面的数出队,否则就将它入队,每进行一次操作就将答案与队列长度比较如果队列长度大于答案就将队列长度赋值给答案。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int n;
long long a[1000009];
bool vis[1000000009];
queue<int> s;
int ans=0;
int p;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
if(vis[a[i]]){
while(s.front()!=a[i]){
vis[s.front()]=0;
s.pop();
}
vis[s.front()]=0;
s.pop();
}
s.push(a[i]);
vis[a[i]]=1;
p = s.size();
ans = max(ans,p);
}
cout<<ans;
return 0;
}
5:曲线难题。
此题为魔王中的魔王,十分难做。
但是但是但是,你要知道,
两端点都不在边上的曲线一定不会相交!!!
所以只用考虑边上的曲线就行了!
AC代码:
#include<bits/stdc++.h>
using namespace std;
int r,c,n;
struct node{
int x,y,id,col;
};
bool cmp(node x,node y){
if(x.id==y.id){
if(x.id==1){
return x.y<y.y;
}
if(x.id==2){
return x.x<y.x;
}
if(x.id==3){
return x.y>y.y;
}
if(x.id==4){
return x.x>y.x;
}
}
return x.id<y.id;
}
int get(int x,int y){
if(x==0){
return 1;
}
if(y==c){
return 2;
}
if(x==r){
return 3;
}
if(y==0){
return 4;
}
}
bool check(int a,int b){
return a==0||b==0||a==r||b==c;
}
node a[200005];
stack<int> s;
int main(){
ios::sync_with_stdio(false);
int cnt=0;
cin>>r>>c>>n;
for(int i=0;i<n;i++){
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
if(check(x1,y1)&&check(x2,y2)){
a[cnt].x=x1;
a[cnt].y=y1;
a[cnt].col=i;
a[cnt].id=get(x1,y1);
cnt++;
a[cnt].x=x2;
a[cnt].y=y2;
a[cnt].col=i;
a[cnt].id=get(x2,y2);
cnt++;
}
}
sort(a,a+cnt,cmp);
for(int i=0;i<cnt;i++){
if(!s.empty()&&a[i].col==s.top()){
s.pop();
}
else{
s.push(a[i].col);
}
}
if(s.empty()){
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
return 0;
}
小结:速度不够 快 ⇒ \Overrightarrow{快} 快,要是速度快可以A四道题。