码迷,mamicode.com
首页 > 编程语言 > 详细

Codeforces Round #277.5 (Div. 2) JAVA版题解

时间:2014-11-25 01:59:54      阅读:140      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   ar   color   os   sp   




Codeforces Round #277.5 (Div. 2)
A. SwapSort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

In this problem your goal is to sort an array consisting of n integers in at most n swaps. For the given array find the sequence of swaps that makes the array sorted in the non-descending order. Swaps are performed consecutively, one after another.

Note that in this problem you do not have to minimize the number of swaps — your task is to find any sequence that is no longer than n.

Input

The first line of the input contains integer n (1?≤?n?≤?3000) — the number of array elements. The second line contains elements of array:a0,?a1,?...,?an?-?1 (?-?109?≤?ai?≤?109), where ai is the i-th element of the array. The elements are numerated from 0 to n?-?1 from left to right. Some integers may appear in the array more than once.

Output

In the first line print k (0?≤?k?≤?n) — the number of swaps. Next k lines must contain the descriptions of the k swaps, one per line. Each swap should be printed as a pair of integers ij (0?≤?i,?j?≤?n?-?1), representing the swap of elements ai and aj. You can print indices in the pairs in any order. The swaps are performed in the order they appear in the output, from the first to the last. It is allowed to print i?=?j and swap the same pair of elements multiple times.

If there are multiple answers, print any of them. It is guaranteed that at least one answer exists.

Sample test(s)
input
5
5 2 5 1 4
output
2
0 3
4 2
input
6
10 20 20 40 60 60
output
0
input
2
101 100
output
1
0 1
简单排序...
/**
 * Created by ckboss on 14-11-20.
 */
import java.util.*;

public class CF489A {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        int[] a = new int[n];
        for(int i=0;i<n;i++){
            a[i]=in.nextInt();
        }
        List<Integer> li = null;
        li = new ArrayList<Integer>();
        for(int i=0;i<n;i++){
            int pos=0;
            for(int j=0;j<n-i;j++){
                if(a[j]>=a[pos]){
                    pos=j;
                }
            }
            if(pos==n-i-1) continue;
            int t=a[pos]; a[pos]=a[n-i-1]; a[n-i-1]=t;
            li.add(pos); li.add(n-i-1);
        }
        System.out.println(li.size()/2);
        for(int i=0;i<li.size();i+=2){
            System.out.println(li.get(i)+" "+li.get(i + 1));
        }
    }
}



B. BerSU Ball
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The Berland State University is hosting a ballroom dance in celebration of its 100500-th anniversary! n boys and m girls are already busy rehearsing waltz, minuet, polonaise and quadrille moves.

We know that several boy&girl pairs are going to be invited to the ball. However, the partners‘ dancing skill in each pair must differ by at most one.

For each boy, we know his dancing skills. Similarly, for each girl we know her dancing skills. Write a code that can determine the largest possible number of pairs that can be formed from n boys and m girls.

Input

The first line contains an integer n (1?≤?n?≤?100) — the number of boys. The second line contains sequence a1,?a2,?...,?an (1?≤?ai?≤?100), where ai is the i-th boy‘s dancing skill.

Similarly, the third line contains an integer m (1?≤?m?≤?100) — the number of girls. The fourth line contains sequence b1,?b2,?...,?bm (1?≤?bj?≤?100), where bj is the j-th girl‘s dancing skill.

Output

Print a single number — the required maximum possible number of pairs.

Sample test(s)
input
4
1 4 6 2
5
5 1 5 7 9
output
3
input
4
1 2 3 4
4
10 11 12 13
output
0
input
5
1 1 1 1 1
3
1 2 3
output
2
二分图最大匹配:
/**
 * Created by ckboss on 14-11-21.
 */
import java.util.*;

public class CF489B {

    static final int maxn=222;

    static int N,n,m;

    static int[] linker = new int[maxn];
    static boolean[] used = new boolean[maxn];

    static int[] Adj = new int[maxn];
    static int Size;

    static class Edge{
        int to,next;
    }

    static Edge[] edge = new Edge[maxn*maxn];

    static void init(){
        Arrays.fill(Adj,-1);
        Size=0;
    }

    static void Add_Edge(int u,int v){
        edge[Size] = new Edge();
        edge[Size].to=v;
        edge[Size].next=Adj[u];
        Adj[u]=Size++;
    }


