Пример #1
0
        static void Method(string[] args)
        {
            int n = ReadInt();

            int[] array = ReadInts();
            for (int i = 0; i < n; i++)
            {
                array[i] = Min(array[i], i + 1);
            }

            int res    = 0;
            int tmpNow = 0;
            GeneralPriorityQueue <int> queue = new GeneralPriorityQueue <int>();

            for (int i = n - 1; i >= 0; i--)
            {
                queue.Enqueue(-array[i]);
                if (tmpNow < n - i)
                {
                    res++;
                    tmpNow += -queue.Dequeue();
                }
            }

            WriteLine(res);
        }
Пример #2
0
        static List <int[]> AstarSearch(bool[,] grid, int sX, int sY, int gX, int gY)
        {
            var opens = new GeneralPriorityQueue <NodeInfo>();

            opens.Enqueue(new NodeInfo(sX, sY, null, 0, Abs(gX - sX) + Abs(gY - sY)));
            var closed = new List <NodeInfo>();

            int[] dx = new int[4] {
                1, 0, -1, 0
            };
            int[] dy = new int[4] {
                0, 1, 0, -1
            };
            int h = grid.GetLength(0);
            int w = grid.GetLength(1);

            bool[,] visited = new bool[h, w];
            while (opens.Count > 0)
            {
                NodeInfo now = opens.Dequeue();
                if (visited[now.y, now.x])
                {
                    continue;
                }

                visited[now.y, now.x] = true;
                closed.Add(now);
                if (now.x == gX && now.y == gY)
                {
                    break;
                }

                for (int i = 0; i < 4; i++)
                {
                    int toX = now.x + dx[i];
                    int toY = now.y + dy[i];
                    if (toX < 0 || toX >= w || toY < 0 || toY >= h)
                    {
                        continue;
                    }
                    if (visited[toY, toX])
                    {
                        continue;
                    }
                    if (!grid[toY, toX])
                    {
                        continue;
                    }

                    int nextH = Abs(toX - gX) + Abs(toY - gY);
                    opens.Enqueue(new NodeInfo(toX, toY, now, now.c + 1, nextH));
                }
            }
            List <int[]> path    = new List <int[]>();
            NodeInfo     current = closed[closed.Count - 1];

            if (!(current.x == gX && current.y == gY))
            {
                return(path);
            }
            path.Add(new int[2] {
                current.x, current.y
            });
            while (current.parent != null)
            {
                current = current.parent;
                path.Add(new int[2] {
                    current.x, current.y
                });
            }
            path.Reverse();
            return(path);
        }
Пример #3
0
        static void Method(string[] args)
        {
            int[] nm = ReadInts();
            int   n  = nm[0];
            int   m  = nm[1];

            int[][] abs = new int[m][];
            for (int i = 0; i < m; i++)
            {
                abs[i] = ReadInts();
            }

            List <int>[] forwardGraph = new List <int> [n];
            for (int i = 0; i < n; i++)
            {
                forwardGraph[i] = new List <int>();
            }

            int[] remainParents = new int[n];
            for (int i = 0; i < m; i++)
            {
                forwardGraph[abs[i][0] - 1].Add(abs[i][1] - 1);
                remainParents[abs[i][1] - 1]++;
            }

            GeneralPriorityQueue <int> que = new GeneralPriorityQueue <int>();

            for (int i = 0; i < n; i++)
            {
                if (remainParents[i] == 0)
                {
                    que.Enqueue(i);
                }
            }

            List <int> resList = new List <int>();

            while (que.Count > 0)
            {
                int now = que.Dequeue();
                resList.Add(now + 1);

                foreach (int child in forwardGraph[now])
                {
                    remainParents[child]--;
                    if (remainParents[child] == 0)
                    {
                        que.Enqueue(child);
                    }
                }
            }

            if (resList.Count == n)
            {
                WriteLine(string.Join(" ", resList));
            }
            else
            {
                WriteLine("-1");
            }
        }