Codeforces Round #155 (Div. 2)

A. Cards with Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

Petya has got 2n cards, each card contains some integer. The numbers on the cards can be the same. Let's index all cards by consecutive integers from 1 to 2n. We'll denote the number that is written on a card with number i, as ai. In order to play one entertaining game with his friends, Petya needs to split the cards into pairs so that each pair had equal numbers on the cards. Help Petya do that.

Input

The first line contains integer n (1 ≤ n ≤ 3·105). The second line contains the sequence of 2n positive integers a1, a2, ..., a2n (1 ≤ ai ≤ 5000) — the numbers that are written on the cards. The numbers on the line are separated by single spaces.

Output

If it is impossible to divide the cards into pairs so that cards in each pair had the same numbers, print on a single line integer -1. But if the required partition exists, then print n pairs of integers, a pair per line — the indices of the cards that form the pairs.

Separate the numbers on the lines by spaces. You can print the pairs and the numbers in the pairs in any order. If there are multiple solutions, print any of them.

Sample test(s)
input
3
20 30 10 30 20 10
output
4 2
1 5
6 3
input
1
1 2
output
-1


题意:给你一些数字,看能否全部均等的分成两部分,可以的话输出每对的编号,不行的话输出-1

#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int a;
stack<int>p[5001];
int main()
{
    int n;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&n);
    for(int i=1;i<=5000;i++)
    {
        while(p[i].size())
            p[i].pop();
    }
    for(int i=1;i<=2*n;i++)
    {
        scanf("%d",&a);
        p[a].push(i);
    }
    for(int i=1;i<=5000;i++)
    {
        if(p[i].size()%2==1)
        {
            printf("-1\n");
            return 0;
        }
    }
    for(int i=1;i<=5000;i++)
    {
        while(p[i].size())
        {
            int e=p[i].top();
            p[i].pop();
            int f=p[i].top();
            p[i].pop();
            printf("%d %d\n",e,f);
        }
    }
    return 0;
}


B. Jury Size
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

In 2013, the writers of Berland State University should prepare problems for n Olympiads. We will assume that the Olympiads are numbered with consecutive integers from 1 to n. For each Olympiad we know how many members of the jury must be involved in its preparation, as well as the time required to prepare the problems for her. Namely, the Olympiad number i should be prepared by pipeople for ti days, the preparation for the Olympiad should be a continuous period of time and end exactly one day before the Olympiad. On the day of the Olympiad the juries who have prepared it, already do not work on it.