    static boolean dfs(int u){
        for(int i=Adj[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(used[v]==false){
                used[v]=true;
                if(linker[v]==-1||dfs(linker[v])==true){
                    linker[v]=u;
                    return true;
                }
            }
        }
        return false;
    }

    static int hungary(){
        int ret=0;
        Arrays.fill(linker,-1);
        for(int u=0;u<n;u++){
            Arrays.fill(used,false);
            if(dfs(u)) {
                ret++;
            }
        }
        return ret;
    }

    static int[] boys = new int[maxn];
    static int[] girls = new int[maxn];

    public static void main(String[] args){

        Scanner in = new Scanner(System.in);

        n=in.nextInt();
        for(int i=0;i<n;i++){
            boys[i]=in.nextInt();
        }

        m=in.nextInt();
        for(int i=0;i<m;i++){
            girls[i]=in.nextInt();
        }

        N=Math.max(n,m);

        init();

        for(int i=0;i<N;i++){
            if(i>=n) continue;
            for(int j=0;j<N;j++){
                if(j>=m) continue;
                if(Math.abs(boys[i]-girls[j])>1) continue;
                Add_Edge(i,j);
            }
        }

        int ans=hungary();

        System.out.println(ans);
    }
}


C. Given Length and Sum of Digits...
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base without leading zeroes.

Input

The single line of the input contains a pair of integers ms (1?≤?m?≤?100,?0?≤?s?≤?900) — the length and the sum of the digits of the required numbers.

Output

In the output print the pair of the required non-negative integer numbers — first the minimum possible number, then — the maximum possible number. If no numbers satisfying conditions required exist, print the pair of numbers "-1 -1" (without the quotes).

Sample test(s)
input
2 15
output
69 96
input
3 0
output
-1 -1
贪心:
/**
 * Created by ckboss on 14-11-21.
 */
import java.util.*;

public class CF489C {

    int n,s;
    int[] minNum = new int[999];
    int[] maxNum = new int[999];

    void getminNum(){
        int sum=1;
        minNum[0]=1;
        int pos=n-1;
        while(sum<s){
            if(minNum[pos]<9){
                sum++;
                minNum[pos]++;
            }
            else pos--;
        }
    }

    void getmaxNum(){
        int sum=1;
        maxNum[0]=1;
        int pos=0;
        while(sum<s){
            if(maxNum[pos]<9){
                sum++;
                maxNum[pos]++;
            }
            else pos++;
        }
    }

    void Solve(){
        Scanner in = new Scanner(System.in);
        n=in.nextInt(); s=in.nextInt();
        if(s>n*9){
            System.out.println("-1 -1");
        }
        else if(s==0){
            if(n==1){
                System.out.println("0 0");
            }
            else{
                System.out.println("-1 -1");
            }
        }
        else{
            getminNum(); getmaxNum();
            for(int i=0;i<n;i++){
                System.out.printf("%d",minNum[i]);
            }
            System.out.print(" ");
            for(int i=0;i<n;i++){
                System.out.printf("%d",maxNum[i]);
            }
        }
    }
    CF489C(){
        Solve();
    }
    public static void main(String[] args){
        new CF489C();
    }
}


D. Unbearable Controversy of Being
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Tomash keeps wandering off and getting lost while he is walking along the streets of Berland. It‘s no surprise! In his home town, for any pair of intersections there is exactly one way to walk from one intersection to the other one. The capital of Berland is very different!

Tomash has noticed that even simple cases of ambiguity confuse him. So, when he sees a group of four distinct intersections abc and d, such that there are two paths from a to c — one through b and the other one through d, he calls the group a "damn rhombus". Note that pairs (a,?b)(b,?c),(a,?d)(d,?c) should be directly connected by the roads. Schematically, a damn rhombus is shown on the figure below:

bubuko.com,布布扣

Other roads between any of the intersections don‘t make the rhombus any more appealing to Tomash, so the four intersections remain a "damn rhombus" for him.

Given that the capital of Berland has n intersections and m roads and all roads are unidirectional and are known in advance, find the number of "damn rhombi" in the city.

When rhombi are compared, the order of intersections b and d doesn‘t matter.

Input

The first line of the input contains a pair of integers nm (1?≤?n?≤?3000,?0?≤?m?≤?30000) — the number of intersections and roads, respectively. Nextm lines list the roads, one per line. Each of the roads is given by a pair of integers ai,?bi (1?≤?ai,?bi?≤?n;ai?≠?bi) — the number of the intersection it goes out from and the number of the intersection it leads to. Between a pair of intersections there is at most one road in each of the two directions.

It is not guaranteed that you can get from any intersection to any other one.

Output

Print the required number of "damn rhombi".

Sample test(s)
input
5 4
1 2
2 3
1 4
4 3
output
1
input
4 12
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
output
12
暴力DFS
/**
 * Created by ckboss on 14-11-21.
 */
import java.util.*;

public class CF489D {

