Codeforces Round #193 (A-C)(水题 + DP + 贪心)

这场是周三训练赛的一场CF,一个半小时只做出来了AB,后来补到了C。

这大概是一场阅读理解场吧,全场读不懂题目。这英语水平怎么过六级啊喂

A. Down the Hatch!

Everybody knows that the Berland citizens are keen on health, especially students. Berland students are so tough that all they drink is orange juice!

Yesterday one student, Vasya and his mates made some barbecue and they drank this healthy drink only. After they ran out of the first barrel of juice, they decided to play a simple game. All n people who came to the barbecue sat in a circle (thus each person received a unique index bi from 0 to n - 1). The person number 0 started the game (this time it was Vasya). All turns in the game were numbered by integers starting from 1. If the j-th turn was made by the person with index bi, then this person acted like that:

1. he pointed at the person with index (bi + 1) mod n either with an elbow or with a nod (x mod y is the remainder after dividing x by y);

2. if j ≥ 4 and the players who had turns number j - 1j - 2j - 3, made during their turns the same moves as player bi on the current turn, then he had drunk a glass of juice;

3. the turn went to person number (bi + 1) mod n.

The person who was pointed on the last turn did not make any actions.

The problem was, Vasya's drunk too much juice and can't remember the goal of the game. However, Vasya's got the recorded sequence of all the participants' actions (including himself). Now Vasya wants to find out the maximum amount of juice he could drink if he played optimally well (the other players' actions do not change). Help him.

You can assume that in any scenario, there is enough juice for everybody.

Input

The first line contains a single integer n (4 ≤ n ≤ 2000) — the number of participants in the game. The second line describes the actual game: the i-th character of this line equals 'a', if the participant who moved i-th pointed at the next person with his elbow, and 'b', if the participant pointed with a nod. The game continued for at least 1 and at most 2000 turns.

Output

Print a single integer — the number of glasses of juice Vasya could have drunk if he had played optimally well.

Examples

input

4

abbba

output

1

input

4

abbab

output

0

Note

In both samples Vasya has got two turns — 1 and 5. In the first sample, Vasya could have drunk a glass of juice during the fifth turn if he had pointed at the next person with a nod. In this case, the sequence of moves would look like "abbbb". In the second sample Vasya wouldn't drink a single glass of juice as the moves performed during turns 3 and 4 are different.

A题:真的是阅读理解浪费了好久时间。其实就是n个人按照顺序有一串动作,当某个人和他前三个人做的动作一样时他能喝一杯juice,问编号为0的人最多能喝多少杯。就是对字符串每k*n-1,k*n-2,k*n-3位判断是否一样就行(0可以改变自己的动作而别人不可以)。复杂度O(lena/n)

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2010;
const double eps = 1e-8;
int sgn(double x){ return x>eps ? 1 : x<-eps ? -1 : 0;}

int main()
{
    int n,cnt = 0;
    char a[maxn];
    scanf("%d",&n);
    scanf("%s",a);
    int lena = strlen(a);

    for(int i=1; i*n < lena; i++)
    {
        if(a[i*n-1] == a[i*n-2] && a[i*n-2] == a[i*n-3]) cnt++;
    }

    printf("%d\n",cnt);
}


B. Maximum Absurdity

Reforms continue entering Berland. For example, during yesterday sitting the Berland Parliament approved as much as n laws (each law has been assigned a unique number from 1 to n). Today all these laws were put on the table of the President of Berland, G.W. Boosch, to be signed.

This time mr. Boosch plans to sign 2k laws. He decided to choose exactly two non-intersecting segments of integers from 1 to n of length k and sign all laws, whose numbers fall into these segments. More formally, mr. Boosch is going to choose two integers ab(1 ≤ a ≤ b ≤ n - k + 1, b - a ≥ k) and sign all laws with numbers lying in the segments [aa + k - 1] and [bb + k - 1] (borders are included).

As mr. Boosch chooses the laws to sign, he of course considers the public opinion. Allberland Public Opinion Study Centre (APOSC) conducted opinion polls among the citizens, processed the results into a report and gave it to the president. The report contains the absurdity value for each law, in the public opinion. As mr. Boosch is a real patriot, he is keen on signing the laws with the maximum total absurdity. Help him.

Input

The first line contains two integers n and k (2 ≤ n ≤ 2·1050 < 2k ≤ n) — the number of laws accepted by the parliament and the length of one segment in the law list, correspondingly. The next line contains n integers x1, x2, ..., xn — the absurdity of each law (1 ≤ xi ≤ 109).

Output

Print two integers ab — the beginning of segments that mr. Boosch should choose. That means that the president signs laws with numbers from segments [aa + k - 1] and [bb + k - 1]. If there are multiple solutions, print the one with the minimum number a. If there still are multiple solutions, print the one with the minimum b.

Examples

input

5 2
3 6 1 1 6

output

1 4

input

6 2
1 1 1 1 1 1

output

1 3

Note

In the first sample mr. Boosch signs laws with numbers from segments [1;2] and [4;5]. The total absurdity of the signed laws equals 3 + 6 + 1 + 6 = 16.

In the second sample mr. Boosch signs laws with numbers from segments [1;2] and [3;4]. The total absurdity of the signed laws equals 1 + 1 + 1 + 1 = 4.

B题:有n个数对连续k个求和,问哪两个起点所包含的两个长度为k的区间内的值求和最大。需要注意两个区间不能有重叠部分。

这道题第一反应就用尺取法将数组转化为sum[i] 表示以i为起点连续k个数的和,之后第一发不在状态,码了一个线段树出来想要在枚举i起点的同时,查询之后区间的最大值。完全可以实现,但是感觉不方便记录j的起点坐标,就放弃了。

整理思路后决定用dp,dp[i][0]表示以i为起点,到最后这个区域中sum[i]的最大值,同时用dp[i][1]记录下对应坐标,更新时尽量选择较小的下标。

然后枚举i,查询对应区间的dp[i][0],直接取最大值。复杂度O(n)

我也不知道自己哪里碰的不对了调了半天只要把B题代码粘在B题后面就会出问题,我等下统一粘在别的地方好了。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5+10;
const double eps = 1e-8;
int sgn(double x){ return x>eps ? 1 : x<-eps ? -1 : 0;}

long long dp[maxn][2];
long long a[maxn],b[maxn];


int main()
{
    int n, k,x=maxn,y=maxn;
    long long maxx = 0, sum = 0;
    scanf("%d%d",&n,&k);
    int r = n-k+1;
    for(int i=1;i<=n;i++)
        scanf("%I64d",&a[i]);
    for(int i=1;i<=k;i++)
        sum += a[i];

    b[1] = sum;

    for(int i=2;i<=r;i++){
        sum = sum + a[i+k-1] - a[i-1];
        b[i] = sum;
    }

    dp[r][0] = r;
    dp[r][1] = b[r];

    for(int i=r;i>=k+1;i--)
    {
        if(b[i] >= dp[i+1][1]){
            dp[i][1] = b[i];
            dp[i][0] = i;
        }
        else{
            dp[i][1] = dp[i+1][1];
            dp[i][0] = dp[i+1][0];
        }
    }

    for(int i=1;i<=n-2*k+1;i++)
    {
        if(b[i] + dp[i+k][1] > maxx || (b[i] + dp[i+k][1] == maxx && dp[i+k][0] < y)){
            x = i;
            y = dp[i+k][0];
            maxx = b[i] + dp[i+k][1];
        }
    }


    printf("%d %d\n",x, y);
}

C. Students' Revenge

A student's life is fraught with complications. Some Berland University students know this only too well. Having studied for two years, they contracted strong antipathy towards the chairperson of some department. Indeed, the person in question wasn't the kindest of ladies to begin with: prone to reforming groups, banning automatic passes and other mean deeds. At last the students decided that she just can't get away with all this anymore...

The students pulled some strings on the higher levels and learned that the next University directors' meeting is going to discuss n orders about the chairperson and accept exactly p of them. There are two values assigned to each order: ai is the number of the chairperson's hairs that turn grey if she obeys the order and bi — the displeasement of the directors if the order isn't obeyed. The students may make the directors pass any p orders chosen by them. The students know that the chairperson will obey exactly k out of these p orders. She will pick the orders to obey in the way that minimizes first, the directors' displeasement and second, the number of hairs on her head that turn grey.

The students want to choose p orders in the way that maximizes the number of hairs on the chairperson's head that turn grey. If there are multiple ways to accept the orders, then the students are keen on maximizing the directors' displeasement with the chairperson's actions. Help them.

Input

The first line contains three integers n (1 ≤ n ≤ 105), p (1 ≤ p ≤ n), k (1 ≤ k ≤ p) — the number of orders the directors are going to discuss, the number of orders to pass and the number of orders to be obeyed by the chairperson, correspondingly. Each of the following n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109), describing the corresponding order.

