2023年百度之星程序设计竞赛初赛1题解
每次出题都出其不意---->群友蓝桥国三,百度之星ac一道题
根据官方的视频题解整理
依据难度的划分
第五题:促销糖果

分析:从答案出发想吃K个糖果,必定有k个糖纸,考虑换购,则有一张糖纸是不可以换的(因为你必须至少要买一颗糖果需要特判
ac代码
java
import java.util.Scanner; import java.util.ArrayList; import java.io.*; import java.util.StringTokenizer; public class Main{ static BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer tokenizer=new StringTokenizer(""); static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double Double() throws IOException { return Double.parseDouble(next()); } static long nextlong() throws IOException { return Long.parseLong(next()); } public static void main(String[] args)throws IOException { ArrayList<Long>ans=new ArrayList<>(); long t=nextlong(); while(t-->0) { long p=nextlong(); long k=nextlong(); if(k==0) { ans.add(k); } else{ k-=(k-1)/p; ans.add(k); } } for(Long i:ans) { pw.println(i); } pw.flush(); } }
c++
#include <iostream> #include <vector> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t; cin >> t; vector<long long> ans; while (t--) { long long p, k; cin >> p >> k; if (k == 0) { ans.push_back(k); } else { k -= (k - 1) / p; ans.push_back(k); } } for (long long i : ans) { cout << i << '\n'; } cout.flush(); return 0; }
第一题公园

思路 :T 和F汇合与点g然后一起到达N,用三段dfs.然后枚举三段值求最小
java 代码
import java.util.Scanner; import java.util.ArrayList; import java.util.Arrays; public class Main{ static int a, b, c; static int s1, s2, n, m; static ArrayList<Integer>[] son = new ArrayList[40010]; static int[][] dis = new int[3][40010]; static int[] que = new int[40010]; public static void main(String[] args) { Scanner input = new Scanner(System.in); a = input.nextInt(); b = input.nextInt(); c = input.nextInt(); s1 = input.nextInt(); s2 = input.nextInt(); n = input.nextInt(); m = input.nextInt(); for (int i = 1; i <= n; i++) { son[i] = new ArrayList<>(); // Initialize each ArrayList } for (int i = 1; i <= m; i++) { int u = input.nextInt(); int v = input.nextInt(); son[u].add(v); son[v].add(u); } calc(dis[0], s1); calc(dis[1], s2); calc(dis[2], n); long ans = Long.MAX_VALUE; // Initialize ans to maximum value for (int i = 1; i <= n; i++) { if (dis[0][i] != -1 && dis[1][i] != -1 && dis[2][i] != -1) { ans = Math.min(ans, 1L * dis[0][i] * a + 1L * dis[1][i] * b + 1L * dis[2][i] * (a + b - c)); } } long op = ans == Long.MAX_VALUE ? -1 : ans; // Use Long.MAX_VALUE for comparison System.out.println(op); } public static void calc(int[] dis, int s) { int tall = 1; Arrays.fill(dis, -1); dis[s] = 0; que[1] = s; for (int i = 1; i <= tall; i++) { int cur = que[i]; for (int v : son[cur]) { if (dis[v] == -1) { dis[v] = dis[cur] + 1; que[++tall] = v; } } } } }
c++代码
#include <iostream> #include <vector> #include <algorithm> #include <climits> using namespace std; const int MAXN = 40010; int a, b, c; int s1, s2, n, m; vector<int> son[MAXN]; int dis[3][MAXN]; int que[MAXN]; void calc(int dis[], int s) { int tall = 1; fill(dis, dis + MAXN, -1); dis[s] = 0; que[1] = s; for (int i = 1; i <= tall; i++) { int cur = que[i]; for (int v : son[cur]) { if (dis[v] == -1) { dis[v] = dis[cur] + 1; que[++tall] = v; } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> a >> b >> c >> s1 >> s2 >> n >> m; for (int i = 1; i <= n; i++) { son[i].clear(); // Clear each vector } for (int i = 1; i <= m; i++) { int u, v; cin >> u >> v; son[u].push_back(v); son[v].push_back(u); } calc(dis[0], s1); calc(dis[1], s2); calc(dis[2], n); long long ans = LLONG_MAX; // Initialize ans to maximum value for (int i = 1; i <= n; i++) { if (dis[0][i] != -1 && dis[1][i] != -1 && dis[2][i] != -1) { ans = min(ans, 1LL * dis[0][i] * a + 1LL * dis[1][i] * b + 1LL * dis[2][i] * (a + b - c)); } } long long op = (ans == LLONG_MAX) ? -1 : ans; // Use LLONG_MAX for comparison cout << op << '\n'; return 0; }

思路二分答案:
AC代码 java
import java.io.*; import java.util.StringTokenizer; import java.math.BigInteger; public class Main { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); static StringTokenizer tokenizer = new StringTokenizer(""); static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static BigInteger nextBigInteger() throws IOException { return new BigInteger(next()); } static int n; static int m; static int[] s = new int[100010]; static int[] v = new int[100010]; public static void main(String[] args) throws IOException { n = nextInt(); m = nextInt(); for (int i = 1; i <= n; i++) { s[i] = nextInt(); v[i] = nextInt(); } BigInteger l = BigInteger.ONE; BigInteger r = BigInteger.valueOf(Long.MAX_VALUE); BigInteger ans = BigInteger.valueOf(-1); while (l.compareTo(r) <= 0) { BigInteger mid = l.add(r).shiftRight(1); // (l + r) / 2 if (cheak(mid)) { ans = mid; r = mid.subtract(BigInteger.ONE); } else { l = mid.add(BigInteger.ONE); } } pw.println(ans); pw.flush(); } public static boolean cheak(BigInteger t) { BigInteger sum = BigInteger.ZERO; BigInteger max = BigInteger.ZERO; for (int i = 1; i <= n; i++) { BigInteger cur = t.subtract(BigInteger.valueOf(s[i])).multiply(BigInteger.valueOf(v[i])).max(BigInteger.ZERO); max = max.max(cur); sum = sum.add(cur); } return sum.subtract(max).compareTo(BigInteger.valueOf(m)) >= 0; } }
本文来自博客园,作者:LILi2209,转载请注明原文链接:https://www.cnblogs.com/Lili-202209/p/17615261.html

浙公网安备 33010602011771号