洛谷试炼场---USACO

洛谷试炼场---USACO

USACO Section 1.1

1.p1200 你的飞碟在这儿
难度:入门难度
考点:输入,输出 ,字符串操作,字符转数字,取模  
适用:小学生

#include <stdio.h>
#include <string.h>
int main(){
    char hui[10],dui[10];
    int h,d;
    int hlen,dlen;
    int i;
    scanf("%s%s",hui,dui);
    hlen=strlen(hui);
    dlen=strlen(dui);
    h=1;
    d=1;
    for(i=0;i<hlen;i++)
        h*=hui[i]-'A'+1;
    for(i=0;i<dlen;i++)
        d*=dui[i]-'A'+1;
    if(h%47==d%47)
        printf("GO\n");
    else
        printf("STAY\n");
    return 0;
}

2.//P1201 [USACO1.1]贪婪的送礼者Greedy Gift Givers
//读完题目,第一直觉,弄懂样例,该题就做了大半了。
//模拟了样例中dave的数据,发现剩下的钱可以算做收到的钱。
//开个结构体,虽然多耗费了空间,但理解起来比较方便。
//该题的输入也值得一编,有些烦,但不难。
//编着编着,突然发现,查找需要花时间,是否会超时?2000*2000=4*10^6应该不会超时
//提交,测试点1,7,8WA,测试点4,5,6,9TLE
//感觉题目不对劲,查了英文原题,
//Lines NP+2..end:     NP groups of lines organized like this:
//The first line in the group tells the person's name who will be giving gifts.
//The second line in the group contains two numbers: The initial amount of money (in the range 0..2000) to be divided up into gifts by the giver and then the number of people to whom the giver will give gifts, NGi (0 ≤ NGi ≤ NP-1).
//If NGi is nonzero, each of the next NGi lines lists the the name of a recipient of a gift.
//看了他人代码,真是火大了,怎么翻译的,弄了半天,发钱的种类同样有n种,关键一句: NP groups of lines organized like this:
//修改,提交AC,水题一道,翻译坏了事,真是服了洛谷对该题的翻译,水题变难题。
#include <stdio.h>
#include <string.h>
struct node{
    char name[20];
    int fa;
    int shou;
}q[2000+10];
int main(){
    int n,i,j,k,fa,num;
    char name[20];
    memset(q,0,sizeof(q));
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%s",&q[i].name);
    for(i=1;i<=n;i++){
        scanf("%s",name);
        scanf("%d%d",&fa,&num);
        for(j=1;j<=n;j++)
            if(strcmp(q[j].name,name)==0){
                q[j].fa=fa;
                if(num!=0)
                    q[j].shou+=fa%num;
                break;
            }
        for(j=1;j<=num;j++){
            scanf("%s",&name);
            for(k=1;k<=n;k++)
                if(strcmp(q[k].name,name)==0){
                    if(num!=0)
                        q[k].shou+=fa/num;//1 此处写成q[j].shou=fa/num;,查了会
                    break;
                }
        }
    }
    for(i=1;i<=n;i++)
        printf("%s %d\n",q[i].name,q[i].shou-q[i].fa);
    return 0;
}

