Advertisement
ec1117

Untitled

Jun 24th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.awt.Point;
  5.  
  6. public class movie{
  7. public static final String TASKNAME = "movie";
  8. public static long mod= 1000000007;
  9. public static Debug db;
  10.  
  11. public static void main(String[] args) throws IOException {
  12. // InputReader in = new InputReader(System.in);
  13. // PrintWriter out = new PrintWriter(System.out);
  14. InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
  15. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
  16. Autocompletion solver = new Autocompletion();
  17. db=new Debug(System.getSecurityManager()==null);
  18. solver.solve(1, in, out);
  19. out.close();
  20. }
  21. static class Autocompletion {
  22.  
  23. public void solve(int testNumber, InputReader in, PrintWriter out) {
  24. int n=in.nextInt();
  25. int l=in.nextInt();
  26. int dur[]=new int[n];
  27. ArrayList<Pair> arr=new ArrayList<Pair>();//index by order
  28. for(int i=0;i<n;i++) {
  29. int d=in.nextInt();
  30. dur[i]=d;
  31. int c=in.nextInt();
  32. for(int j=0;j<c;j++) {
  33. int temp=in.nextInt();
  34. arr.add(new Pair(temp,i));
  35. }
  36. }
  37. Collections.sort(arr);
  38. ArrayList<Integer> adj[]=new ArrayList[arr.size()];
  39. for(int i=0;i<arr.size();i++)adj[i]=new ArrayList<Integer>();
  40. for(int i=0;i<arr.size();i++) {
  41. for(int j=i+1;j<arr.size();j++) {
  42. if(arr.get(i).x+dur[arr.get(i).y]>=arr.get(j).x) {
  43. adj[i].add(j);
  44. }
  45. else {
  46. break;
  47. }
  48. }
  49. }
  50. int dp[]=new int[1<<n];
  51. Arrays.fill(dp, -1);
  52. dp[0]=0;
  53. ArrayDeque<Pair> dq=new ArrayDeque<Pair>();
  54. for(int i=0;i<arr.size();i++) {
  55. if(arr.get(i).x==0) {
  56. dq.add(new Pair(i,1<<arr.get(i).y));
  57. dp[1<<arr.get(i).y]=dur[arr.get(i).y];
  58. }
  59. else {
  60. break;
  61. }
  62. }
  63. while(!dq.isEmpty()) {
  64. Pair x=dq.remove();
  65. for(int y:adj[x.x]) {
  66. int newSet=(1<<arr.get(y).y | x.y);
  67. if((1<<arr.get(y).y & x.y)==0) {//no movies in common
  68. if(arr.get(y).x+dur[arr.get(y).y]>dp[newSet]) {//better
  69. dp[newSet]=arr.get(y).x+dur[arr.get(y).y];
  70. dq.add(new Pair(y,newSet));
  71. }
  72. }
  73. }
  74. }
  75. int ret=22;
  76. for(int i=0;i<dp.length;i++) {
  77. if(dp[i]>=l) {
  78. // System.out.println(i);
  79. ret=Math.min(ret, Integer.bitCount(i));
  80. }
  81. }
  82. if(ret==22) {
  83. out.println(-1);
  84. }
  85. else {
  86. out.println(ret);
  87. }
  88. }
  89. }
  90. static class Pair implements Comparable<Pair>{
  91. int x;
  92. int y;
  93. Pair(int a, int b){
  94. x=a;
  95. y=b;
  96. }
  97. @Override
  98. public int compareTo(Pair arg0) {
  99. if(arg0.x!=x)return x-arg0.x;
  100. return y-arg0.y;
  101. }
  102. }
  103. static class Triple implements Comparable<Triple>{
  104. int x;
  105. int y;
  106. int z;
  107. Triple(int a, int b, int c){
  108. x=a;
  109. y=b;
  110. z=c;
  111. }
  112. @Override
  113. public int compareTo(Triple arg0) {
  114. if(arg0.x!=x)return x-arg0.x;
  115. return y-arg0.y;
  116. }
  117. }
  118.  
  119. static class InputReader {
  120. public BufferedReader reader;
  121. public StringTokenizer tokenizer;
  122.  
  123. public InputReader(InputStream stream) {
  124. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  125. tokenizer = null;
  126. }
  127.  
  128. public String next() {
  129. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  130. try {
  131. tokenizer = new StringTokenizer(reader.readLine());
  132. } catch (IOException e) {
  133. throw new RuntimeException(e);
  134. }
  135. }
  136. return tokenizer.nextToken();
  137. }
  138.  
  139. public int nextInt() {
  140. return Integer.parseInt(next());
  141. }
  142. public long nextLong() {
  143. return Long.parseLong(next());
  144. }
  145. public int[] nextIntArr(int n) {
  146. int arr[]=new int[n];
  147. for(int i=0;i<n;i++) {
  148. arr[i]=this.nextInt();
  149. }
  150. return arr;
  151. }
  152. }
  153. public static class Debug {
  154. private boolean allowDebug;
  155.  
  156. public Debug(boolean allowDebug) {
  157. this.allowDebug = allowDebug;
  158. }
  159.  
  160. private void outputName(String name) {
  161. System.out.print(name + " = ");
  162. }
  163.  
  164. public void debug(String name, int x) {
  165. if (!allowDebug) {
  166. return;
  167. }
  168.  
  169. outputName(name);
  170. System.out.println("" + x);
  171. }
  172.  
  173. public void debug(String name, long x) {
  174. if (!allowDebug) {
  175. return;
  176. }
  177. outputName(name);
  178. System.out.println("" + x);
  179. }
  180.  
  181. public void debug(String name, double x) {
  182. if (!allowDebug) {
  183. return;
  184. }
  185. outputName(name);
  186. System.out.println("" + x);
  187. }
  188.  
  189. public void debug(String name, int[] x) {
  190. if (!allowDebug) {
  191. return;
  192. }
  193. outputName(name);
  194. System.out.println(Arrays.toString(x));
  195. }
  196.  
  197. public void debug(String name, long[] x) {
  198. if (!allowDebug) {
  199. return;
  200. }
  201. outputName(name);
  202. System.out.println(Arrays.toString(x));
  203. }
  204.  
  205. public void debug(String name, double[] x) {
  206. if (!allowDebug) {
  207. return;
  208. }
  209. outputName(name);
  210. System.out.println(Arrays.toString(x));
  211. }
  212.  
  213. public void debug(String name, Object x) {
  214. if (!allowDebug) {
  215. return;
  216. }
  217. outputName(name);
  218. System.out.println("" + x);
  219. }
  220.  
  221. public void debug(String name, Object... x) {
  222. if (!allowDebug) {
  223. return;
  224. }
  225. outputName(name);
  226. System.out.println(Arrays.deepToString(x));
  227. }
  228. }
  229. }
  230.  
  231. //equal :problem
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement