周五东塘407--三班结课测试

· · 个人记录

周五东塘407--三班结课测试

题号 分数
T618144 100
T618146 100
T618168 100
T618173 0
T619294 100
P1873 100
T618404 5

T618144 迷宫之输出最短步数

标准BFS(肥肠erzy)

#include<bits/stdc++.h> 
#define int long long
using namespace std;
int n,m;
char s[501][501];
int dir[8][10]={{1,0},{0,1},{-1,0},{0,-1}};
bool vis[501][501];
struct node{
    int x,y,step;
};
signed main(){
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=m;j++){
            cin>>s[i][j];
        }
    }
    queue<node> q;
    q.push({1,1,0});
    vis[1][1]=1;
    while(!q.empty()){
        node cu=q.front();
        q.pop();
        if(cu.x==n&&cu.y==m){
            cout<<cu.step<<endl;
            return 0; 
        }
        for(int i = 0;i<=3;i++){
            int px=cu.x+dir[i][0];
            int py=cu.y+dir[i][1];
            if(px>=1&&py>=1&&px<=n&&py<=m&&vis[px][py]!=1&&s[px][py]!='1'){
                vis[px][py]=1;
                q.push({px,py,cu.step+1});
            }
        }
    }
    cout<<-1<<endl;
}

T618146 统计线路

标准DFS(肥肠erzy)

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,ans;
char mp[1001][1001];
bool vis[1001][1001];
int dir[8][10]={{1,0},{0,1},{-1,0},{0,-1}};
void dfs(int x,int y){
    if(mp[x][y]=='2'){
        ans++;
        return;
    }
    for(int i = 0;i<=3;i++){
        int px=x+dir[i][0];
        int py=y+dir[i][1];
        if(px>=1&&py>=1&&px<=n&&py<=n&&vis[px][py]!=1&&mp[px][py]!='1'){
            vis[px][py]=1;
            dfs(px,py);
            vis[px][py]=0;
        }
    }
}
signed main(){
    cin>>n;
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=n;j++){
            cin>>mp[i][j];
        }
    }
    vis[1][1]=1;
    dfs(1,1);
    cout<<ans;
}

T618168 全排列问题

模板题(肥肠erzy)

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
int a[100001];
int vis[1001];
void dfs(int v,int r){
    if(r==n){
        for(int i = 0;i<r;i++){
            cout<<"    "<<a[i];
        }
        cout<<endl;
        return;
    }
    if(v>=n){
        return;
    }
    for(int i = 1;i<=n;i++){
        if(!vis[i]){
            vis[i]=1;
            a[r]=i;
            dfs(v+1,r+1);
            vis[i]=0;
        }
    }
}
signed main(){
    cin>>n;
    dfs(0,0);
}

T618173 学籍管理

map题

错误原因

自己手写size,重复判断if忘记加一个判断条件,导致删除完后无法将size增加

ac code:

#include<bits/stdc++.h>
#define int long long
using namespace std;
map<string,int> mp;
int n,size;
signed main(){
    cin>>n;
    while(n--){
        int a;
        cin>>a;
        if(a==1){
            string t;
            int f;
            cin>>t>>f;
            if(!mp.count(t) ||mp[t]==-1){
                size++;
            }
            mp[t]=f;
            cout<<"OK"<<endl;
        }
        if(a==2){
            string t;
            cin>>t;
            if(mp[t]==-1||!mp.count(t)){
                cout<<"Not found"<<endl;
            }else{
                cout<<mp[t]<<endl;

            }
        }
        if(a==3){
            string t;
            cin>>t;
            if(mp[t]==-1||!mp.count(t)){
                cout<<"Not found"<<endl;
            }else{
                mp[t]=-1;
                cout<<"Deleted successfully"<<endl;
                size--;
            }
        }
        if(a==4){
            cout<<size<<endl;
        }
    }
}

T619294 二分查找左侧边界

模板题

#include<bits/stdc++.h>
#define int long long

using namespace std;
int n;
int s[10000001];
int q;
/*

*/
int tw(int x){
    int l=0,r=n+1;
    while(l+1<r) {
        int mid=(l+r)/2;
        if(s[mid]>=x){
            r=mid;
        }else{
            l=mid;
        }
    }
    return r;
}
signed main(){
    cin>>n;
    for(int i = 1;i<=n;i++){
        cin>>s[i];
    }
    sort(s,s+n);
    cin>>q;
    while(q--){
        int t;
        cin>>t;
        int f=tw(t);
        if(s[f]!=t){
            cout<<"-1"<<' ';
        }else{
            cout<<f<<' ';
        }
    }
}

P1873 [COCI 2011/2012 #5] EKO / 砍树

经典二分答案

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+1;
int n,m,l,r;
int s[N];
bool check(int x){
    int ans=0;
    for(int i = 1;i<=n;i++){
        if(s[i]>x){
            ans+=s[i]-x; 
        }
    }
    return ans>=m;
}
/*  
*/
signed main(){
    cin>>n>>m;
    for(int i = 1;i<=n;i++){
        cin>>s[i];
        r=max(r,s[i]);
    }
    while(l+1<r) {
        int mid=(l+r)/2;
        if(check(mid)){
            l=mid;
        }else{
            r=mid;
        }
    }
    cout<<l;
}

T618404 引用文献

二分答案题 未做

#include <bits/stdc++.h>
using namespace std;
int n,lks,a[100005];
bool check(int h) {
    int count=0, x = lks;
    for(int i = n; i >= 1; -- i) {
        if(a[i] >= h)
            ++ count;
        else if(a[i] == h - 1 && x)
            ++ count, -- x;
    }
    return count>=h;
}
int main() {
    cin>>n>>lks;
    for(int i=1; i<=n; i++) cin>>a[i];
    sort(a + 1, a + 1 + n);
    int l=1,r=n,ans=0;
    while(l<=r) {
        int mid=(l+r)/2;
        if(check(mid)) {
            ans=mid;
            l=mid+1;
        } else {
            r=mid-1;
        }
    }
    cout<<ans;
    return 0;
}