Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp

题目链接:http://codeforces.com/contest/597

A. Divisibility

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.

Input

The only line contains three space-separated integers ka and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).

Output

Print the required number.

Examples

input

1 1 10

output

10

input

2 -4 4

output

5

题意:找出[a,b]区间内整除k的数的个数;

思路:小心点特判即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=4e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
///   数组大小
int main()
{
    ll k,a,b;
    scanf("%lld%lld%lld",&k,&a,&b);
    if(a<=0&&b>=0)
        printf("%lld\n",(b/k)-(a/k)+1);
    else if(a>=0&&b>=0)
        printf("%lld\n",(b/k)-(a/k+(a%k?1:0))+1);
    else
        printf("%lld\n",(abs(a)/k)-(abs(b)/k+(abs(b)%k?1:0))+1);
    return 0;
}

B. Restaurant

time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

A restaurant received n orders for the rental. Each rental order reserve the restaurant for a continuous period of time, the i-th order is characterized by two time values — the start time li and the finish time ri (li ≤ ri).

Restaurant management can accept and reject orders. What is the maximal number of orders the restaurant can accept?

No two accepted orders can intersect, i.e. they can‘t share even a moment of time. If one order ends in the moment other starts, they can‘t be accepted both.

Input

The first line contains integer number n (1 ≤ n ≤ 5·105) — number of orders. The following n lines contain integer values li and ri each (1 ≤ li ≤ ri ≤ 109).

Output

Print the maximal number of orders that can be accepted.

Examples

input

27 114 7

output

1

input

51 22 33 44 55 6

output

3

input

64 81 54 72 51 36 8

output

2

贪心:按r从小到大排序即可;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=5e5+10,M=4e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
struct is
{
    int l,r;
    bool operator <(const is &c)const
    {
        return r<c.r;
    }
}a[N];

///   数组大小

int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&a[i].l,&a[i].r);
    sort(a+1,a+1+n);
    int s=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i].l>s)
        {
            ans++;
            s=a[i].r;
        }
    }
    printf("%d\n",ans);
    return 0;
}

C. Subsequences

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

For the given sequence with n different elements find the number of increasing subsequences with k + 1 elements. It is guaranteed that the answer is not greater than 8·1018.

Input

First line contain two integer values n and k (1 ≤ n ≤ 105, 0 ≤ k ≤ 10) — the length of sequence and the number of elements in increasing subsequences.

Next n lines contains one integer ai (1 ≤ ai ≤ n) each — elements of sequence. All values ai are different.

Output

Print one integer — the answer to the problem.

Examples

input

5 212354

output

7

题意:给你n个数,最长上升子序列长度为k+1的个数;

思路:看下数据范围k<10很关键,dp[i][j]表示以i为结束长度为j时候的方案数

   现在你到i的时候你只需要再T[j](树状数组)的a[i]的位置表示方案数;

   统计小于a[i]的方案,k==0时候特判;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=4e6+10,inf=2147483647;
const ll INF=1e18+10,mod=1e9+7;
struct AYT
{
    ll tree[N];
    int lowbit(int x)
    {
        return x&-x;
    }
    void update(int x,ll c)
    {
        while(x<N)
        {
            tree[x]+=c;
            x+=lowbit(x);
        }
    }
    ll query(int x)
    {
        ll ans=0;
        while(x)
        {
            ans+=tree[x];
            x-=lowbit(x);
        }
        return ans;
    }
};
AYT T[12];

///   数组大小
int a[N];
int main()
{
    int n,k;
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    if(k==0)
        return 0*printf("%d\n",n);
    ll ans=0;
    for(int i=1;i<=n;i++)
    {
        ans+=T[k].query(a[i]-1);
        for(int j=k;j>=2;j--)
        {
            ll x=T[j-1].query(a[i]-1);
            T[j].update(a[i],x);
        }
        T[1].update(a[i],1);
    }
    printf("%lld\n",ans);
    return 0;
}

A. Divisibility

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.

Input

The only line contains three space-separated integers ka and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).

Output

Print the required number.

Examples

