牛客练习赛7

ACM竞赛算法解析
本文提供了ACM竞赛中三道算法题的详细解答过程,包括简单的对比胜负判断、动态规划解决糖果分配问题以及使用离线查询算法进行高效求和运算。

有题解就不写题解啦、 这次三题比之前的感觉要简单点

A

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
 
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define lson (rt << 1)
#define rson ((rt << 1) | 1)
#define pill pair<int, int>
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e5 + 10;
const int INF = 1e9 + 10;
int a[10], b[10];
 
int main(){
    int t;  scanf("%d", &t) ;
    while(t--) {
        for(int i = 0; i < 6; ++i) {
            scanf("%d", a + i) ;
        }
        for(int i = 0; i < 6; ++i) {
            scanf("%d", b + i) ;
        }
        int x = 0, y = 0;
        for(int i = 0; i < 6; ++i) {
            for(int j = 0; j < 6; ++j) {
                if(a[i] > b[j])  x++;
                else if(a[i] < b[j]) y++;
            }
        }
        if(x == y) {
            puts("Tie");
        } else if(x > y) {
            puts("Alice");
        } else {
            puts("Bob");
        }
    }
    return 0;
}


B

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
 
using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define lson (rt << 1)
#define rson ((rt << 1) | 1)
#define pill pair<int, int>
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 400 + 10;
const LL INF = 1e15 + 10;
LL dp[qq][qq];
LL candy[qq][qq];
int n, m;
void init() {
    for(int i = 0; i <= n; ++i) {
        for(int j = 0; j <= n; ++j) {
            dp[i][j] = INF;
        }
    }
    dp[0][0] = 0;
}
void print() {
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= n; ++j) {
            printf("%lld ", dp[i][j]);
        }
        puts("");
    }
}
 
int main(){
    scanf("%d%d", &n, &m) ;
    for(int i = 1; i <= n; ++i) {
        for(int j = 1; j <= m; ++j) {
            scanf("%lld", &candy[i][j]) ;
        }
        sort(candy[i] + 1, candy[i] + 1 + m);
        for(int j = 1; j <= m; ++j) {
            candy[i][j] += candy[i][j - 1];
        }
    }
    init();
    for(int i = 1; i <= n; ++i) {
        for(int j = min(i * m, n); j >= i; --j) {
            for(int l = 0; l <= m; ++l) {
                if(j - l < 0)    continue;
                dp[i][j] = min(dp[i][j], dp[i - 1][j - l] + candy[i][l] + 1LL * l * l);
            }
        }
    }
    printf("%lld\n", dp[n][n]);
    return 0;
}


E

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset>
 
using namespace std;
#define LL __int128
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define lson (rt << 1)
#define rson ((rt << 1) | 1)
#define pill pair<int, int>
#define mst(a, b)   memset(a, b, sizeof a)
#define REP(i, x, n)    for(int i = x; i <= n; ++i)
const int MOD = 1e9 + 7;
const int qq = 1e6 + 10;
const int INF = 1e9 + 10;
LL sum[qq];
int a[qq], b[qq];
int n;
 
int lowBit(int x) {
    return x & (-x);
}
LL getSum(int x) {
    LL ans = 0;
    while(x > 0) {
        ans += sum[x];
        x -= lowBit(x);
    }
    return ans;
}
void upDate(int x, int dis) {
    while(x <= n) {
        sum[x] += dis;
        x += lowBit(x);
    }
}
struct Node {
    int x, id;
}p[qq];
bool cmp1(const Node &a, const Node &b) {
    return a.x < b.x;
}
bool cmp2(const Node &a, const Node &b) {
    return a.id < b.id;
}
void print(LL x) {
    if(x == 0)  return ;
    if(x)   print(x / 10) ;
    putchar(x % 10 + '0');
}
 
int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) {
        scanf("%d", a + i);
        b[i] = a[i];
    }
    sort(b + 1, b + n + 1);
    int k = unique(b + 1, b + n + 1) - b;
    LL ans = 0;
    for(int i = n; i >= 1; --i) {
        int x = lower_bound(b + 1, b + k, a[i]) - b;
        ans += getSum(x - 1) * i;
        upDate(x, n - i + 1);
    }
    if(ans == 0) {
        puts("0");
    } else {
        print(ans);
    }
    return 0;
}

D题应该可做,待补
牛客练习赛142是一场编程竞赛,通常包含多个算法题目,涵盖如数组、字符串、链表、动态规划等常见数据结构与算法知识点。针对这类比赛的解题思路和方法,可以从以下几个方面进行分析: ### 题目类型与解题策略 1. **数组相关问题** - 常见的题目包括查找数组中出现次数超过一半的数字、寻找缺失的数字、求解最大子数组和等。 - 解题方法包括使用哈希表统计频率、摩尔投票法(适用于多数元素问题)、双指针技巧或前缀和优化。 2. **链表操作** - 链表题目可能涉及反转链表、判断链表是否有环、找出两个链表的相交节点等。 - 例如,在找两个链表相交点的问题中,可以先计算各自长度,然后让长链表先走差值步数,再同步遍历比较节点地址[^3]。 3. **字符串处理** - 包括最长回文子串、无重复字符的最长子串等。 - 可采用滑动窗口、动态规划或中心扩展法等策略。 4. **树与图** - 树相关的题目可能涉及二叉树的遍历、路径和、最近公共祖先等问题。 - 图论问题可能需要使用深度优先搜索(DFS)、广度优先搜索(BFS)或拓扑排序等算法。 5. **动态规划** - 动态规划常用于解决背包问题、最长递增子序列、编辑距离等。 - 关键在于定义状态转移方程,并通过迭代或记忆化搜索进行求解。 6. **贪心算法** - 适用于区间调度、活动选择、硬币找零等问题。 - 贪心策略的核心在于每一步都做出局部最优选择。 ### 示例代码:摩尔投票法解决“多数元素”问题 ```python def majorityElement(nums): count = 0 candidate = None for num in nums: if count == 0: candidate = num count += (1 if num == candidate else -1) return candidate ``` 该算法时间复杂度为 O(n),空间复杂度为 O(1),非常适合处理大规模输入的数据集[^2]。 ### 提升解题能力的建议 - **刷题积累经验**:在 LeetCode、Codeforces、AtCoder 等平台上持续练习,熟悉各种题型。 - **学习经典算法**:掌握常见的算法模板,如二分查找、归并排序、快速选择等。 - **阅读官方题解与讨论区**:了解不同解法的优劣,尤其是最优解的时间复杂度分析。 - **模拟比赛训练**:定期参加在线编程比赛,提升实战能力和代码调试速度。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值