Output

Print in an arbitrary order p distinct integers — the numbers of the orders to accept so that the students could carry out the revenge. The orders are indexed from 1 to n in the order they occur in the input. If there are multiple solutions, you can print any of them.

Examples

input

5 3 2
5 6
5 8
1 3
4 3
4 11

output

3 1 2

input

5 3 3
10 18
18 17
10 20
20 18
20 18

output

2 4 5

Note

In the first sample one of optimal solutions is to pass orders 1, 2, 3. In this case the chairperson obeys orders number 1 and 2. She gets 10 new grey hairs in the head and the directors' displeasement will equal 3. Note that the same result can be achieved with order 4 instead of order 3.

In the second sample, the chairperson can obey all the orders, so the best strategy for the students is to pick the orders with the maximum sum of ai values. The chairperson gets 58 new gray hairs and the directors' displeasement will equal 0.

C题:一共有n条规定,学生可以选择p条通过,学生会主席会遵守其中的k条。没条指令包含两个值,a 主席遵守后长出灰头发的数量,b 主席不遵守学校的反感值。主席选择k条遵守时会优先考虑学校的反感值。而我们需要让灰头发尽量多的前提下,让学校的反感值也尽量高。

这道题读题花了好久好久,都过了一天了才做出来。做这道题时快排了三次,第一次:按照主席的选择标准,越靠前的代表主席越不可能遵守。这是我们可以知道p-k条指令不管我们怎么挑选,主席是一定都不会遵守的,属于无效指令。第二次:我们就对剩下的指令按照我们的“惩罚标准”排序,找出惩罚力度最大的那几条。把它记录下来。第三次:还原成第一次排序的情况。记录的全选,而第一个记录的之前,紧连着的p-k条指令也要选上。结束


