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 from1 to2n. We'll denote the number that is written on a card with numberi, asai. 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 of2n positive integersa1, 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 printn 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
用STL乱搞
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;

int b[600005];
vector<int> v[700005];
int main()
{
    freopen("input","r",stdin);
    freopen("output","w",stdout);
    int n,i,cur,j;
    while(cin>>n)
    {
        int flag=0;
        for(i=1;i<=2*n;i++)
        {
            cin>>b[i];
            v[b[i]].push_back(i);
        }
        for(i=1;i<=5000;i++)
        {
            int s=v[i].size();
            if(s%2==1)
            {
                flag=1;break;
            }
        }
        if(flag)
           cout<<"-1"<<endl;
        else
        {
            for(i=1;i<=5000;i++)
            {
                int l=v[i].size();
                for(int k=0;k<l;k+=2)
                     cout<<v[i][k]<<" "<<v[i][k+1]<<endl;
            }
        }
    }
    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 ton. 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 numberi should be prepared bypi people forti 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 followingn lines contains four integersmi,di,pi andti — the month and day of the Olympiad (given without leading zeroes), the needed number of the jury members and the time needed to prepare thei-th Olympiad (1 ≤ mi ≤ 12,di ≥ 1,1 ≤ pi, ti ≤ 100),di doesn't exceed the number of days in monthmi. 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
因为数据范围不大,直接把每个日期是改年的第几天,然后之前不准备的日子填上人数。最后扫一遍就好。
#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int day[1000];
int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    while(cin>>n)
    {
        memset(day,0,sizeof(day));
        int m,d,p,t;
        for(int i=1;i<=n;i++)
        {
            cin>>m>>d>>p>>t;
            for(int j=1;j<m;j++)
                 d+=month[j];
            while(t--)
            day[d-1+200-t]+=p;
        }
        int ans=0;
        for(int i=0;i<1000;i++)
            ans=max(ans,day[i]);
        cout<<ans<<endl;
    }
    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 stringx and get exact stringy. 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 stringt from strings. You are permitted to perform the replacing operation: every operation is replacing some character from the strings by any other character. Get the anagram of stringt in the least number of replacing operations. If you can get multiple anagrams of stringt 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 stringp of lengthn is lexicographically smaller than stringq of the same length, ifp1 = q1,p2 = q2, ...,pk - 1 = qk - 1,pk < qk for somek (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 (from1 to105 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 stringt from strings. In the second line print the lexicographically minimum anagram that could be obtained inz 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 strings 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".

首先统计每个字符串每种字符的个数。那每种字符串的个数只差的一半(因为算了两次)就是修改的字符数
当S字符串比目标字符串的某个字符有多时,就从A开始扫,判断S字符串比目标字符串少的字符,因为要按字典序,所以要做到尽可能的在字符的前面改变。
接着把得到的字符赋给string s,再将个数减少。知道整个字符串比完为止。
#include<iostream>
#include<string>
#include<cmath>
#include<stdio.h>
using namespace std;
string s1,s2;
int a[500],b[500],ans;
int f(int a)
{
    return a>0?a:-a;
}
int main()
{
 //   freopen("input.txt","r",stdin);
 //   freopen("output.txt","w",stdout);

    while(cin>>s1>>s2)
    {
    ans=0;
    for(int i=0;i<s1.length();i++)
    {
        a[s1[i]]++;
        b[s2[i]]++;
    }

    for(int i=0;i<s1.length();i++)
    {
        ans+=f(a[s1[i]]-b[s2[i]]);
    }

    string s=s1;

    for(int i=0;i<s.length();i++)
    {
        int c=s[i];
       // cout<<c<<endl;
        if(a[c]>b[c])
        {
            for(int j='A';j<='Z';j++)
            {

                if(a[j]<b[j])
                {
                    if(j<c||b[c]==0)
                    {
                        s[i]=j;
                        b[j]--;
                    }
                    else
                        b[c]--;
                    break;
                }
            }
            a[c]--;
        }
    }

    cout<<ans/2<<endl<<s<<endl;
    }
    return 0;

}

/*
CDBABC
ADCABD
2
ADBADC
*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值