4.3周五下午--东塘--蒋

· · 个人记录

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

错误原因:

没排序 ,样例过了。

思路:

  1. 排序完后对每一个数判断是否出现过 ,如果有就把他标记为出现过并加入一个数组 ,让后让这个数出现的次数加一。

    代码:

    
    #include<bits/stdc++.h>

using namespace std;

int n, a[200005], b[200005], m; map<int, int> t; map<int, bool> v;

int main() { cin >> n;

for(int i = 1; i <= n; i++)
{
    cin >> a[i];
}
sort(a + 1, a + 1 + n);

for(int i = 1; i <= n; i++)
{
    if(v[a[i]] == 0)
    {
        v[a[i]] = 1;
        b[++m] = a[i];
    }
    t[a[i]]++;
}

for(int i = 1; i <= m; i++)
{
    cout << b[i] << " " << t[b[i]] << "\n";
}
return 0;

}

# [T748205 跳房子](https://www.luogu.com.cn/problem/T748205?contestId=319118)
## 错误原因:
### 把题目理解错了 。
## 思路:
1. ### 暴力枚举每个起点, 让后可以用 $dfs$ 。每次在四周找到一个字符让后让字符串加上他 。
2. ### 直到大于五次时 ,就判断字符有没有出现过 ,如果没有就标记成有 ,并让答案加一。
## 代码:
```cpp
#include<bits/stdc++.h>
// 
using namespace std;

int cnt;
char a[10][10];
map<string, bool> vis;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

void dfs(int o, int x, int y, string s)
{
    if(o > 5)
    {
        if(vis[s] == 0)
        {
            vis[s] = 1;
            cnt++;
        }

        return ;
    } 

    for(int i = 0; i < 4; i++)
    {
        int nx = dx[i] + x, ny = dy[i] + y;
        if(nx >= 1 && nx <= 5 && ny >= 1 && ny <= 5)
        {
            dfs(o + 1, nx, ny, s + a[nx][ny]);
        }
    }

    return ; 
}

int main()
{
    for(int i = 1; i <= 5; i++)
    {
        for(int j = 1; j <= 5; j++)
        {
            cin >> a[i][j];
        }
    }

    for(int i = 1; i <= 5; i++)
    {
        for(int j = 1; j <= 5; j++)
        {
            string s;
            s += a[i][j];
            dfs(1, i, j, s);
        }
    }

    cout << cnt;
    return 0;
} 

T748207 [COCI 2010/2011 #3] ZNANSTVENIK

错误原因:

题意理解错了 ,以为可以随便删 。

思路:

  1. 我们可以从最后一行枚举 。先假设这行要保留 ,然后判断如果满足要求就跳出循环输出 n 减去要保留的几行 。

    代码:

    
    #include<bits/stdc++.h>
    // 
    using namespace std;

int n, m; bool v[1005]; map<string, bool> vis; char a[1005][1005];

bool check() { vis.clear();

for(int i = 1; i <= m; i++)
{
    string s = "";
    for(int j = n; j >= 1; j--)
    {
        if(v[j] == 1)
        {
            continue;
        }
        s += a[j][i];
    }

    if(vis[s] == 1)
    {
        return 0;
    }
    vis[s] = 1;
}

return 1;

}

int main() { cin >> n >> m;

for(int i = 1; i <= n; i++)
{
    for(int j = 1; j <= m; j++)
    {
        cin >> a[i][j];
    }   
    v[i] = 1;
}

int cnt = 0;
for(int i = n; i >= 1; i--)
{
    cnt++;
    v[i] = 0;
    if(check() == 1)
    {
        break;
    }
}

cout << n - cnt;
return 0;

}

# [T748209 鬼子进村](https://www.luogu.com.cn/problem/T748209?contestId=319118)
## 错误原因:
### 没看清楚题 ,以为是最后统一判断 。
## 思路:
1. ### 维护一个 $set$ 和一个 栈 ,都用来储存被摧毁的房子 。
2. ### 第一种情况把房子的编号压进 $set$ 和 栈里就好了 。第二种情况取栈的栈首 ,并把他清空 。
3. ### 第三种情况在 $set$ 里找离他最近的房子 ,如果最近的就是他自己就可以直接输出 $0$ , 否则输出离他最近的两座房子的距离。
## 代码:
```cpp
#include<bits/stdc++.h>

using namespace std;

int n, m, ans;
stack<int> stk;
set<int> se;

int main()
{
    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);
            stk.push(x);
        }
        else if(op == 'R')
        {
            x = stk.top();
            stk.pop();
            auto it = se.lower_bound(x);
            se.erase(it);
        }
        else if(op == 'Q')
        {
            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;
}