8.18 总结

· · 个人记录

T638298 小明搭公交

思路

最后一句话还是有点难理解的好吧

判断 s+t 是否 \leq x,是那就求 s_{max},否则略过。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=150;
int x,n;
int s,t,ans=-1;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>x;
    for(int i=1;i<=n;i++){
        cin>>s>>t;
        if(s+t<=x) ans=max(s,ans);
    }
    cout<<ans;
    return 0;
} 

T638908 宾果

思路

怎么说呢,当时居然没发现 N \times (i-1) + j 弄下来就是 1、2、3...的顺序。

然后我们需要根据这个公式掌握根据一个数推出它所对应的行和列:

求行和列呢又和 八皇后 的做法有点像了,统计每行和每列被标记过的数量,判断是否 =n,如果是的就输出当前的回合数,不是那就继续。对角线我们就只需要两个变量实时统计,只需要判断是否在对角线上就行了。

补充:

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,t,hang[2005],lie[2005],d1,d2;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>t;
    for(int tt=1;tt<=t;tt++){
        int a;
        cin>>a;
        int i=(a-1)/n+1;
        int j=(a-1)%n+1;
        hang[i]++;
        lie[j]++;
        if(i==j) d1++;
        if(i+j==n+1) d2++;
        if(hang[i]==n || lie[j]==n || d1==n || d2==n){
            cout<<tt;
            return 0;
        }
    }
    cout<<-1;
    return 0;
}

T641930 长度为n的第k个排列

总结

送分

思路

说两种方法。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,k,cnt,a[100001];
bool vis[100001];
void dfs(int step){
    if(step==n+1){
        cnt++;
        if(cnt==k){
            for(int i=1;i<=n;i++){
                cout<<a[i]<<" ";
            }
            exit(0);
        }
        return;
    }
    for(int i=1;i<=n;i++){
        if(vis[i]==true) continue;
        a[step]=i;
        vis[i]=1;
        dfs(step+1);
        vis[i]=0;
    }
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    dfs(1);
    return 0;
}

T638280 兜兜转转还是你

名字这么有诗意

思路

代码走四个 bfs 广搜,从起点的四个邻居位置分别出发,如果从不同方向回到起点的邻居位置,则存在满足条件的回路。

然后这样的题目描述 hw \leq 10^6,所以要是正常的数组的话 char c[1e6][1e6](正常不能这样打)绝对会爆炸。所以只能用 vector,而且我们可以不用 char 类型的数组,直接用 bool,能走就是 0,否则(不能走)就是 1。

然后就是非常标准的广搜。

\tiny{黑历史:局部变量不能与全局变量重名}

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxx=1e6+5;
vector<bool> c[maxx];
int dx[]={1,-1,0,0};
int dy[]={0,0,1,-1};
int n,m,X,Y;
struct node{
    int x,y;
};
queue<node> q;
bool check(int x,int y){
    return (x<1 || x>n || y<1 || y>m);
}
void bfs(int sx,int sy){
    q.push({sx,sy});
    c[sx][sy]=1;
    while(!q.empty()){
        node f=q.front();
        q.pop();
        for(int i=0;i<4;i++){
            int xx=f.x+dx[i];
            int yy=f.y+dy[i];
            if(xx<1 || xx>n || yy<1 || yy>m || c[xx][yy]){
                continue;
            }
            c[xx][yy]=1;
            if(xx==X+1 && yy==Y || xx==X-1 && yy==Y || xx==X && yy==Y+1 || xx==X && yy==Y-1){
                cout<<"Yes";
                exit(0);
            }
            q.push({xx,yy});
        }
    }
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        c[i].push_back(0);
        for(int j=1;j<=m;j++){
            char x;
            cin>>x;
            if(x=='#') c[i].push_back(1);
            else if(x=='S'){
                X=i;
                Y=j;
                c[i].push_back(1);
            }
            else c[i].push_back(0);
        }
    }
    for(int i=0;i<4;i++){
        int xxx=X+dx[i];
        int yyy=Y+dy[i];
        if(check(xxx,yyy)) continue;
        if(!c[xxx][yyy]){
//          c[xxx][yyy]=1;
            while(q.size()) q.pop();
//          cout<<endl;
            bfs(xxx,yyy);
        }
    }
    cout<<"No";
    return 0;
}