728总结

· · 个人记录

T637845 快往上爬

只要维护一个水位变量,采用最优传送策略:直接传送到可以传送的最高塔,再按照题意模拟即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, t, k, MAX;
int h[1000000 + 9];
signed main()
{
    cin >> t;
    while (t --)
    {
        cin >> n >> k;
        int now = 1;//表示当前水位高度
        for(int i = 1; i <= n; i ++)
        {
            cin >> h[i];//塔的高度
        }
        int cur = h[k];//当前k塔的高度
        bool flag = true;
        sort(h + 1, h + 1 + n);//要前往最高塔,排序方便计算
        for(int i = 1; i <= n; i ++)
        {
            if (cur >= h[i])//不要选择传送到更低的塔
            {
                continue;
            }
            else
            {
                if (now + abs(h[i] - cur) - 1 > cur)//传送到i塔前水会淹过来
                {
                    cout << "NO\n";
                    flag = false;
                    break;
                }
            }
            now += abs(h[i] - cur);//水位上升
            cur = h[i];//传送到了i塔
        }
        if (flag)
        {
            cout << "YES\n";
        }
    }
}

T637434 小梦分糖果

遍历数组,发现要改的就计算更改次数,取每次更改的最小值即可。

#include<bits/stdc++.h>
using namespace std;
int t, n ,a[100000 + 9];
int main()
{
    cin >> t;
    while (t --)
    {
        cin >> n;
        for(int i = 1; i <= n; i ++)
        {
            cin >> a[i];    
        } 
        sort(a + 1, a + n + 1);//糖果数量排序,方便计算
        int cnt = 0, MIN = 1e9;
        for (int i = 1; i < n; i ++)
        {
            cnt = i - 1;
            int pos = lower_bound(a + 1, a + 1 + n, a[i] + a[i + 1]) - a;//二分当前数量和上一个的数量是否要改
            cnt += n - pos + 1; //要改的次数
            MIN = min(MIN, cnt);//取最小值
        }
        cout << MIN << "\n";
    }
    return 0;
}

T637889 箭头路径

使用广搜寻找到达 (2, n) 的路,但到达 (i, j) 时需要判断当前格子上的字符。如为<,则向左走一格;否则向右走一格即可。

#include <bits/stdc++.h> 
using namespace std;
char a[2 + 9][100000 + 9];
bool vis[2 + 9][100000 + 9];
int n;
struct node
{
    int x, y;//不需要记录步数
};
int dx[4] = {-1, 0, 1, 0}; 
int dy[4] = {0, 1, 0, -1};
string BFS()
{
    queue <node> q;
    q.push({1, 1});//压入起点(1, 1)
    while (q.empty() == false)
    {
        node c = q.front();//取出当前队首元素
        q.pop();//弹出队首
        if (c.x == 2 && c.y == n)//到达终点(2, n)
        {
            return "YES\n";
        }
        for (int i = 0; i <= 3; i ++)//向四周扩散道路
        {
            int nx = c.x + dx[i];
            int ny = c.y + dy[i];
            if (a[nx][ny] == '<')//当前单元格中的箭头为<,强制向左移动1格。
            {
                ny --;
            }
            else//当前单元格中的箭头为>,强制向右移动1格。
            {
                ny ++;
            }
            if (nx >= 1 && nx <= 2 && ny >= 1 && ny <= n && vis[nx][ny] == false)//可以走
            {
                vis[nx][ny] = true;//标记为走过
                q.push({nx, ny});//压入新起点
            }
        }
    }
    return "NO\n";
}
int main()
{
    int t;
    cin >> t;
    while (t --)
    {
        cin >> n;
        memset(vis, false, sizeof vis);//每次都要清空当前vis数组
        for (int i = 1; i <= 2; i ++)
        {
            for (int j = 1; j <= n; j ++)
            {
                cin >> a[i][j];
            }
        }
        cout << BFS();
    }
    return 0;
}

T637642 比较数字

找到 0 时直接把后面的数字全改成 1 即可。

#include <bits/stdc++.h>
#define int long long
using namespace std;
signed main()
{
    string s;
    bool f = true;//是否接下来输出1
    cin >> s;
    for (int i = 0; i < s.size(); i ++)
    {
        if (! f)//已经找到0了
        {
            cout << '1';
            continue;
        }
        if (s[i] != '0')//不是0直接输出
        {
            cout << s[i];
            continue;
        }
        f = false;
        cout << '1';
    }
    return 0;
}

T638238 传送门

对于每两个相通的楼层建立一条无向边,再进行DFS找能到达的最大节点编号即可。

#include<bits/stdc++.h>
#define int long long
using namespace std;
int ans = 0;
map <int, vector<int> > op;//楼层高度过大,在map里建立一个vector
map <int, bool> vis;
void dfs(int u)
{
    vis[u] = true;//标记为走过了
    ans = max(ans, u);//取最大节点编号
    for (int i = 0; i < op[u].size(); i ++)//枚举所有可以到达的点
    {
        int v = op[u][i];
        if (! vis[v])//当前点没有走过
        {
            dfs(v);//继续搜索
        }
    }
}
signed main()
{
    int n;
    cin >> n;
    for (int i = 1; i <= n; i ++)
    {
        int x, y;
        cin >> x >> y;
        op[x].push_back(y);//x可以通向y
        op[y].push_back(x);//y可以通向x
    }
    dfs(1);
    cout << ans;
    return 0;
}