【USACO training】Chapter 1 入门

本教程介绍了USACO的入门章节,涵盖字符串模拟、日期模拟、区间合并、矩阵旋转等算法。通过AcWing平台的多个题目进行实践,如 AcWing 1340. 贪心的送礼者,AcWing 1343. 挤牛奶,以及AcWing 1348. 搭配牛奶等,旨在帮助初学者巩固基础并掌握贪心策略。
摘要由CSDN通过智能技术生成

整理的算法模板合集: ACM模板


Section 1.1 介绍

在这里插入图片描述

USACO Training 综述

http://train.usaco.org/usacogate(USACO 97道题)   
USA Computing Olympiad 是美国高校的信息学测评网站,也是美国中学生的官方竞赛网站。   
美国著名在线题库,专门为信息学竞赛选手准备。   
全英文界面,但有非官方的中文翻译。推荐直接阅读英语原文,既准确可靠又可提高英语水平。   
网站的Training题目全面,每道题附有详细题解,可查看测试数据和运行结果,便于调试、发现错误并改正。采用章节递进的层次结构,由易到难,讲授知识、练习编程结合,题目必须依次完成,避免了只挑简单题做的行为。   

第一章较为简单,属于竞赛入门题。基本不涉及太多算法和技巧,请在一周内刷完本章习题,用来巩固基础,并开始下一章的练习。

Section 1.2 提交解决方案,任务类型,特殊问题

luoguP1200、P1201、P1202、P1203

1.2.1 AcWing 1339. 你的旅途由此开始(字符串模拟)

在这里插入图片描述
经 典

简单的字符串模拟

#include <cstdio>
#include <algorithm>
#include <iostream>
std :: string s1, s2;
const int mod = 47;

int main()
{
   
    std :: cin >> s1 >> s2;
    int mul1 = 1, mul2 = 1;
    for(int i = 0; i < s1.length(); ++ i)
        mul1 *= (s1[i] - 'A' + 1) % mod;
    for(int i = 0; i < s2.length(); ++ i)
        mul2 *= (s2[i] - 'A' + 1) % mod;

    if(mul1 % mod == mul2 % mod)puts("GO");
    else puts("STAY");
    return 0;
}

1.2.2 AcWing 1340. 贪婪的送礼者(模拟,map)

在这里插入图片描述
模拟,直接用map存名字和当前的钱数,每次送钱的时候,自己能剩下的就等于 money - (money / num) * num,注意如果num = 0需要特判一下。剩下的直接模拟就行了

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>

using namespace std;
const int N = 50007;

int n, m;
map<string, int>mp;
string names[N];
int main()
{
   
    scanf("%d", &n);
    for(int i = 1; i <= n; ++ i){
   
        string s;
        cin >> s;
        names[i] = s;
        mp[s] = 0;
    }
    for(int i = 1; i <= n; ++ i){
   
        int money, num;
        string s;
        cin >> s;
        scanf("%d%d", &money, &num);
        int remain = 0;
        if(num != 0)remain = money - (money / num) * num;
        
        money -= remain;
        mp[s] -= money;
        for(int j = 1; j <= num; ++ j){
   
            string name;
            cin >> name;
            mp[name] += money / num;
        }
    }
    for(int i = 1; i <= n; ++ i){
   
        cout << names[i] << " " << mp[names[i]] << endl;
    }
    return 0;
}

1.2.3 AcWing 1341. 十三号星期五(日期模拟)

在这里插入图片描述
经典日期模拟题目。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

int month[13] = {
   0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int weekday[7];

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

    int year = 1900, days = 0;
    while (n -- )
    {
   
        for (int i = 1; i <= 12; i ++ )
        {
   
            weekday[(days + 12) % 7] ++ ;
            days += month[i];
            if (i == 2)
                if (year % 4 == 0 && year % 100 || year % 400 == 0)
                    days ++ ;
        }
        year ++ ;
    }

    for (int i = 5, j = 0; j < 7; i ++, j ++ )
        cout << weekday[i % 7] << ' ';

    return 0;
}

基姆拉尔森计算公式:

w e e k = ( d a y + 2 × m o n t h + [ 3 × ( m o n t h + 1 ) ÷ 5 ] + y e a r + [ y e a r ÷ 4 ] − [ y e a r ÷ 100 ] + [ y e a r ÷ 400 ] + 1 ) m o d 7 week=(day+2×month+[3×(month+1)÷5]+year+[year÷4]−[year÷100]+[year÷400]+1)mod7 week=(day+

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

繁凡さん

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

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

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

打赏作者

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

抵扣说明:

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

余额充值