1. 程式人生 > >Infinite Fraction Path

Infinite Fraction Path

原題傳送門

題意:

給定n個點個點權(0~9),每個點i可以去到(i*i+1)%n的點,要求走n步,使得走過的點權組成的字串字典序最大

思路:BFS + 貪心剪枝(優先佇列)

寬搜最大點權的點為起點,存在兩種剪枝方案

  1. 答案字串的第 i 的位置已經放置了更大的點權

  2. 由於是多個起點寬搜而去,因此存在兩條路徑搜到了同一個點,但是兩條路徑把對當前點的放置位置不一樣,這個時候需要明確一點,把當前點放得更後面的答案更優,例如一個樣例

9 -> 9 -> 9 -> 8

在這裡我們一開始儲存進佇列裡面的就是三個9,但是第一個9為起點的話,8的位置就是在第四,而如果是第三個9為起點的話,8的位置就是第二,如果是先從第一個9去更新答案進而抵達8,那麼8的last(最後放置答案裡面位置) = 4,但是我們的優先佇列裡面還存在這以第三個9作為起點,取出這個9更新8的時候,發現以這樣更新8的話,放置在答案位置是2,很明顯,前者更優,因此能夠放後面就肯定是更優的

AC程式碼:

#include <bits/stdc++.h>
using namespace std;

const int N = 1.5e5 + 9;

int a[N], to[N], last[N], ans[N];
// 點權, 邊, 最後一次放置在答案中的位置, 答案序列
char s[N];
int T, n;

