Codeforces Round #376 (Div. 2)

A - Night at the Museum
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Grigoriy, like the hero of one famous comedy film, found a job as a night security guard at the museum. At first night he receivedembosser and was to take stock of the whole exposition.

Embosser is a special devise that allows to "print" the text of a plastic tape. Text is printed sequentially, character by character. The device consists of a wheel with a lowercase English letters written in a circle, static pointer to the current letter and a button that print the chosen letter. At one move it's allowed to rotate the alphabetic wheel one step clockwise or counterclockwise. Initially, static pointer points to letter 'a'. Other letters are located as shown on the picture:

After Grigoriy add new item to the base he has to print its name on the plastic tape and attach it to the corresponding exhibit. It's not required to return the wheel to its initial position with pointer on the letter 'a'.

Our hero is afraid that some exhibits may become alive and start to attack him, so he wants to print the names as fast as possible. Help him, for the given string find the minimum number of rotations of the wheel required to print it.

Input

The only line of input contains the name of some exhibit — the non-empty string consisting of no more than 100 characters. It's guaranteed that the string consists of only lowercase English letters.

Output

Print one integer — the minimum number of rotations of the wheel, required to print the name given in the input.

Sample Input

Input
zeus
Output
18
Input
map
Output
35
Input
ares
Output
34

Hint

 

To print the string from the first sample it would be optimal to perform the following sequence of rotations:

  1. from 'a' to 'z' (1 rotation counterclockwise),
  2. from 'z' to 'e' (5 clockwise rotations),
  3. from 'e' to 'u' (10 rotations counterclockwise),
  4. from 'u' to 's' (2 counterclockwise rotations).
In total,  1 + 5 + 10 + 2 = 18 rotations are required.

题解: 一个字母到后一个字母的差(顺时针和逆时针中取最小) 第一个字母以a为基准 (水题!)

<pre name="code" class="cpp">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <map>
#include <list>
#include <queue>
using namespace std;

int main()
{
    int n,c;
    while(scanf("%d",&n)!=EOF)
    {
        c=0;
        printf("I hate ");
        n--;
        if(!n) printf("it");
        else printf("that ");
        while(n)
        {
            if(c==0)
            {
                printf("I love ");
                c=1;
            }
            else
            {
                printf("I hate ");
                c=0;
            }
            n--;
            if(!n) printf("it");
            else printf("that ");
        }
        printf("\n");
    }
    return 0;
}


 


B - Coupons and Discounts
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition.

Teams plan to train for n times during n consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be ai teams on the i-th day.

There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total).

As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days.

Sereja wants to order exactly ai pizzas on the i-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day n.

Input

The first line of input contains a single integer n (1 ≤ n ≤ 200 000) — the number of training sessions.

The second line contains n integers a1a2, ..., an (0 ≤ ai ≤ 10 000) — the number of teams that will be present on each of the days.

Output

If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes).

Sample Input

Input
4
1 2 1 2
Output
YES
Input
3
1 0 1
Output
NO

Hint

In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample.

In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days.


题解: 买pizza共有两种方法 (贪心 只要一天一天考虑就行了 水题)

第一种是2个一起买 

DAY1   DAY2 

  2             0        

第二种是相邻两天一天买一个(优惠券方式)  

DAY1   DAY2 

   1            1          

买的次数可以无限

对于每一天尽可能用第一种优惠方式买  余下的就用第二种方式买   

<pre name="code" class="cpp">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <map>
#include <list>
#include <queue>
using namespace std;
const int maxn=2e5+9;
int a[maxn];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
            int ok=1;
        for(int i=0;i<n-1;i++)
        {
            if( (a[i]%2)>a[i+1])
            {
                ok=0;
                break;
            }
            else a[i+1]=a[i+1]-(a[i]%2);
        }
        if(a[n-1]%2!=0) ok=0;
        if(ok) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

 



C - Socks
Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.

Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i (obviously, li stands for the left foot and ri for the right). Each sock is painted in one ofk colors.

When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.

Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.

The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of mdays.

Input

The first line of input contains three integers nm and k (2 ≤ n ≤ 200 0000 ≤ m ≤ 200 0001 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.

The second line contain n integers c1c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.

Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ nli ≠ ri) — indices of socks which Arseniy should wear during the i-th day.

Output

Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.

Sample Input

Input
3 2 3
1 2 3
1 2
2 3
Output
2
Input
3 2 2
1 1 2
1 2
2 1
Output
0

Hint

In the first sample, Arseniy can repaint the first and the third socks to the second color.

In the second sample, there is no need to change any colors.


题解: 有n只袜子 穿m天 k种颜色 只能在所有袜子穿之前给袜子涂色 求最少涂色数(左右颜色必须一样) 

          用并查集解决,只需一个一个集合处理,看每一个集合中需要涂色的最少袜子数


<pre name="code" class="cpp">#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <math.h>
#include <cstring>
#include <map>
#include <list>
#include <set>
#include <queue>
using namespace std;
const int maxn=2e5+9;
int fa[maxn];
int a[maxn],b[maxn];
set<int> s;
set<int>::iterator it;
vector<int> aa[maxn];//存每一个集合元素对应的编号
void setfa()//每个点都建立集合
{
    for(int i=1; i<maxn; i++)
        fa[i]=i;
}
int ffind(int x)
{
    return fa[x]!=x?fa[x]=ffind(fa[x]) : x;//递归获取根节点 数据压缩
}
int combine(int x,int y)//合并x,y两个元素所在的集合
{
    x=ffind(x);
    y=ffind(y);
    if(x!=y) fa[y]=x;
}
int main()
{
    int n,m,k,l,r;
    while(scanf("%d%d%d",&n,&m,&k)!=EOF)
    {
        s.clear();
        int sum=0,ok=0,SUM=0;;
        setfa();
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=1; i<=m; i++)
        {
            scanf("%d%d",&l,&r);
            s.insert(l);
            s.insert(r);
            combine(l,r);
        }
        for(int i=1; i<=n; i++)
        {
            int pop;
            it=s.find(i);
            if(it!=s.end())//袜子要用到
            {
                pop=ffind(i);
                if(aa[pop].size()==0) aa[pop].push_back(-1);
                aa[pop].push_back(a[i]);
            }
        }
        memset(b,0,sizeof(b));
        for(int i=1; i<=n; i++)
        {
            if(aa[i].size()!=0)//相应根元素所对应的集合遍历
            {
                int pp=aa[i].size(),M=0;
                sort(aa[i].begin(),aa[i].end());
                for(int j=aa[i].size()-1; j>0; j--)
                {
                    if(aa[i][j]!=aa[i][j-1])
                    {
                        b[aa[i][j]]+=pp-j;
                        M=max(M,pp-j);
                        pp=j;
                    }
                }
                SUM+=aa[i].size()-1-M;
                aa[i].clear();
            }
        }
        printf("%d\n",SUM);
    }
    return 0;
}

 



E和F还没做  QAQ

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值