    final int maxn=3300;

    class Edge{
        int to,next;
    }

    Edge[] edge = new Edge[maxn*10];
    int[] Adj = new int[maxn];
    int Size;

    void init(){
        Size=0; Arrays.fill(Adj,-1);
    }

    void Add_Edge(int u,int v){
        edge[Size] = new Edge();
        edge[Size].to=v;
        edge[Size].next=Adj[u];
        Adj[u]=Size++;
    }

    int[] degree = new int[maxn];

    int n,m;

    boolean[] vis = new boolean[maxn];
    int[] dps = new int[maxn];

    void dfs(int u,int pre,int deep){
        //System.out.println(pre+" --> "+u+" deep: "+deep);
        if(deep==2) {
            dps[u]++;
            return ;
        }
        for(int i=Adj[u];i!=-1;i=edge[i].next){
            int v=edge[i].to;
            if(v==pre||vis[v]==true) continue;
            vis[v]=true;
            dfs(v,u,deep+1);
        }
    }

    CF489D(){

        Scanner in = new Scanner(System.in);

        n=in.nextInt(); m=in.nextInt();

        init();
        for(int i=0;i<m;i++){
            int u=in.nextInt(); int v= in.nextInt();
            Add_Edge(u,v);
            degree[v]++;
        }

        int ans=0;
        for(int u=1;u<=n;u++){
            if(Adj[u]==-1||edge[Adj[u]].next==-1) continue;
            Arrays.fill(dps,0);
            for(int i=Adj[u];i!=-1;i=edge[i].next){
                Arrays.fill(vis,false);
                vis[u]=true;
                dfs(edge[i].to,u,1);
            }

            for(int i=1;i<=n;i++){
                if(dps[i]>1) {
                    ans+=(dps[i]*(dps[i]-1))/2;
                }
               // System.out.println("u: "+u+"  "+i+" dps: "+dps[i]);
            }
        }

        System.out.println(ans);
    }

    public static void main(String[] args){
        new CF489D();
    }
}


E. Hiking
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A traveler is planning a water hike along the river. He noted the suitable rest points for the night and wrote out their distances from the starting point. Each of these locations is further characterized by its picturesqueness, so for the i-th rest point the distance from the start equals xi, and itspicturesqueness equals bi. The traveler will move down the river in one direction, we can assume that he will start from point 0 on the coordinate axis and rest points are points with coordinates xi.

Every day the traveler wants to cover the distance l. In practice, it turns out that this is not always possible, because he needs to end each day at one of the resting points. In addition, the traveler is choosing between two desires: cover distance l every day and visit the most picturesque places.

Let‘s assume that if the traveler covers distance rj in a day, then he feels frustration bubuko.com,布布扣, and his total frustration over the hike is calculated as the total frustration on all days.

Help him plan the route so as to minimize the relative total frustration: the total frustration divided by the total picturesqueness of all the rest points he used.

The traveler‘s path must end in the farthest rest point.

Input

The first line of the input contains integers n,?l (1?≤?n?≤?1000,?1?≤?l?≤?105) — the number of rest points and the optimal length of one day path.

Then n lines follow, each line describes one rest point as a pair of integers xi,?bi (1?≤?xi,?bi?≤?106). No two rest points have the same xi, the lines are given in the order of strictly increasing xi.

Output

Print the traveler‘s path as a sequence of the numbers of the resting points he used in the order he used them. Number the points from 1 to n in the order of increasing xi. The last printed number must be equal to n.

Sample test(s)
input
5 9
10 10
20 10
30 1
31 5
40 10
output
1 2 4 5 
Note

In the sample test the minimum value of relative total frustration approximately equals 0.097549. This value can be calculated as bubuko.com,布布扣.

01分数规划 求最大值
/**
 * Created by ckboss on 14-11-22.
 */
import java.util.*;

public class CF489E {

    int n,l;
    class Point{
        public int x,b;
    }
    Point[] p = new Point[1100];
    double[] dp = new double[1100];
    int[] pre = new int[1100];

    boolean check(double L){
        Arrays.fill(dp,-1e8);
        Arrays.fill(pre,-1);
        dp[0]=0;
        for(int i=1;i<=n;i++){
            dp[i] = dp[0] + L*p[i].b - Math.sqrt(Math.abs(p[i].x-l));
            pre[i]=0;
            for(int j=1;j<i;j++){
                double r=1.*p[i].x-p[j].x;
                double tr = dp[j] + L*p[i].b - Math.sqrt(Math.abs(r-1.*l));
                if(dp[i]<tr) {
                    dp[i]= Math.max(dp[i], tr);
                    pre[i]=j;
                }
            }
        }
        if(dp[n]>0) return true;
        return false;
    }