#include<bits/stdc++.h>
using namespace std;

const int maxn = 1e5+10;
const double eps = 1e-8;
int sgn(double x){ return x>eps ? 1 : x<-eps ? -1 : 0;}

struct Node{
    int x, y, id;
    bool vis;
}a[maxn];

bool cmp1(Node a, Node b)           /// obey优先级排序
{
    if(a.y == b.y) return a.x>b.x;
    else return a.y<b.y;
}

bool cmp2(Node a, Node b)           /// grey优先级排序
{
    if(a.x == b.x) return a.y<b.y;
    else return a.x<b.x;
}

int main()
{
    int n,p,k;
    scanf("%d%d%d", &n, &p, &k);
    for(int i=0; i<n; i++)
    {
        scanf("%d%d", &a[i].x, &a[i].y);
        a[i].vis = false;
        a[i].id = i+1;
    }

    sort(a, a+n, cmp1);
    sort(a+p-k, a+n, cmp2);
    for(int i=n-1; i>=n-k; i--)
        a[i].vis = true;

    sort(a+p-k, a+n, cmp1);

    bool found = false;
    for(int i=p-k; i<n; i++)
    {
        if(a[i].vis)
        {
            if(!found){
                for(int j=i-1; j>=i-p+k; j--)
                    printf("%d ", a[j].id);
                found = true;
            }
            printf("%d ", a[i].id);
        }
    }

    printf("\n");
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值