For example, if the Olympiad is held on December 9th and the preparation takes 7 people and 6 days, all seven members of the jury will work on the problems of the Olympiad from December, 3rd to December, 8th (the jury members won't be working on the problems of this Olympiad on December 9th, that is, some of them can start preparing problems for some other Olympiad). And if the Olympiad is held on November 3rd and requires 5 days of training, the members of the jury will work from October 29th to November 2nd.

In order not to overload the jury the following rule was introduced: one member of the jury can not work on the same day on the tasks for different Olympiads. Write a program that determines what the minimum number of people must be part of the jury so that all Olympiads could be prepared in time.

Input

The first line contains integer n — the number of Olympiads in 2013 (1 ≤ n ≤ 100). Each of the following n lines contains four integersmidipi and ti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare the i-th Olympiad (1 ≤ mi ≤ 12di ≥ 11 ≤ pi, ti ≤ 100), di doesn't exceed the number of days in month mi. The Olympiads are given in the arbitrary order. Several Olympiads can take place in one day.

Use the modern (Gregorian) calendar in the solution. Note that all dates are given in the year 2013. This is not a leap year, so February has 28 days. Please note, the preparation of some Olympiad can start in 2012 year.

Output

Print a single number — the minimum jury size.

Sample test(s)
input
2
5 23 1 2
3 13 2 3
output
2
input
3
12 9 2 1
12 8 1 3
12 8 2 2
output
3
input
1
1 10 1 13
output
1


题意:染色问题,求被覆盖区间内的最大值,可能会到2012年,做复杂了。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int a[366];
int b[367];
int m,d,r,t;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int dd(int m,int d)
{
    int ans=0;
    for(int i=0;i<m;i++)
    {
        ans+=month[i];
    }
    ans+=d;
    return ans;
}
int main()
{
    int cases;
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    scanf("%d",&cases);
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    for(int i=1;i<=cases;i++)
    {//9.3 9.8  6day   12.9
        scanf("%d%d%d%d",&m,&d,&r,&t);
        int today=dd(m,d);
        int tag=0;
        if(today<=t)
        {
            //printf("nimei\n");
            tag=1;
            t=t-today;
        }
        if(tag)
        {
            for(int i=366;i>=366-t;i--)
            {
                b[i]+=r;
            }
            for(int i=1;i<=today-1;i++)
                a[i]+=r;
        }
        else
        for(int i=today-t;i<=today-1;i++)
        {
            a[i]+=r;
        }
    }
    int ans=0;
    for(int i=0;i<=365;i++)
    {
        ans=max(ans,a[i]);
    }
    for(int i=0;i<=366;i++)
    {
        ans=max(ans,b[i]);
    }
    //printf("b[366]=%d\n",b[366]);
    //printf("a[1]=%d\n",a[1]);
    printf("%d\n",ans);
    return 0;
}


C. Anagram
time limit per test
1 second
memory limit per test
256 megabytes
input
input.txt
output
output.txt

String x is an anagram of string y, if we can rearrange the letters in string x and get exact string y. For example, strings "DOG" and "GOD" are anagrams, so are strings "BABA" and "AABB", but strings "ABBAC" and "CAABA" are not.

You are given two strings s and t of the same length, consisting of uppercase English letters. You need to get the anagram of string tfrom string s. You are permitted to perform the replacing operation: every operation is replacing some character from the string s by any other character. Get the anagram of string t in the least number of replacing operations. If you can get multiple anagrams of string t in the least number of operations, get the lexicographically minimal one.

The lexicographic order of strings is the familiar to us "dictionary" order. Formally, the string p of length n is lexicographically smaller than string q of the same length, if p1 = q1p2 = q2, ..., pk - 1 = qk - 1pk < qk for some k (1 ≤ k ≤ n). Here characters in the strings are numbered from 1. The characters of the strings are compared in the alphabetic order.

Input

The input consists of two lines. The first line contains string s, the second line contains string t. The strings have the same length (from 1to 105 characters) and consist of uppercase English letters.

Output

In the first line print z — the minimum number of replacement operations, needed to get an anagram of string t from string s. In the second line print the lexicographically minimum anagram that could be obtained in z operations.

Sample test(s)
input
ABA
CBA
output
1
ABC
input
CDBABC
ADCABD
output
2
ADBADC
Note

The second sample has eight anagrams of string t, that can be obtained from string s by replacing exactly two letters: "ADBADC", "ADDABC", "CDAABD", "CDBAAD", "CDBADA", "CDDABA", "DDAABC", "DDBAAC". These anagrams are listed in the lexicographical order. The lexicographically minimum anagram is "ADBADC".


题意:给你两个字符串,按要求,相同位上的字母不变,不相同的尽量少操作的变换成第二种,并且按字典序改变。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
string s1,s2;
int a[26];
int b[26];
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    cin>>s1>>s2;
    for(int i=0;i<s1.size();i++)
        a[s1[i]-'A']++;
    for(int i=0;i<s2.size();i++)
        b[s2[i]-'A']++;
    string s;
    for(int i=0;i<26;i++)
    {
        int mm=b[i]-a[i];
        if(mm>=0)
            s+=string(mm,i+'A');
        a[i]-=b[i];
    }
    cout<<s.size()<<endl;
    int tt=0;
    for(int i=0;i<s1.size();i++)
    {
        if(a[s1[i]-'A']>0)
        {
            if(s1[i]>s[tt])
            {
                a[s1[i]-'A']--;
                s1[i]=s[tt];
                tt++;
            }
            else
            {
                if(b[s1[i]-'A']>0)
                    b[s1[i]-'A']--;
                else
                {
                    a[s1[i]-'A']--;
                    s1[i]=s[tt];
                    tt++;
                }
            }
        }
    }
    cout<<s1<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值