    CF489E(){
       Scanner in = new Scanner(System.in);
        n=in.nextInt(); l=in.nextInt();
        for(int i=1;i<=n;i++) {
            p[i]=new Point();
            p[i].x=in.nextInt();
            p[i].b=in.nextInt();
        }
        double low=0,high=1e7,mid = 0,ans = 0;
        while(Math.abs(high-low)>1e-7){
            mid=(low+high)/2;
            if(check(mid)==true){
                ans=high=mid;
            }
            else low=mid;
        }
        check(ans);
        int u=n;
        List<Integer> as = new ArrayList<Integer>();
        as.add(u);
        while(pre[u]!=-1){
            u=pre[u];
            if(u==0) continue;
            as.add(u);
        }
        for(int i=as.size()-1;i>=0;i--)
            System.out.printf("%d ",as.get(i));
    }

    public static void main(String[] args){
        new CF489E();
    }
}


F. Special Matrices
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An n?×?n square matrix is special, if:

  • it is binary, that is, each cell contains either a 0, or a 1;
  • the number of ones in each row and column equals 2.

You are given n and the first m rows of the matrix. Print the number of special n?×?n matrices, such that the first m rows coincide with the given ones.

As the required value can be rather large, print the remainder after dividing the value by the given number mod.

Input

The first line of the input contains three integers nmmod (2?≤?n?≤?5000?≤?m?≤?n2?≤?mod?≤?109). Then m lines follow, each of them contains ncharacters — the first rows of the required special matrices. Each of these lines contains exactly two characters ‘1‘, the rest characters are ‘0‘. Each column of the given m?×?n table contains at most two numbers one.

Output

Print the remainder after dividing the required value by number mod.

Sample test(s)
input
3 1 1000
011
output
2
input
4 4 100500
0110
1010
0101
1001
output
1
Note

For the first test the required matrices are:

011
101
110

011
110
101

In the second test the required matrix is already fully given, so the answer is 1.


DP:

dp[i][j][k] 在第i行还有j列可以填一个值k列可以填2个值

/**
 * Created by ckboss on 14-11-22.
 */

import java.util.*;

public class CF489F {

    final int maxn = 550;
    int n, m;
    long mod;
    int[][] tu = new int[maxn][maxn];
    int[] sum = new int[maxn];
    long[][][] dp = new long[2][maxn][maxn];

    CF489F() {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        mod = in.nextLong();
        for (int i = 1; i <= m; i++) {
            StringBuffer sb = new StringBuffer("");
            sb.append(in.next());
            for (int j = 0; j < n; j++) {
                tu[i][j] = (sb.charAt(j) == '1') ? 1 : 0;
                if(tu[i][j]==1){
                    sum[j]++;
                }
            }
        }

        if(n==m){
            System.out.println("1");
            return ;
        }

        int one = 0, two=0;
        for(int i=0;i<n;i++){
            //System.out.println(" .... "+i);
            sum[i]=2-sum[i];
            if(sum[i]==1) one++;
            else if(sum[i]==2) two++;
        }
        dp[m%2][one][two]=1;

        //System.out.println(m+","+one+","+two);

        long ans=0;
        int now=(m+1)%2,last=m%2;
        for(int i=m+1;i<=n;i++){
           for(one = 0; one<=n;one++){
               for(two=0;two<=n;two++){
                   if(dp[last][one][two]!=0){
                       if(one-2>=0) {
                           dp[now][one - 2][two] = (dp[now][one - 2][two] + dp[last][one][two] * (one * (one - 1) / 2)) % mod;
                       }
                       if(two-2>=0) {
                           dp[now][one+2][two - 2] = (dp[now][one+2][two - 2] + dp[last][one][two] * (two * (two - 1) / 2)) % mod;
                       }
                       if(two-1>=0) {
                           dp[now][one][two - 1] = (dp[now][one][two - 1] + dp[last][one][two] * one * two) % mod;
                       }
                   }
               }
           }
           ans = (ans + dp[now][0][0]) % mod;
           int t = now; now=last; last=t;
        }

        System.out.println(ans);
    }

    public static void main(String[] args) {
        new CF489F();
    }
}




Codeforces Round #277.5 (Div. 2) JAVA版题解

标签:des   style   blog   http   io   ar   color   os   sp   

原文地址:http://blog.csdn.net/ck_boss/article/details/41462363

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!