CF1256(div3 java题解)
2024-09-05 06:17:45
A:
题意:给定A个N元,B个一元,问是否可以凑成S元。
思路:A*i+j=S 即 A*I<=S<=A*I+B 即min(S/N,A)+B>=S;
/* @author nimphy @create 2019-11-05-10:34 about:CF1256A */ import java.util.*; public class CF1256 { public static void main(String[] args) { Scanner In = new Scanner(System.in); int Q, A, B, N, S; Q = In.nextInt(); while (Q-- > 0) { A = In.nextInt(); B = In.nextInt(); N = In.nextInt(); S = In.nextInt(); int I = Math.min(S / N, A) * N; //System.out.println(I); if (I + B >= S) System.out.println("YES"); else System.out.println("NO"); } } }
B:
题意:给定一个排列,现在让你做一套操作,使得字典序最小。
思路:贪心,先尽量把1提到前面,然后是2....,如果满足{位置交换没用过,而且比左边的小就换}
/* @author nimphy @create 2019-11-05-11:34 about:CF1256B */ import java.util.*; public class CF1256 { static int[] a = new int[1010]; static boolean[] vis = new boolean[1010]; public static void main(String[] args) { Scanner In = new Scanner(System.in); int Q, N; Q = In.nextInt(); while (Q-- > 0) { N = In.nextInt(); for (int i = 1; i <= N; i++) { a[i] = In.nextInt(); vis[i] = false; } for (int i = 1; i <= N; i++) { int pos=0; for (int j = 1; j <= N; j++) { if (a[j] == i) { pos = j; break; } } while (pos > i && !vis[pos]&&a[pos]<a[pos-1]) { int t = a[pos]; a[pos] = a[pos - 1]; a[pos - 1] = t; vis[pos] = true; pos--; } } for (int i = 1; i <= N; i++) { System.out.print(a[i]+" "); } System.out.println(); } } }
C:
思路:贪心+情况可能多-----忽略。
------------------------------白嫖了输入优化--------------------------------------
D:
题意: 给定N,K和一个01串,你可以交换相邻的字符,但是次数不超过K次,求最后的最小字典序串。
思路:结论是,把前面的0移动到越前面,字典序最小。 那么我们从前往后扫‘0’,如果前面有x个‘1’,那么它可以和前面第min(x,K)个位置交换。
/* @author nimphy @create 2019-11-05-11:34 about:CF1256C */ import java.io.*; import java.util.*; public class CF1256 { private static boolean doLocalTest = System.getSecurityManager() == null; private Scanner sc = new Scanner(System.in); private PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { long executionTime = 0; if (doLocalTest) { executionTime = System.currentTimeMillis(); try { System.setIn((new FileInputStream("in"))); // System.setOut(new PrintStream(new File("out"))); } catch (FileNotFoundException e) { doLocalTest = false; } } CF1256 cf1256 = new CF1256(); cf1256.solve(); if (doLocalTest) { cf1256.out.println("======== [ End of Output] ========"); cf1256.out.println("> Time Spent: " + (System.currentTimeMillis() - executionTime) + " ms"); } cf1256.out.flush(); } static char[] a; static char[] ans = new char[10010]; void solve() { int T, N; long K; T = sc.nextInt(); while (T-- > 0) { N = sc.nextInt(); K = sc.nextLong(); a = sc.next().toCharArray(); // System.out.println(Arrays.toString(a)); int pre = 0; for (int i = 0; i < N && K > 0; i++) { if (a[i] == '1') { pre++; continue; } if (pre == 0) continue; int t = pre; if (K < t) t = (int) K; a[i - t] = '0'; a[i] = '1'; K -= t; } for (int i = 0; i < N; i++) out.print(a[i]); out.println(); } } } class Scanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; Scanner(InputStream in) { bufferedReader = new BufferedReader(new InputStreamReader(in)); stringTokenizer = new StringTokenizer(""); } String nextLine() { try { return bufferedReader.readLine(); } catch (IOException e) { throw new IOError(e); } } boolean hasNext() { while (!stringTokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) { return false; } stringTokenizer = new StringTokenizer(s); } return true; } String next() { hasNext(); return stringTokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } }
E:
题意:给的大小为N的集合,然后分组,每组元素个数不小于3个,代价是每组的最大值减最小值之和,求分组是的代价最小。
分组越多越好,那么可以假设最多6个一组。 懒得写了,毕竟要输出具体分组。(还不会java的结构体排序)
F:
题意:给定两个字符数组A[] , B[],长度相同。 现在你可以选择可以长度len,然后进行任意轮操作,每轮操作是反转一段A和一段B,其长度都是len,问最后是否可以相同。
思路:发现len选2最优,因为你无论len选多大,都可以由len=2转移过来,达到同样的效果; 那么现在len=2了,又发现,如果某一组有相同的字符,那么另外一组可以任意排列,所以YES; 而如果A和B都是各异的字符,那么如果逆序对的奇偶性相同,则YES。
/* @author nimphy @create 2019-11-05-11:34 about:CF1256F */ import java.io.*; import java.util.*; public class Main { private static boolean doLocalTest = System.getSecurityManager() == null; private Scanner sc = new Scanner(System.in); private PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) { long executionTime = 0; if (doLocalTest) { executionTime = System.currentTimeMillis(); try { System.setIn((new FileInputStream("in"))); // System.setOut(new PrintStream(new File("out"))); } catch (FileNotFoundException e) { doLocalTest = false; } } Main fcy = new Main(); fcy.solve(); if (doLocalTest) { fcy.out.println("======== [ End of Output] ========"); fcy.out.println("> Time Spent: " + (System.currentTimeMillis() - executionTime) + " ms"); } fcy.out.flush(); } String a,b; int[] num1=new int[26]; int[] num2=new int[26]; void solve() { int T, N; long K; T = sc.nextInt(); while (T-- > 0) { N = sc.nextInt(); a=sc.next(); b=sc.next(); for(int i=0;i<26;i++) num1[i]=num2[i]=0; for(int i=0;i<N;i++) { num1[a.charAt(i)-'a']++; num2[b.charAt(i)-'a']++; } boolean Flag=true; for(int i=0;i<26;i++) if(num1[i]!=num2[i]) Flag=false; if(!Flag) { out.println("NO"); continue; } for(int i=0;i<26;i++) if(num1[i]>1||num2[i]>1) Flag=false; if(!Flag) { out.println("YES"); continue; } long inv1=0,inv2=0; for(int i=0;i<26;i++) num1[i]=num2[i]=0; for(int i=0;i<N;i++){ for(int j=a.charAt(i)-'a'+1;j<26;j++) inv1+=num1[j]; for(int j=b.charAt(i)-'a'+1;j<26;j++) inv2+=num2[j]; num1[a.charAt(i)-'a']++; num2[b.charAt(i)-'a']++; } if(Math.abs(inv1-inv2)%2==0) out.println("YES"); else out.println("NO"); } } } class Scanner { private BufferedReader bufferedReader; private StringTokenizer stringTokenizer; Scanner(InputStream in) { bufferedReader = new BufferedReader(new InputStreamReader(in)); stringTokenizer = new StringTokenizer(""); } String nextLine() { try { return bufferedReader.readLine(); } catch (IOException e) { throw new IOError(e); } } boolean hasNext() { while (!stringTokenizer.hasMoreTokens()) { String s = nextLine(); if (s == null) { return false; } stringTokenizer = new StringTokenizer(s); } return true; } String next() { hasNext(); return stringTokenizer.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } }
最新文章
- JS继承之原型继承
- XV Open Cup named after E.V. Pankratiev. GP of Tatarstan
- python 脚本中使用了第三方openpyxl 打包程序运行提示ImportError:cannot import name __version__
- SQL 行列转换简单示例
- javascript应用之如何判断一个数为素数
- String.Format 全汇总
- jQuery插件开发的模式和结构
- iOS 获取手机的型号,系统版本,软件名称,软件版本
- Codeforce 220 div2
- Android 多状态按钮 ToggleButton
- js的继承实现
- ●BZOJ 3143 [Hnoi2013]游走
- 配置VLAN
- Ubuntu安装后上网问题,
- GDI+_从Bitmap里得到的Color数组值分解
- [luogu3878][TJOI2010]分金币【模拟退火】
- SNMP MIBs and IPv6
- unity游戏设计与实现 --读书笔记(一)
- Qt基础学习---滑动条之QSlider
- 【转】python实战——教你用微信每天给女朋友说晚安