Codeforces Round #129 (Div. 2)

Codeforces Round #129 (Div. 2)


A水题

B题,我用的是单调栈来搞的,思路:从右向左扫描,维护一个单调递减的栈,如果新加入的元素比栈顶元素高的话,就ans+=差值

#include <iostream>
#include <cstdio>

using namespace std;

const int maxn=100010;

int t[maxn],stack[maxn];

int main()
{
    int n;
    while(scanf("%d",&n)==1)
    {
        for(int i=1;i<=n;i++)
          scanf("%d",&t[i]);
        int top=0;
        long long ans=0;
        for(int i=n;i>=1;i--)
        {
            if(top>0&&t[i]>=stack[top-1])
            {
               // cout<<t[i]<<"  "<<stack[top-1]<<endl;
                ans+=(t[i]-stack[top-1]);
                while(top>0&&t[i]>=stack[top-1]) top--;
            }
            stack[top++]=t[i];
        }
        printf("%I64d\n",ans);
    }
    return 0;
}
/*
10
50 13 9 3 5 6 7 4 1 47
*/


C题,如果给定数的位数为len,那么先求出1,2,len-1位的满足题意的个数,然后再求len位的个数,例如4313

那么 1位数的个数 : 9

         2位数的个数: 9

        3位数的个数: 90

       四位数个数 :

          首位数是1,2,3时 3*100

        首位数是  4时     31  - 1

#include <iostream>
using namespace std;
long long a[20];
void init ( )
{
    a[1] = 9;
    a[2] = 9;
    for ( int i = 3; i < 20; ++i )
        a[i] = a[i - 1] * 10;
}
long long solve(long long x )
{
    long long ret = 0, tmp, tmp1;
    int len = 0, b[18], i;
    if ( x == 0 ) return 0;
    while ( x )
    {
        b[len++] = x % 10;
        x /= 10;
    }
    if ( len == 1 ) return b[0];
    for ( i = 1; i < len; ++i ) ret += a[i];
    //cout<<ret<<endl;
    tmp1 = b[len - 1] - 1;
    //cout<<tmp1<<endl;
    tmp = len - 2;
    while ( tmp-- ) tmp1 *= 10;
    //cout<<tmp1<<endl;
    ret += tmp1;
    for ( tmp = 0, i = len - 2; i > 0; --i ) tmp = tmp*10 + b[i];
    tmp++;
    if ( b[0] >= b[len - 1] ) tmp = tmp;
    else tmp--;
    ret += tmp;
    return ret;
}
int main ( )
{
    int i, len1, len2;
    init( );
    long long l, r;
    while( cin >> l >>r)
    {
        cout << solve ( r ) - solve(l-1) << endl;
    }
}

D题  离散化直接用map更方便

 

#include <iostream>
#include <map>
#include <cstdio>

using namespace std;

map<int,pair<int,int> > h;
map<int,pair<int,int> >::iterator it;

int main()
{
    int n,a,b;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&a,&b);
        h[a].first++;
        if(a!=b) h[b].second++;
    }
  //  for(it=h.begin();it!=h.end();it++)
  //    cout<<(*it).first<<" "<<(*it).second.first<<"  "<<(*it).second.second<<endl;
    n=(n+1)/2;
    int ans=99999999;
    for(it=h.begin();it!=h.end();it++)
      if((*it).second.first+(*it).second.second>=n) ans=min(ans,(n-(*it).second.first)<0?0:(n-(*it).second.first));
    if(ans!=99999999) printf("%d\n",ans);
    else puts("-1");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值