4.3总结

· · 个人记录

4.3总结

P1097 [NOIP 2007 提高组] 统计数字

错误代码:

#include<bits/stdc++.h>
using namespace std;
int n;
map<int,int> mp;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
      int a;
      cin>>a;
      if(mp.count(a)==1){
        mp[a]++;
      }else{
        mp[a]=1;
      }
    }
    for(auto i:mp){
      cout<<i.first()<<' '<<i.second()<<'\n';
    }
    return 0;
}

错误原因:

其实不难发现

cout<<i.first()<<' '<<i.second()<<'\n';

这里给first和second加了括号 所以CE!!!

正确代码:

#include<bits/stdc++.h>
using namespace std;
int n;
map<int,int> mp;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
      int a;
      cin>>a;
      if(mp.count(a)==1){
        mp[a]++;
      }else{
        mp[a]=1;
      }
    }
    for(auto i:mp){
      cout<<i.first<<' '<<i.second<<'\n';
    }
    return 0;
}

T748207 [COCI 2010/2011 #3] ZNANSTVENIK

正确思路:

题目让我们删除最多的前i行,那我们不妨把它看成保留最后n - i行。

所以我们可以维护一个每一位的map,随后每次加入一个,当满足时输出答案。

正确代码:

#include <bits/stdc++.h>
using namespace std;
int n,m;
char c[1005][1005];
string s[1005];
map<string,int> mp;
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>c[i][j];
        }
    }
    for(int i=n;i>=1;i--){
        for(int j=1;j<=m;j++){
            s[j]+=c[i][j];
        }
        mp.clear();
        bool flag=1;
        for(int j=1;j<=m;j++){
            if(mp[s[j]]==0) mp[s[j]]++;
            else{
                flag=0;
                break;
            }
        }
        if(flag==1){
            cout<<i-1;
            return 0;
        }
    }
    return 0;
}

T748209 鬼子进村

正确思路:

首先是修复动作,我们可以用stack来存储,这样就可以满足后进先出的规则。

随后是摧毁,我们只要在map上表明即可(在0和n+1处都放一个以摧毁房子)

最后是被围,我们只要已当前房子向两端遍历即可

正确代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m;
stack<int> st;
set<int> se;
signed main(){
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>m;
    se.insert(0);
    se.insert(n+1);
    while(m--){
        char op;
        int x;
        cin>>op;
        if(op=='D'){
            cin>>x;
            se.insert(x);
            st.push(x);
        }
        else if(op=='R'){
            x=st.top();
            st.pop();
            auto it=se.lower_bound(x);
            se.erase(it);
        }
        else{
            cin>>x;
            auto it=se.lower_bound(x);
            if(*it==x) cout<<"0\n";
            else{
                int l,r;
                r=*it;
                it--;
                l=*it;
                cout<<r-l-1<<'\n';
            }
        }
    }
    return 0;
}