Codeforces Round #274 (Div. 1) A B C

昨天一觉起来都5点半了。。没来的及玩,补的题。。

A题:http://codeforces.com/contest/480/problem/A

A. Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.

According to the schedule, a student can take the exam for the i-th subject on the day number ai. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day bi (bi < ai). Thus, Valera can take an exam for the i-th subject either on day ai, or on day bi. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number ai.

Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

Input

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integers ai and bi (1 ≤ bi < ai ≤ 109) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

Output

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

Sample test(s)
input
3
5 2
3 1
4 2
output
2
input
3
6 1
5 2
4 3
output
6
Note

In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher writes down the schedule date, 4), then he takes an exam in the first subject (the teacher writes down the mark with date 5). Thus, Valera takes the last exam on the second day and the dates will go in the non-decreasing order: 3, 4, 5.

In the second sample Valera first takes an exam in the third subject on the fourth day. Then he takes an exam in the second subject on the fifth day. After that on the sixth day Valera takes an exam in the first subject.


简单贪心。。按a第一关键字,b第二关键字降序排序下就好。肯定先选满足的b。。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int mod=1000000007;
vector<pp>a;
int main()
{
    int n,x,y;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        a.pb(mp(x,y));
    }
    sort(a.begin(),a.end());
    int ans=0;
    for(int i=0;i<n;i++)
    {
        if(a[i].second>=ans) ans=a[i].second;
        else ans=a[i].first;
    }
    printf("%d\n",ans);
    return 0;
}

B: http://codeforces.com/contest/480/problem/B

B. Long Jumps
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long jumps, and Valery has lost his favorite ruler!

However, there is no reason for disappointment, as Valery has found another ruler, its length is l centimeters. The ruler already has nmarks, with which he can make measurements. We assume that the marks are numbered from 1 to n in the order they appear from the beginning of the ruler to its end. The first point coincides with the beginning of the ruler and represents the origin. The last mark coincides with the end of the ruler, at distance l from the origin. This ruler can be repesented by an increasing sequence a1, a2, ..., an, where ai denotes the distance of the i-th mark from the origin (a1 = 0an = l).

Valery believes that with a ruler he can measure the distance of d centimeters, if there is a pair of integers i and j (1 ≤ i ≤ j ≤ n), such that the distance between the i-th and the j-th mark is exactly equal to d (in other words, aj - ai = d).

Under the rules, the girls should be able to jump at least x centimeters, and the boys should be able to jump at least y (x < y) centimeters. To test the children's abilities, Valery needs a ruler to measure each of the distances x and y.

Your task is to determine what is the minimum number of additional marks you need to add on the ruler so that they can be used to measure the distances x and y. Valery can add the marks at any integer non-negative distance from the origin not exceeding the length of the ruler.

Input

The first line contains four positive space-separated integers nlxy (2 ≤ n ≤ 1052 ≤ l ≤ 1091 ≤ x < y ≤ l) — the number of marks, the length of the ruler and the jump norms for girls and boys, correspondingly.

The second line contains a sequence of n integers a1, a2, ..., an (0 = a1 < a2 < ... < an = l), where ai shows the distance from the i-th mark to the origin.

Output

In the first line print a single non-negative integer v — the minimum number of marks that you need to add on the ruler.

In the second line print v space-separated integers p1, p2, ..., pv (0 ≤ pi ≤ l). Number pi means that the i-th mark should be at the distance of pi centimeters from the origin. Print the marks in any order. If there are multiple solutions, print any of them.

Sample test(s)
input
3 250 185 230
0 185 250
output
1
230
input
4 250 185 230
0 20 185 250
output
0
input
2 300 185 230
0 300
output
2
185 230
Note

In the first sample it is impossible to initially measure the distance of 230 centimeters. For that it is enough to add a 20 centimeter mark or a 230 centimeter mark.

In the second sample you already can use the ruler to measure the distances of 185 and 230 centimeters, so you don't have to add new marks.

In the third sample the ruler only contains the initial and the final marks. We will need to add two marks to be able to test the children's skills.

有意思的题。

题意:有一个长为L的尺子,n个刻度,为了测出x和y的长度,问最少要添加几个刻度。

