AcWing平台每日一题活动

1.10

AcWing 104. 货仓选址

思路
绝对值不等式问题:中位数就是最优解
商店数为奇数时,货仓选址位置就是中间商店的那个位置。当为偶数时,货仓选址位置就在中间两个商店的任意位置
链接
https://www.acwing.com/problem/content/106/

时间复杂度
O ( n l o g n ) O(nlogn) O(nlogn)

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
int a[N];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    sort(a,a + n);
    int res = 0;
    for (int i = 0; i < n; i ++) res +=abs(a[i] - a[n / 2]);
    cout << res <<endl;
    return 0;
}

时间复杂度
O ( n ) O(n) O(n)

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 100010;

int n;
int a[N];

int main()
{
    cin >> n;
    for (int i = 0; i < n; i ++) cin >> a[i];
    nth_element(a, a + n / 2, a + n);//直接取第几个值,且时间复杂度O(n)
    int res = 0;
    for (int i = 0; i < n; i ++) res +=abs(a[i] - a[n / 2]);
    cout << res <<endl;
    return 0;
}

AcWing 898. 数字三角形

链接
https://www.acwing.com/problem/content/900/
思路
动态规划,线性DP问题:可以从上到下,也可以从下到上进行每一行的选择
其中所有的路径选择的总数为 2 n 2^n 2n .
纠错:是2的n-1次方,实话实说,我编辑不出来…

通过状态表示与状态计算进行DP:
其中状态表示指的是某一集合:这题如果是从下往上走,那么就是从底向上走到(i,j)所有路线的集合
其中状态计算指的是某一属性:集合的最大值、最小值或者个数,此题是找最大值,先找小集合的最大值,将小集合两两归并成大集合,最终的到大集合的最大值
链接
https://www.acwing.com/problem/content/900/

时间复杂度
O(nlogn)

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 510;

int n;
int w[N][N], f[N][N];

int main()
{
    cin >> n;
    for (int i = 1; i <=n; i ++ )
    {
        for (int j = 1; j <= i; j ++ )
        {
            cin >> w[i][j];
        }
    }//输入一个树形图(数字三角形)
    for (int i = 1; i <= n; i ++ ) f[n][i] = w[n][i];//从底往上,起始最后一行
    for (int i = n - 1; i; i --)
    {
        for (int j = 1; j <= i; j ++ )
        {
            f[i][j] = max(f[i + 1][j] + w[i][j], f[i + 1][j + 1] + w[i][j]);//树的左右分支
        }
    }
    cout << f[1][1] << endl;
    
    return 0;
}

AcWing 756. 蛇形矩阵

思路
题干的意思是让你顺时针去填空每一个位置
它是写游戏常见的思路:有墙就拐弯。
另外这里还可以通过偏移来减少判断的代码量
详细见代码注解
链接
https://www.acwing.com/problem/content/758/

#include <iostream>

using namespace std;

const int N = 110;
int q[N][N];

int main()
{
    int n, m;
    cin >> n >> m;
    int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    int x = 0, y = 0, d = 1;//定义初始填充位置与方向
    
    for (int i = 1; i <= n * m; i ++)
    {
        q[x][y] = i;//填充每一个位置
        int a = x + dx[d], b = y + dy[d];//如果没有撞墙,就一直延着一个方向走
        if(a < 0 || a >= n || b < 0 || b >= m || q[a][b])//墙,已经填充的位置也变成了墙
        {
            d = (d + 1) % 4;//如果撞墙了,就换方向
            a = x + dx[d], b = y + dy[d];
        }
        x = a, y = b;
    }
    for (int i = 0; i < n; i ++ )
    {
        for (int j = 0; j < m; j ++ )
        {
            cout << q[i][j] << ' ';
        }
        cout << endl;
    }
    
    return 0;
}

AcWing 1113. 红与黑

思路
广度优先搜索,深度优先搜索两个都ok
链接
https://www.acwing.com/problem/content/1115/

时间复杂度
O(m*n)

//BFS广度优先搜索
//#include <bits/stdc++.h> 万能头文件
#include <iostream>
#include <queue>
#include <algorithm>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;//存放两维坐标
const int N = 25;

int n, m;
char g[N][N];

int bfs(int sx, int sy)
{
    queue<PII> q;
    q.push({sx, sy});
    g[sx][sy] = '#';
    int res = 0;//记录所有能搜到的点的数量
    
    int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
    while (q.size())
    {
        auto t = q.front();//取出队头元素
        q.pop();//删去队头
        res ++ ;//从队列里每出来一个点,数量就 ++
        
        for (int i = 0; i < 4; i ++ )//扩展四个方向
        {
            int x = t.x + dx [i], y =t.y +dy[i];
            if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] != '.') continue;//当前位置走过,出界就continue
            g[x][y] = '#';//当前位置没有走过,标记一下
            q.push({x, y});//存到当前队列里
        }
    }
    return res;
}

int main()
{
    while (cin >> m >> n, n || m)
    {
        for (int i = 0; i < n; i ++ ) cin >>g[i];
        int x, y;
        for (int i = 0; i < n; i ++ )
            for ( int j = 0; j < m; j ++ )
                if (g[i][j] == '@')//设x,y为起始位置
                {
                    x = i;
                    y = j;
                }
        cout << bfs(x, y) <<endl;
    }
    
    return 0;
}

2.11

AcWing 4296. 合适数对

思路
从小到大先枚举x,在枚举y,第一个符合的方案就是x最小的方案。
链接
https://www.acwing.com/problem/content/description/4299/

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int n, a, b;
    cin >> n >> a >> b;
    
    for (int x = 0 ; x * a <= n; x ++)
    {
        if((n - a * x) % b == 0)
        {
            int y = (n - a * x) / b;
            cout << "YES" << endl;
            cout << x << ' '<< y << endl;
            return 0;
        }
    }
    
    cout << "NO" << endl;
    return 0;
}

AcWing 4297. 截断数组

思路
哈希表存放s1的值,与s3进行比较,如果相等则为最大,并输出。
链接
https://www.acwing.com/problem/content/description/4300/
在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_set>

using namespace std;

typedef long long LL;

const int N = 200010;

int n;
LL s[N];

int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; i ++)
    {
        int x;
        scanf("%d", &x);
        s[i] = s[i - 1] + x;
    }
    
    unordered_set<LL> hash;
    hash.insert(s[1]);
    
    for(int i = 2; i <= n; i++)
    {
        LL s3 = s[n] - s[i - 1];
        if(hash.count(s3))
        {
            printf("%lld\n", s3);
            return 0;
        }
        hash.insert(s[i]);
    }
    puts("0");
    return 0;
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nighty_k

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值