SER2016 DIV1 B, K 题解

B: Base Sums

时间限制: 1 Sec  内存限制: 128 MB
提交: 17  解决: 0
[提交][状态][讨论版]

题目描述

Given three values n, a, and b, find the smallest m>n such that the sum of the digits of m in base a is the same as the sum of digits of m in base b.

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. There will be a single line of input, with three integers, n (0≤n≤1016), a and b (2≤a<b≤36), all of which will be in base 10.

输出

Output a single integer, m, which is the smallest number greater than n such that the sum of its digits in base a is the same as the sum of its digits in base b. Output m in base 10.

样例输入

66 10 16

样例输出

144

123

K: Zigzag

时间限制: 1 Sec  内存限制: 512 MB
提交: 85  解决: 21
[提交][状态][讨论版]

题目描述

A sequence of integers is said to Zigzag if adjacent elements alternate between strictly increasing and strictly decreasing. Note that the sequence may start by either increasing or decreasing. Given a sequence of integers, determine the length of the longest subsequence that Zigzags. For example, consider this sequence:
1 2 3 4 2
There are several Zigzagging subsequences of length 3:
1 3 2     1 4 2     2 3 2      2 4 2    3 4 2
But there are none of length greater than 3, so the answer is 3.

输入

Each input will consist of a single test case. Note that your program may be run multiple times on different inputs. The first line of input contains an integer n (1≤n≤1,000,000) which is the number of integers in the list. Each of the following n lines will have an integer k (1≤k≤1,000,000)

输出

Output a single integer, which is the length of the longest Zigzagging subsequence of the input list.

样例输入

5

1

2

3

4

2

样例输出

3


去重后 找 两端点  间的 拐点;

注意:

全部相同 答案为1; 一个端点

只有一个上升 或者 下降 答案为2  两个端点

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define FIN      freopen("input.txt","r",stdin)
#define FOUT     freopen("output.txt","w",stdout)
#define S1(n)    scanf("%d",&n)
#define SL1(n)   scanf("%I64d",&n)
#define S2(n,m)  scanf("%d%d",&n,&m)
#define SL2(n,m)  scanf("%I64d%I64d",&n,&m)
#define Pr(n)     printf("%d\n",n)
 
using namespace std;
typedef long long ll;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e7+5;
const int MOD=1e9+7;
const int mod=1e9+7;
int dir[5][2]={0,1,0,-1,1,0,-1,0};
 
ll inv[maxn*2],fac[maxn];
ll gcd(ll a,ll b){ return b?gcd(b,a%b):a;}
ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll ans=exgcd(b,a%b,x,y);ll temp=x;x=y;y=temp-a/b*y;return ans;}
ll lcm(ll a,ll b){ return b/gcd(a,b)*a;}
ll qpow(ll x,ll n){ll res=1;for(;n;n>>=1){if(n&1)res=(res*x)%MOD;x=(x*x)%MOD;}return res;}
void INV(){inv[1] = 1;for(int i = 2; i < maxn; i++) inv[i] = (MOD - MOD / i) * inv[MOD % i] % MOD;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){ x=1; y=0; d=a; }else{ ex_gcd(b,a%b,d,y,x); y-=x*(a/b);}}
void Fac(){fac[0]=1;for(int i=1;i<=maxn;i++)fac[i]=(fac[i-1]*i)%MOD;}
ll inv_exgcd(ll a,ll n){ll d,x,y;ex_gcd(a,n,d,x,y);return d==1?(x+n)%n:-1;}
ll inv1(ll b){return b==1?1:(MOD-MOD/b)*inv1(MOD%b)%MOD;}
ll inv2(ll b){return qpow(b,MOD-2);}
ll cal(ll m,ll n){if(m<n)return 0;return (fac[m]*inv[fac[n]]%MOD)%MOD*inv[fac[m-n]]%MOD;}
ll cals(ll m,ll n){if(m<n)return 0;return (fac[m]*inv1(fac[n])%MOD)%MOD*inv1(fac[m-n])%MOD;}
 
int a[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n)){
        mem(a,0);
        int t,k=1;
        scanf("%d",&a[1]);
        for(int i=2;i<=n;i++){
            scanf("%d",&t);
            if(t!=a[k])
                a[++k]=t;
        }
        if(k<2){
            printf("1\n");
            continue;
        }
        else{
            int sum=2;
            for(int i=2;i<k;i++){
                if(((a[i]>a[i-1])&&(a[i]>a[i+1]))||((a[i]<a[i-1])&&(a[i]<a[i+1]))){
                    sum++;
                }
            }
        //  if(sum>2)
                printf("%d\n",sum);
        //  else
        //      printf("0\n");
        }
    }
    return 0;
}



posted @ 2017-09-03 20:45  Sizaif  阅读(185)  评论(0编辑  收藏  举报