8.10上午 河西S班期末综合测试 - 蒋 总结

· · 个人记录

T1 P2032 扫描

课上原题,这个我是粗心了,没按老师的方法来定数组,而是直接打上去,导致少了一个零,RE了两的点。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[2000005];
int n, k;
deque<int> dq;
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k;
    for(int i=1;i<=n;i++){
        cin >> a[i];
        if(i>k && !dq.empty() && a[i-k]==a[dq.front()]){
            dq.pop_front();
        }
        while(!dq.empty() && a[dq.back()]<a[i]){
            dq.pop_back();
        }
        dq.push_back(i);
        if(i>=k) cout << a[dq.front()] << "\n";
    }
    return 0;
}

T2 P3737 [HAOI2014] 遥感监测

这题我AC了,是课后作业原题,就是那个雷达那道题。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, R, ans=1;
double last;
struct st{
    double l, r;
}a[105];
bool cmp(st x, st y){
    return x.r<y.r;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> R;
    for(int i=1;i<=n;i++){
        double x, y;
        cin >> x >> y;
        x*=1.0;
        y*=1.0;
        a[i].l=x-sqrt(R*R-y*y)*1.0;
        a[i].r=x+sqrt(R*R-y*y)*1.0;
    }
    sort(a+1, a+n+1, cmp);
    last=a[1].r;
    for(int i=2;i<=n;i++){
        if(a[i].l>last) ans++, last=a[i].r;
    }
    cout << ans;
    return 0;
}

T3 P3405 [USACO16DEC] Cities and States S

这还是课上原题。可能是课上当时没太理解且课后为认真复习的原因,正解与暴力都写错了。这道题正解是用map。

我之前提出过建边并看是否有环的想法。但时间复杂度貌似不允许,也不知道正不正确,改日再试。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, ans=0;
map<pair<string, string> , int> mp;
string s[200005], s1[200005];
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i=1;i<=n;i++){
        cin >> s[i] >> s1[i];
    }
    for(int i=1;i<=n;i++){
        string t1=s[i].substr(0, 2), t2=s1[i];
        pair<string, string> p1, p2;
        p1={t1, t2};
        p2={t2, t1};
        if(p1==p2) continue;
        if(mp.count(p2)) ans+=mp[p2];
        mp[p1]++; 
    }
    cout << ans;
    return 0;
}

P2082 区间覆盖(加强版)

仍然是课上原题,AC了。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, ans=0;
struct st{
    int l, r;
}a[100005];
bool cmp(st x, st y){
    return x.l<y.l;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n;
    for(int i=1;i<=n;i++) cin >> a[i].l >> a[i].r;
    sort(a+1, a+n+1, cmp);
    int begin1=a[1].l, last=a[1].r;
    for(int i=2;i<=n;i++){
        if(a[i].l>last){
            ans+=last-begin1+1;
            begin1=a[i].l;
            last=a[i].r;
        }
        last=max(last, a[i].r);
    }
    ans+=(last-begin1+1);
    cout << ans;
    return 0;
}

T4 P1784 数独

课后作业原题,AC了,其实就是大暴搜。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n=9, flag=0;
int mp[10][10], vis1[3][3][10], vish[10][10], visl[10][10];
void dfs(int x,int y){
    if(flag==1) return ;
    if(x==n+1){
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout << mp[i][j] << " ";
            }
            cout << "\n";
        }
        flag=1;
        return;
    }
    int nx, ny;
    if(y==n) nx=x+1, ny=1;
    else nx=x, ny=y+1;
    if(mp[x][y]!=0){
        dfs(nx, ny);
        return;
    }else{
        for(int i=1;i<=n;i++){
            if(vis1[(x-1)/3][(y-1)/3][i]==0 && vish[x][i]==0 && visl[y][i]==0){
                vis1[(x-1)/3][(y-1)/3][i]=1, vish[x][i]=1, visl[y][i]=1;
                mp[x][y]=i;
                dfs(nx, ny);
                vis1[(x-1)/3][(y-1)/3][i]=0, vish[x][i]=0, visl[y][i]=0;
                mp[x][y]=0;
            } 
        }
    }
    return;
}
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin >> mp[i][j];
            vish[i][mp[i][j]]=1;
            visl[j][mp[i][j]]=1;
            vis1[(i-1)/3][(j-1)/3][mp[i][j]]=1;
        }
    }
    dfs(1, 1);
    return 0;
}

T6 P1671 [USACO05FEB] Rigging the Bovine Election S

这道题我没有AC。

其实他的正解是变了型dfs,dfs里面套了个bfs,那为什么不用bfs呢?因为他可以是各种奇奇怪怪的形状,单纯的bfs不行。

代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 30;
typedef pair<int, int> pii;
int n, m, ans, sx, sy;
char a[N][N];
bool vis[N][N];
set<vector<pii> > se;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
void dfs(int num1, int num2, vector<pii> v) {
    if (num2 >= 4) return;
    if (num1 + num2 == 7) {
        if (num1 > num2) {
            sort(v.begin(), v.end());
            se.insert(v);
        }
        return ;
    }
    for (int k = 0; k < v.size(); k++) {
        int x = v[k].first, y = v[k].second;
        for (int i = 0; i < 4; i++) {
            int nx = x + dx[i], ny = y + dy[i];
            if (nx < 1 || ny < 1 || nx > n || ny > m) continue;
            if (vis[nx][ny]) continue;
            vis[nx][ny] = 1;
            vector<pii> v2 = v;
            v2.push_back({nx, ny});
            if (a[nx][ny] == 'J') dfs(num1 + 1, num2, v2);
            else dfs(num1, num2 + 1, v2);
            vis[nx][ny] = 0;
        }
    }
    return;
}
int main() {
    n = 5;
    m = 5;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            cin >> a[i][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++) {
            vector<pii> v;
            v.push_back({i, j});
            vis[i][j] = 1;
            if (a[i][j] == 'J') dfs(1, 0, v);
            else dfs(0, 1, v);
            vis[i][j] = 0;
        }
    cout << se.size();
    return 0;
}

T7 P12194 [NOISG 2025 Prelim] Snacks

我们把提议改成我们熟悉的样子:给定n个数,进行q次操作,每次将 (l_i, r_i) 范围内的数更改为 x_i ,并输出每次更改前与更改后n个数的和。

那我们可以使用map与lower_bound神力,先用num存,再算前缀和,用lower_baound查找并直接将内个值所对应的num清空并且加到修改后的num上,最终值也随之修改。最后实时输出即可。

代码:

#include<bits/stdc++.h>
#define int long long
using namespace std;
map<long long, long long> num;
long long n, q, ans;  
signed main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> q;
    for(int i=1;i<=n;i++){
        int a;
        cin >> a;
        num[a]++;
        ans+=a;
    }
    cout << ans << "\n";
    while(q--){
        int l, r, x;
        cin >> l >> r >> x;
        int tot=0;
        auto it=num.lower_bound(l);
        while(it!=num.end() && it->first<=r){
            tot+=it->second;
            ans-=(it->first) * (it -> second);
            auto it2=it;
            it++;
            num.erase(it2);
        }
        num[x] += tot;
        ans+=tot*x;
        cout << ans << "\n";
    }
    return 0;
}

说实话,这样子一想,还是挺简单的。