526. Beautiful Arrangement

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/excellentlizhensbfhw/article/details/81592480

Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 <= i <= N) in this array:

  1. The number at the ith position is divisible by i.
  2. i is divisible by the number at the ith position.

 

Now given N, how many beautiful arrangements can you construct?

Example 1:

Input: 2
Output: 2
Explanation: 

The first beautiful arrangement is [1, 2]:

Number at the 1st position (i=1) is 1, and 1 is divisible by i (i=1).

Number at the 2nd position (i=2) is 2, and 2 is divisible by i (i=2).

The second beautiful arrangement is [2, 1]:

Number at the 1st position (i=1) is 2, and 2 is divisible by i (i=1).

Number at the 2nd position (i=2) is 1, and i (i=2) is divisible by 1.

Note:

  1. N is a positive integer and will not exceed 15.

DFS或回溯算法求解。

在生成全排列的过程中判断是否满足题目要求的两个条件即可,程序如下所示:

class Solution {
    private int cnt = 0;
    private List<Integer> list = new LinkedList<>();
    private boolean[] visited;
    public int countArrangement(int N) {
        visited = new boolean[N+1];
        backTracing(N, 1);
        return cnt;
    }
    public void backTracing(int N, int begin){
        if (begin > N+1){
            return ;
        }
        if (begin == N+1){
            cnt ++;
            return;
        }
        for (int i = 1; i <= N; ++ i){
            if (visited[i]){
                continue;
            }
            if (list.size() != 0 && (i%(list.size()+1)) != 0 && ((list.size()+1) % i) != 0){
                continue;
            }
            visited[i] = true;
            list.add(i);
            backTracing(N, begin+1);
            list.remove(list.size() - 1);
            visited[i] = false;
        }
    }
}

 

阅读更多

Team Arrangement

11-01

Problem DescriptionnBarry Bennett, the coach of the Bings football team, wants to arrange his team for an important match against the Bangs. He decides on the formation he wants to play, for example 4-4-2, meaning that there will be four defenders, four midfielders, and two strikers (and of course, one goalkeeper). Your task is to determine the players who will play. For each available player, we know his role (e.g. midfielder). For each role, the players are selected in ascending order of their numbers. When the players are selected, you must determine the captain too, who is the player with the longest record in the team play. In case two players have the same record, the one with bigger number is chosen. Note that the captain is chosen among the players that are selected in the arrange.n nnInputnThe input consists of multiple test cases. The first 22 lines of each test case contain the data for the 22 available players in this format: nnnumber name role year1–year'1 year2–year'2 ...nnnumber is the player number (unique positive integer less than 100). name is a string of at most 20 letters. role is a single character among G, D, M, S, for goalkeeper, defender, midfielder, and striker respectively. Each yeari –year'i pair (yeari ≤ year'i) shows the player has been a member of the team between the specified years (inclusive). The years are in four-digit format. There is at least one and at most 20 such pairs, and the same year is not repeated more than once in the list. There is a 23rd line describing the desired formation, like 4-4-2 in that format. Note that there are only three numbers in the formation (so, 4-3-2-1 is not valid), none of them is zero, and their sum is always 10. The input is terminated by a line containing a single 0.n nnOutputnFor each test case, output a list of 11 players chosen in the arrange. Each line must contain the player number, his name and his role, separated by single blank characters. The players must be sorted according to their role, in the order of goalkeeper, defenders, midfielders, and strikers. The players with the same role are sorted according to ascending order of their numbers. There is an exception that the captain always comes as the first player in the entire list. If it is not possible to arrange the team conforming to the desired formation, write a single line containing IMPOSSIBLE TO ARRANGE in the output. There should be a blank line after the output for each test case.n nnSample Inputn9 PlayerA M 2000-2001 2003-2006n2 PlayerB M 2004-2006n10 PlayerC D 2001-2005n1 PlayerD D 2000-2001 2002-2004n11 PlayerE S 2003-2006n8 PlayerF M 2005-2006n22 PlayerG S 2005-2006n25 PlayerH G 2000-2001 2002-2003 2005-2006n6 PlayerI D 2003-2006n26 PlayerJ D 2003-2004 2000-2001n18 PlayerK M 2003-2004n19 PlayerL M 2000-2001 2003-2006n7 PlayerM S 2003-2006 1999-2001n21 PlayerN S 2003-2006n13 PlayerO S 2005-2006n15 PlayerP G 2001-2006n14 PlayerQ D 2003-2004n5 PlayerR S 2000-2005n20 PlayerS G 2000-2002 2003-2003n12 PlayerT M 2004-2005n3 PlayerU D 2000-2005n4 PlayerV M 2001-2004n4-4-2n0n nnSample Outputn7 PlayerM Sn15 PlayerP Gn1 PlayerD Dn3 PlayerU Dn6 PlayerI Dn10 PlayerC Dn2 PlayerB Mn4 PlayerV Mn8 PlayerF Mn9 PlayerA Mn5 PlayerR Sn n

Instrction Arrangement

02-12

问题描述 :nnnAli has taken the Computer Organization and Architecture course this term. He learned that there may be dependence between instructions, like WAR (write after read), WAW, RAW.nIf the distance between two instructions is less than the Safe Distance, it will result in hazard, which may cause wrong result. So we need to design special circuit to eliminate hazard. However the most simple way to solve this problem is to add bubbles (useless operation), which means wasting time to ensure that the distance between two instructions is not smaller than the Safe Distance.nThe definition of the distance between two instructions is the difference between their beginning times.nNow we have many instructions, and we know the dependent relations and Safe Distances between instructions. We also have a very strong CPU with infinite number of cores, so you can run as many instructions as you want simultaneity, and the CPU is so fast that it just cost 1ns to finish any instruction.nYour job is to rearrange the instructions so that the CPU can finish all the instructions using minimum time. n输入:nnnThe input consists several testcases.nThe first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.nThe following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N – 1. n输出:nnnThe input consists several testcases.nThe first line has two integers N, M (N <= 1000, M <= 10000), means that there are N instructions and M dependent relations.nThe following M lines, each contains three integers X, Y , Z, means the Safe Distance between X and Y is Z, and Y should run after X. The instructions are numbered from 0 to N – 1.n样例输入:nn5 2n1 2 1n3 4 1n样例输出:nn2nHintnnIn the 1st ns, instruction 0, 1 and 3 are executed;nIn the 2nd ns, instruction 2 and 4 are executed.nSo the answer should be 2.

没有更多推荐了,返回首页