input

1 1 10

output

10

input

2 -4 4

output

5
时间: 2024-11-08 19:00:00

Testing Round #12 A,B,C 讨论,贪心,树状数组优化dp的相关文章

Codeforces Beta Round #12 (Div 2 Only) D. Ball 树状数组查询后缀、最值

http://codeforces.com/problemset/problem/12/D 这里的BIT查询,指的是查询[1, R]或者[R, maxn]之间的最值,这样就够用了. 设三个权值分别是b[1], b[2], b[2]; 首先,先把b[1]值离散化,离散成一个个id,那么只能是在id比较大的地方找了.然后把b[2]排序,倒序查询,也就是先查询最大的,当然它是没可能自杀的,因为它已经最大了,然后查询完后,把它压进bit后,以后在bit查询,就不需要管b[2]了,因为在bit里面的b[2

[luoguP2672] 推销员(贪心 + 树状数组 + 优先队列)

传送门 贪心...蒟蒻证明不会... 每一次找最大的即可,找出一次最大的,数列会分为左右两边,左边用stl优先队列维护,右边用树状数组维护.. (线段树超时了....) 代码 #include <queue> #include <cstdio> #include <iostream> #define N 100001 #define ls now << 1 #define rs now << 1 | 1 #define max(x, y) (p[

D 洛谷 P3602 Koishi Loves Segments [贪心 树状数组+堆]

题目描述 Koishi喜欢线段. 她的条线段都能表示成数轴上的某个闭区间.Koishi喜欢在把所有线段都放在数轴上,然后数出某些点被多少线段覆盖了. Flandre看她和线段玩得很起开心,就抛给她一个问题: 数轴上有个点突然兴奋,如果自己被身上覆盖了超过条线段,这个点就会浑身难受然后把Koishi批判一番. Koishi十分善良,为了不让数轴上的点浑身难受,也为了让自己开心,她想在数轴上放入尽量多的线段. 按照套路,Koishi假装自己并不会做这道题,所以她就来求你帮忙.并承诺如果你解决了问题就

【HOJ2430】【贪心+树状数组】 Counting the algorithms

As most of the ACMers, wy's next target is algorithms, too. wy is clever, so he can learn most of the algorithms quickly. After a short time, he has learned a lot. One day, mostleg asked him that how many he had learned. That was really a hard proble

[CSP-S模拟测试]:d(贪心+树状数组)

题目传送门(内部题65) 输入格式 第一行,一个自然数$T$,代表数据组数.对于每组数据:第一行,一个正整数$n$,一个自然数$m$.接下来$n$行,每行两个正整数,$a_i,b_i$. 输出格式 对于每组数据,输出一行,一个整数,代表答案. 样例 样例输入: 3 2 0 5 10 5 5 2 1 1 1 2 2 3 1 3 5 4 4 5 3 样例输出: 25412 数据范围与提示 保证$0\leqslant m<n,a_i,b_i\leqslant 10^5$. 题解 题目并不难,考虑贪心,

Codeforces Round #227 (Div. 2)---E. George and Cards(贪心, 树状数组+set维护, 好题!)

George is a cat, so he loves playing very much. Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers f

Codeforces Round #301 (Div. 2) E . Infinite Inversions 树状数组求逆序数

E. Infinite Inversions time limit per test 2 seconds memory limit per test 256 megabytes input standard input  output standard output There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We pe

uva 11054 Gerovia的酒交易(贪心+树状数组)

直线上有n个等距的村庄,每个村庄要么买酒,要么卖酒.把k个单位的酒从一个村庄运到相邻村庄需要k个单位的劳动力.问最少需要多少劳动力才能满足所有村庄的需求 思路贪心 #include<cstdio> #include<cstring> #include<cmath> #include<cstdlib> #include<iostream> #include<algorithm> #include<vector> #inclu

Codeforces Round #277 E. LIS of Sequence(486E) 树状数组乱搞

http://codeforces.com/contest/486/problem/E E. LIS of Sequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output The next "Data Structures and Algorithms" lesson will be about Longest I