4.3

· · 个人记录

也是扬眉吐气了一回好吧 |A |B |C |D |E |总 |排 | |:---:|:--:|:---:|:---:|:--:|:---:|:-:| |100|50|100|100|72|422|1|

1.P1097

可以直接用map,first存数字,second存次数

#include <bits/stdc++.h>
using namespace std ;
int n , x ;
map <int , int> mp ;
int main()
{
    cin >> n ;
    while(n --)
    {
        cin >> x ;
        mp[x] ++ ;//记录数据
    }   
    for(auto p : mp)//遍历
    {
        cout << p.first << " " << p.second << "\n" ;
    }
    return 0 ; 
}
#include <bits/stdc++.h>
using namespace std ;
int n , x ;
map <int , int> mp ;
int main()
{
    cin >> n ;
    while(n --)
    {
        cin >> x ;
        mp[x] ++ ;
    }   
    for(auto i = mp.begin() ; i != mp.end() ; i ++)
    {
        cout << i->first << " " << i->second << "\n" ;
    }
    return 0 ; 
}

只不过还可以用最长平台

#include <bits/stdc++.h>
using namespace std ;
int n , a[200005] , cnt = 1 ;
int main()
{
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++)
    {
        cin >> a[i] ;
    }
    sort(a + 1 , a + n + 1) ;
    for(int i = 2 ; i <= n ; i ++)
    {
        if(a[i] != a[i - 1])//如果不是同一个平台
        {
            cout << a[i - 1] << " " << cnt << "\n" ;//输出
            cnt = 1 ;//重置
        }
        else
        {
            cnt ++ ;//延续
        }
    }
    if(cnt)
    {
        cout << a[n] << " " << cnt << "\n" ;//注意!Atention!
    }
    return 0 ;
}

2.T748205

用set存可以得到的编码,得到编码的过程可以用dfs/bfs来做

#include <bits/stdc++.h>
using namespace std ;
string c[10][10] ;
set <string> st ;
int dx[5] = {1 , -1 , 0 , 0} , dy[5] = {0 , 0 , 1 , -1} ;
void dfs(int x , int y , string s)
{
    if(s.size() == 6)
    {
        st.insert(s) ;
        return ;
    }
    for(int i = 0 ; i < 4 ; i ++)
    {
        int nx = x + dx[i] , ny = y + dy[i] ;
        if(nx < 1 || nx > 5 || ny < 1 || ny > 5) continue ;
        dfs(nx , ny , s + c[nx][ny]) ;
    }
}
int main()
{
    for(int i = 1 ; i <= 5 ; i ++)
    {
        for(int j = 1 ; j <= 5 ; j ++)
        {
            cin >> c[i][j] ;
        }
    }
    for(int i = 1 ; i <= 5 ; i ++)
    {
        for(int j = 1 ; j <= 5 ; j ++)
        {
            dfs(i , j , c[i][j]) ;
        }
    }
    cout << st.size() ;
    return 0 ;
}

3. T408208

方法一:
注意这个n\le 32767,如果暴力的话O(n^2)=32767^2\approx10^9看上去会超时,但是实际上我们的j只用枚举到i-1,所以10^9\div 2=5\times10^8,卡不过去

#include <bits/stdc++.h>
using namespace std ;
long long n , a[33000] , sum , minn ;
int main()
{
    cin >> n ;
    cin >> a[1] ;
    sum += a[1] ;
    for(int i = 2 ; i <= n ; i ++)
    {
        cin >> a[i] ;
        minn = 2e9 ;
        for(int j = 1 ; j < i ; j ++)
        {
            minn = min(minn , max(a[i] - a[j] , a[j] - a[i])) ;
            if(minn == 0) break ; 
        }
        sum += minn ;
    }
    cout << sum ;
    return 0 ;
}

正解:
我们可以用set来记录之前的营销额,肯定是越接近越好,set会自动排序,所以我们可以只看上一个和下一个

#include <bits/stdc++.h>
using namespace std;
int n , x , ans ;
set <int> s ;
int main() 
{
    cin >> n ;
    for(int i = 1 ; i <= n ; i ++)
    {
        cin >> x;
        if (i == 1) 
        {
            ans += x;
        } 
        else 
        {
            auto it = s.lower_bound(x);  
            int min_diff = INT_MAX;
            if (it != s.end()) 
            {
                min_diff = min(min_diff, abs(x - *it));
            }
            if (it != s.begin()) 
            {
                --it;
                min_diff = min(min_diff, abs(x - *it));
            }
            ans += min_diff;
        }
        s.insert(x);
    }
    cout << ans;
    return 0;
}

4.T748207

你不能够乱删,你只能删除前i行,这个就是突破口。我们可以把每一列的字符串列举出来,删掉前i行意味着每一列的字符串都删前i个字符,用set看有没有重复

#include <bits/stdc++.h>
using namespace std ;
int n , m ;
char c ;
string s[1005] ;
int main()
{
    cin >> n >> m ;
    for(int i = 1 ; i <= n ; i ++)
    {
        for(int j = 1 ; j <= m ; j ++)
        {
            cin >> c ;
            s[j] += c ;
        }
    }
    for(int i = 1 ; i <= n ; i ++)
    {
        set <string> st ;
        for(int j = 1 ; j <= m ; j ++)
        {
            if(st.count(s[j].substr(i)))
            {
                cout << i - 1 ;
                return 0 ;
            }
            st.insert(s[j].substr(i)) ;
        }
    }
    return 0 ;
}

5.T408209

我们可以把被破坏的东西放进一个set,并压入栈st(因为R操作中要选最近的,所以stack刚好满足),我们每次查询要找的就是x所在的区间的左端点和右端点,我们可以假设两栋新房子:0,n+1,这两栋房子永远都是坏的,所以这样子长度就可以抽象成r-l+1

对于每次查询,我们先检查自身在不在set里,如果在,直接输出0,否则先把最近的右边的坏点找出来,如果没找到,那么就是n+1,左边的同理

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5e4 + 10;
int n , m , x ;
char op ;
set<int> broken;   
stack<int> st;    
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
    while (m--)
    {
        cin >> op;
        if (op == 'D')
        {
            cin >> x;
            broken.insert(x);
            st.push(x);
        }
        else if (op == 'R')
        {
            int x = st.top();
            st.pop();
            broken.erase(x);
        }
        else if (op == 'Q')
        {
            cin >> x;
            if (broken.count(x))
            {
                cout << 0 << '\n';
                continue;
            }
            auto it = broken.lower_bound(x);
            int R = (it == broken.end()) ? (n + 1) : *it;
            int L = 0;
            if (it != broken.begin())
            {
                --it;
                L = *it;
            }
            cout << R - L - 1 << '\n';
        }
    }
    return 0;
}