3.//P1202 [USACO1.1]黑色星期五Friday the Thirteenth
//需要统计的天数400*365 故一次循环不会超时
//该题的难点,计算 计算1900年1月1日至1900+N-1年12月31日 天数
//之后开一个数组,对周一到周日天数进行统计。
//输入样例,怎么输出结果不对,重新看题,昏倒,题意理解错误。
//该题算法十分朴素,但也不容易做出,本人明白了小学奥数,我们也有做不出来的内容。
//https://www.luogu.org/discuss/show?postid=3345看了讨论,一口血吐出来,原以为输出是按:周一 周二 三 四 五 六 周日的方式
//一看讨论,才知道,注意输出顺序是:周六、周日、周一、周二、周三、周四、周五
//痛定思痛,决定还是看英文原题的比较好。2017-10-12 21:54 AC
//特意找了英文原题,才发现:
//OUTPUT FORMAT
//Seven space separated integers on one line. These integers represent the number of times the 13th falls on Saturday, Sunday, Monday, Tuesday, ..., Friday.
//洛谷题目怎么翻译的,在这个地方花了做题者不少冤枉的时间。
//USACO思路如下,先百度英文原题,在进行程序编写。
#include <stdio.h>
#include <string.h>
int count[10];
int month[]={31,0,31,30,31,30,31,31,30,31,30,31};//1-12个月,2月位置里的0用来占位。
int run(int year){//判断闰年,1闰年,0非闰年
    if(year%100==0&&year%400!=0)//1900
        return 0;
    if(year%100==0&&year%400==0)
        return 1;
    if(year%4==0)
        return 1;
    return 0;
}
int main(){
    int days=0,n,i,j;
    memset(count,0,sizeof(count));
    scanf("%d",&n);
    for(i=0;i<n;i++){//年
        for(j=0;j<12;j++){//月
            count[(days+13-1)%7]++;//count[(days+13-1)%7]++;不对,改成 count[(days+13)%7]++;不对,改成 count[(days+13+1)%7]++;样例正确,试着提交,结果AC
            printf("%d %d\n",j,(days+13+1)%7);
            if(j==1)
                if(run(1900+i))
                    days+=29;
                else
                    days+=28;
            else
                days+=month[j];
        }
    }
    printf("%d %d",count[5],count[6]);
    for(i=0;i<5;i++){
        printf(" %d",count[i]);
    }
    return 0;
}

4.//P1203 [USACO1.1]坏掉的项链Broken Necklace
//自个想法是枚举,以为他人会有什么好办法,翻看他人,看了描述部分几个字,枚举,代码没看。
//那么好吧,自己动手,丰衣足食。
//仔细想想,做个关于'w'的预处理,写起代码来会更省心。一写,代码量还挺大,决定推倒重来。
//编着编着,分'w'非'w'挺复杂的,翻看这篇http://blog.csdn.net/chai_jing/article/details/53103002发现确实写得好.
//试了上文,无法AC。但不影响算法写得好。错了测试点3
//考虑两个问题,一是超过n长度,直接输出n,二是'w'开头,可以看成'r'或'b',这个题意不清。提交AC
//该题思路比较奇特,对于按部就班,分类讨论的是极大的挑战。
//技巧与 NOIP 2006 提高组 复赛 能量项链 有部分相同
#include <stdio.h>
char a[800];
int n;
int right(int i){
    int ans=0,j;
    for(j=i;j<=i+n-1;j++)//右扫
        if(a[i]==a[j]||a[j]=='w')
            ans++;
        else
            break;
    return ans;
}
int left(int i){
    int ans=0,j;
    for(j=i+n-1;j>=i;j--)//左扫
        if(a[i+n-1]==a[j]||a[j]=='w')
            ans++;
        else
            break;
    return ans;
}
int main(){
    int i,j,max=0,ans,b,c,d;
    
    scanf("%d%s",&n,a+1);
    for(i=1;i<=n;i++)
        a[i+n]=a[i];//拼成2n串,无需取模运算。
    for(i=1;i<=n;i++){
        ans=0;
        if(a[i]=='w'){//右扫处理
            a[i]='r';
            b=right(i);
            a[i]='b';
            c=right(i);
            a[i]='w';//恢复数据
            d=b>c?b:c;
        }else{
            d=right(i);
        }
        ans+=d;
        if(a[i+n-1]=='w'){//左扫处理
            a[i+n-1]='r';
            b=left(i);
            a[i+n-1]='b';
            c=left(i);
            a[i+n-1]='w';//恢复数据
            d=b>c?b:c;
        }else{
            d=left(i);
        }
        ans+=d;
        if(max<ans)
            max=ans;
    }
    if(max>n)
        printf("%d\n",n);
    else
        printf("%d\n",max);
    return 0;
}

2017-10-12 22:09 AC 该单元。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值