可以用一个set或者二分查找每一个刻度+x,+y是否存在。如果都不存在,那么判断两个刻度之间有没有x+y,y-x的长度,如果有那么只需添加一个就行。代码有点丑233.。

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int N=100005;
LL a[N];
int main()
{
    int n;
    LL l,x,y;
    scanf("%d%lld%lld%lld",&n,&l,&x,&y);
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    int ans=0,flag1=0,flag2=0;
    for(int i=0;i<n;i++)
    {
        if(a[i]+x<=l&&binary_search(a+i,a+n,a[i]+x))
        {
            flag1=1;
            break;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(a[i]+y<=l&&binary_search(a+i,a+n,a[i]+y))
        {
            flag2=1;
            break;
        }
    }
    ans=2-flag1-flag2;
    if(ans!=0)
    {
        if(ans==1)
        {
            printf("1\n");
            if(flag1==0)
                printf("%d\n",x);
            if(flag2==0)
                printf("%d\n",y);
            return 0;
        }
        for(int i=0;i<n;i++)
        {
            if(a[i]+x+y<=l&&binary_search(a+i,a+n,a[i]+x+y))
            {
                printf("1\n%d\n",a[i]+x);
                return 0;
            }
            if(a[i]-y>=0&&binary_search(a,a+i,a[i]+x-y))
            {
                printf("1\n%d\n",a[i]-y);
                return 0;
            }
            if(a[i]+y<=l&&binary_search(a+i,a+n,a[i]+y-x))
            {
                printf("1\n%d\n",a[i]+y);
                return 0;
            }
        }
        printf("2\n%I64d %I64d\n",x,y);
    }
    else printf("0\n");
    return 0;
}

C题: http://codeforces.com/contest/480/problem/C

C. Riding in a Lift
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Imagine that you are in a building that has exactly n floors. You can move between the floors in a lift. Let's number the floors from bottom to top with integers from 1 to n. Now you're on the floor number a. You are very bored, so you want to take the lift. Floor number b has a secret lab, the entry is forbidden. However, you already are in the mood and decide to make k consecutive trips in the lift.

Let us suppose that at the moment you are on the floor number x (initially, you were on floor a). For another trip between floors you choose some floor with number y (y ≠ x) and the lift travels to this floor. As you cannot visit floor b with the secret lab, you decided that the distance from the current floor x to the chosen y must be strictly less than the distance from the current floor x to floor b with the secret lab. Formally, it means that the following inequation must fulfill: |x - y| < |x - b|. After the lift successfully transports you to floor y, you write down number y in your notepad.

Your task is to find the number of distinct number sequences that you could have written in the notebook as the result of k trips in the lift. As the sought number of trips can be rather large, find the remainder after dividing the number by 1000000007 (109 + 7).

Input

The first line of the input contains four space-separated integers nabk (2 ≤ n ≤ 50001 ≤ k ≤ 50001 ≤ a, b ≤ na ≠ b).

Output

Print a single integer — the remainder after dividing the sought number of sequences by 1000000007 (109 + 7).

Sample test(s)
input
5 2 4 1
output
2
input
5 2 4 2
output
2
input
5 3 4 1
output
0
Note

Two sequences p1, p2, ..., pk and q1, q2, ..., qk are distinct, if there is such integer j (1 ≤ j ≤ k), that pj ≠ qj.

Notes to the samples:

  1. In the first sample after the first trip you are either on floor 1, or on floor 3, because |1 - 2| < |2 - 4| and |3 - 2| < |2 - 4|.
  2. In the second sample there are two possible sequences: (1, 2)(1, 3). You cannot choose floor 3 for the first trip because in this case no floor can be the floor for the second trip.
  3. In the third sample there are no sought sequences, because you cannot choose the floor for the first trip.

题意:一个人在电梯里面初始位置a层,只能上下|a-b|的位置,也不能停在该楼层,问这样走k次后不同的方案有多少种。

分析:dp方程很好写,dp[i][j]表示第走i次时在第j层有多少种方案,但是直接转移O(k*n*n)的复杂度肯定超时。

注意转移时比如第i次到第j层(设j<b),那么它可以由第i-1从1到第(j+b-1)/2转移过来(减去第j层的),所以我们只要算一个前缀和就可以实现转移了!复杂度O(k*n)

/**
 * @author neko01
 */
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <cstring>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define clr(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,-1,sizeof a)
#define dbg(a) printf("%d\n",a)
typedef pair<int,int> pp;
const double eps=1e-9;
const double pi=acos(-1.0);
const int INF=0x3f3f3f3f;
const LL inf=(((LL)1)<<61)+5;
const int mod=1000000007;
const int N=5005;
LL dp[N];
LL sum[N];
int main()
{
    int n,a,b,k;
    scanf("%d%d%d%d",&n,&a,&b,&k);
    if(a>b) a=n-a+1,b=n-b+1;
    for(int i=a;i<=n;i++) sum[i]=1;
    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<b;j++)
            dp[j]=(sum[(b+j-1)/2]-(sum[j]-sum[j-1])+mod)%mod;
        for(int i=1;i<=n;i++)
            sum[i]=(sum[i-1]+dp[i])%mod;
    }
    LL ans=0;
    for(int i=1;i<b;i++)
    {
        ans=ans+dp[i];
        if(ans>=mod)
            ans-=mod;
    }
    printf("%I64d\n",ans);
    return 0;
}




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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值