月赛_10_第一场

这场比赛迟到了40多分钟,最后感觉如果再多40分钟,我应该可以写4道题。不过最终以三道题结束。

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=94641#overview

第一题:

用时15分钟

http://codeforces.com/problemset/problem/558/A

思路:

题意就是在以原点0为中心,不断折返,直到要返回的一侧没有数值。输出经过位置的数字和。

我的解法不是挨个遍历,下面介绍我的思路 :

首先先定义了结构体和cmp函数,这步没什么,应该都是这样写。

下面定义了numa(小于0侧的数字个数和), numb(大于0侧的数字个数和), suma(小于0侧的数字和), sumb(大于0侧的数字和);

比较numa和numb的大小,找到小的那个num,ans+=(对应的sum)。对应另一侧只需将ans依次加上num+1个数即得到答案。

注意点就是num+1个数的下标范围的确定。

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

using namespace std;
struct node{
   int x, a;
}tree[105];

bool cmp(const node& h, const node & s){
    return h.x<s.x;
}

int main()
{
    int n, ans, suma, sumb, numa, numb;
    while(scanf("%d", &n)==1){
            ans = 0 ; numa = 0;
           numb = 0;sumb = 0;suma = 0;
        for(int i = 0; i < n; i++){
            scanf("%d%d", &tree[i].x, &tree[i].a);
        }
        sort(tree, tree+n, cmp);
        for(int i = 0; i < n; i++){
            if(tree[i].x<0) {suma+=tree[i].a; numa++;}
            else {sumb+=tree[i].a; numb++;}
        }
        if(numa>numb) {
            ans += sumb;
            for(int i = numa-1; i >=numa-numb-1; i--)
                ans+=tree[i].a;
        }
        else {
            ans += suma;
            for(int i = numa; i <= numa+numa; i++)
                ans+=tree[i].a;
        }
        printf("%d\n", ans);
    }
    return 0;
}

第二题

用时大约15分钟,开始想着用map或者set来记录每个数字出现的次数,但是用它们太局限了,不能实现其他功能,于是就定义了结构体,记录区间 l r 位置,和次数。

其实只要把出现次数和区间记录之后,按题意排序后,答案就是某一端点记录的 l r ,注意的是都需要加一。

这样问题就成了写比较函数cmp

按照题意,首先找到出现次数最大的数的区间,如果最大次数有多个,那么比较区间,输出区间小的,如果区间长度还一样,那就输出 左 l区间 小的那个区间。

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

struct node{
    int l, r;
    int num;
}b[1000005];

int a[100005];

bool cmp(const node &h, const node& s){
    if(h.num==s.num){
        if((h.r-h.l)==(s.r-s.l)){
            return h.l<s.l;
        }
        else return (h.r-h.l)<(s.r-s.l);
    }
    else return h.num > s.num;
}
int main()
{
    int n, maxn;
    while(scanf("%d", &n) == 1){
            memset(b, 0, sizeof(b));
            maxn = 0;
        for(int i = 0; i < n; i++){
           scanf("%d", &a[i]);
           if(b[a[i]].num==0) {
             b[a[i]].l = b[a[i]].r = i;
           }
           else  b[a[i]].r = i;
           b[a[i]].num++;
           if(maxn < a[i]) maxn=a[i];
        }
        sort(b, b+maxn+1, cmp);
        printf("%d %d\n", b[0].l+1, b[0].r+1);
    }
    return 0;
}

第四题

用时最长,脑子犯抽了,Time limit exceeded on test ** 错误提示当成wa了,就在找思路怎么错了,花了好长时间,还好明白过来的时间不算晚。

改过之后就过了。

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

int main()
{
    __int64 a, b, ans;
    scanf("%I64d%I64d", &a, &b);
        ans = 0;
        while( a%b!=0 ){      //开始判断条件是a==b
          ans+=a/b;                   //   a = a- b, 这种方式显然是最费时的。
          a = a % b;               //  简单模拟
          if(a<b) swap(a, b);
          }
          ans+=a/b;
        printf("%I64d\n", ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值