struct node{
	int num, pos, id;
	// 數字,放置在答案的位置,下標
	node(){}
	node(int num, int pos, int id) : num(num), pos(pos), id(id) {}
	// 優先佇列中,數字大的,在答案中的位置靠前的 
bool operator < (const node & rhs) const { if (num != rhs.num) return num < rhs.num; return pos > rhs.pos; } }; void sovle() { scanf("%d%s", &n, s); int ma = 0; for (long long i = 0; i < n; i++) { a[i] = s[i] - '0'; to[i] = (i * i + 1) % n; ma = max(ma, a[i]); } memset
(ans, -1, sizeof ans); memset(last, 0, sizeof last); priority_queue<node> q; for (int i = 0; i < n; i++) { if (a[i] == ma) { q.push(node(ma, 1, i)); } } while (!q.empty()) { node t = q.top(); q.pop(); if (ans[t.pos] == -1) ans[t.pos] = t.num; if (ans[t.pos] > t.num) continue ; // 當前位置已經放置了更大的答案,剪枝 if (t.pos <= last[t.id])continue ; // 如果這個數字已經被放置在更後面了,那種情況更優,剪枝 if (t.pos == n) continue ; last[t.id] = t.pos; q.push(node(a[to[t.id]], t.pos + 1, to[t.id])); } for (int i = 1; i <= n; i++) { putchar(ans[i] + '0'); } puts(""); } int main() { // freopen ("1.txt", "r", stdin); scanf ("%d", &T); for (int ca = 1; ca <= T; ca ++) { printf ("Case #%d: ", ca); sovle(); } return 0; }

相關推薦

HDU 6223 Infinite Fraction Path(BFS+剪枝)

優先 val == d+ mil 如果 inf 2.6 ring The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solic

hdu6223 Infinite Fraction Path 2017沈陽區域賽G題 bfs加剪枝(好題)

getc 細節 \n ret ast scrip mat tom ide 題目傳送門 題目大意:給出n座城市,每個城市都有一個0到9的val,城市的編號是從0到n-1,從i位置出發,只能走到(i*i+1)%n這個位置,從任意起點開始,每走一步都會得到一個數字,走n-1步,會

ACM-ICPC 2017 沈陽賽區現場賽 G. Infinite Fraction Path && HDU 6223

沈陽 end cto fde div pid tails nod tps 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=6223 參考題解:https://blog.csdn.net/qq_40482495/article/d

hdu 6223 Infinite Fraction Path

code int targe rec 堆棧 sign sca color pop 題目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6223 題意:給定長度為n的一串數字S,現在要按照一種規則尋找長度為n的數字串,使得該數字串的字

Infinite Fraction Path(HDU6223 + bfs + 剪枝)

都是 deb print ins fin ini efi math debug 題目鏈接:   http://acm.hdu.edu.cn/showproblem.php?pid=6223 題目: 題意:   給你一個長度為n的數字串,開始時你選擇一個位置(記為

HDU6223——2017ICPC徐州G Infinite Fraction Path

.html 數列 tps -s img sin c++11 type 字典序 題意:   給定一個數字串,每個位子都能向(i*i+1)%n的位子轉移,輸出路徑上,字典序最大的,長度為n的串。 參考:https://www.cnblogs.com/mountaink/p/

HDU6223——2017ICPC徐州G Infinite Fraction Path

題意:   給定一個數字串,每個位子都能向(i*i+1)%n的位子轉移,輸出路徑上,字典序最大的,長度為n的串。 參考:https://www.cnblogs.com/mountaink/p/9541442.html 思路:   BFS,   一個數字肯定是最高位越大,這個數字本身就越大,所以肯定第

hdu6223 Infinite Fraction Path【bfs+剪枝】

先提取出最大的值放進優先佇列裡面 優先佇列先按步數小的先排,步數相同的按值大的優先 剪枝: 當前位的值比隊列出來的值大的直接忽略; 但前走的下標已經被走過了且當前步數小於最大步數直接忽略。 A

HDU-6223 Infinite Fraction Path(2017ACM/ICPC亞洲區瀋陽站)

思路:BFS+剪枝。對於每一層,找其最大值mm,對於小於mm的點和找的可能為同一個點進行剪枝。 Code: #include<iostream> #include<cstd

HDU - 6223 Infinite Fraction Path

Infinite Fraction Path 題 意:輸入一個n,一個長度為n的串,串的字元都是0-9之間,下標為i的字元只能到下標為(i*i+1)%n的位置。問從從那個下標出發,能使路徑字串的字典序最大。 資料範圍:

6223 Infinite Fraction Path——剪枝

對於兩個位置i,j,他們的下一個位置分別為pi = (i*i+1)%n, pj = (j*j+1)%n, pi==pj的概率很高,因此只要把這種情況減掉就能過了 #include <cstdio

HDU 6223 -Infinite Fraction Path(BFS+剪枝)

【題目連結】http://acm.hdu.edu.cn/showproblem.php?pid=6223 【題意】 給一個長度為 n n

Infinite Fraction Path

原題傳送門 題意: 給定n個點個點權(0~9),每個點i可以去到(i*i+1)%n的點,要求走n步,使得走過的點權組成的字串字典序最大 思路:BFS + 貪心剪枝(優先佇列) 寬搜最大點權的點為起點,存在兩種剪枝方案 答案字串的第 i 的位置已經放置了更大的

hdu6223Infinite Fraction Path(2017ACM/ICPC亞洲區瀋陽站G題) 【BFS+剪枝】

傳送門:http://acm.hdu.edu.cn/showproblem.php?pid=6223 The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers

Node.js Path 模塊

工具 詳細 模塊 module tro ebp dex ble put var path = require(‘path‘); module.exports = { entry: ‘./app/index.js‘, output: { filename:

java.io.FileNotFoundException: class path resource ..cannot be opened because it does not exist

java ... mod ons exc pen 方法 except open java.io.FileNotFoundException: class path resource ..cannot be opened because it does not exist

【LeetCode】064. Minimum Path Sum

ive rom right ott path sum 處理 tom ber its 題目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri

Package gtk+-3.0 was not found in the pkg-config search path

path 二進制 all 項目 有時 rpm fedora ack share 問題描述:   在fedora21系統上通過rpmbuild構建fcitx的二進制包時出現以上錯誤,經老程序員指點:“是相應的開發包沒有安裝” 解決辦法:   yum installl gtk3

LeetCode 64. Minimum Path Sum 20170515

tco path sum obj .com 最小 ima minimum type elif Given a m x n grid filled with non-negative numbers, find a path from top left to bottom r

[轉]The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

right clas rup b- row 添加按鈕 n) 1-1 自帶 完整錯誤信息: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS"AS IS" AND ANY EXPRESS O