fork download
  1. import java.io.BufferedOutputStream;
  2. import java.io.BufferedReader;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.OutputStream;
  10. import java.io.PrintWriter;
  11. import java.util.Arrays;
  12. import java.util.StringTokenizer;
  13.  
  14. public class R2D2AndDroidArmy {
  15. public static void main(String[] args) {
  16. InputStream input;
  17. OutputStream output;
  18. try {
  19. input = new FileInputStream("input.txt");
  20. output = new FileOutputStream("output.txt");
  21. } catch (FileNotFoundException e) {
  22. input = System.in;
  23. output = System.out;
  24. }
  25. Kattio io = new Kattio(input, output);
  26. //int t=io.getInt();
  27. //for (int i=1; i<=t; i++)
  28. (new Solve(io)).main();
  29. io.close();
  30.  
  31. if (input instanceof FileInputStream)
  32. try {
  33. input.close();
  34. } catch (IOException e) {
  35. }
  36. if (output instanceof FileOutputStream)
  37. try {
  38. output.close();
  39. } catch (IOException e) {
  40. }
  41. }
  42. }
  43.  
  44. class RMQ
  45. {
  46. int n;
  47. int[] arr;
  48. int[] l,r;
  49. int[] dat;
  50. int[] pos;
  51.  
  52. public RMQ(int n)
  53. {
  54. this.n = n;
  55. arr = new int[n+1];
  56. dat = new int[4*n+1];
  57. l = new int[4*n+1];
  58. r = new int[4*n+1];
  59. pos = new int[n+1];
  60. RMQHelper(1,1,n);
  61. }
  62.  
  63. private void RMQHelper(int i,int x,int y)
  64. {
  65. l[i]=x;
  66. r[i]=y;
  67. if (x==y)
  68. {
  69. dat[i]=arr[x];
  70. pos[x]=i;
  71. }
  72. else
  73. {
  74. int m=(x+y)/2;
  75. RMQHelper(i*2,x,m);
  76. RMQHelper(i*2+1,m+1,y);
  77. dat[i]=Math.max(dat[i*2], dat[i*2+1]);
  78. }
  79. }
  80.  
  81. public void update(int x,int y)
  82. {
  83. arr[x] = y;
  84. dat[pos[x]] = y;
  85. updateHelper(pos[x]/2);
  86. }
  87.  
  88. private void updateHelper(int x)
  89. {
  90. if (x==0) return;
  91.  
  92. dat[x] = Math.max(dat[x*2],dat[x*2+1]);
  93. updateHelper(x/2);
  94. }
  95.  
  96. public int get(int l,int r)
  97. {
  98. return getHelper(1,l,r);
  99. }
  100.  
  101. private int getHelper(int i,int x,int y)
  102. {
  103. int res;
  104. if (y<l[i] || x>r[i]) res=0;
  105. else
  106. if (x<=l[i] && r[i]<=y) res=dat[i];
  107. else
  108. {
  109. res=getHelper(i*2,x,y);
  110. res=Math.max(res,getHelper(i*2+1,x,y));
  111. }
  112.  
  113. return res;
  114. }
  115.  
  116. @Override
  117. public String toString() {
  118. return "RMQ [n=" + n + ", arr=" + Arrays.toString(arr) + ", l=" + Arrays.toString(l) + ", r="
  119. + Arrays.toString(r) + ", dat=" + Arrays.toString(dat) + ", pos=" + Arrays.toString(pos) + "]";
  120. }
  121. }
  122.  
  123. class RMQEfficient
  124. {
  125. int n;
  126. int[] arr;
  127. int[][] rmq;
  128. int[] pow;
  129. int[] mn;
  130.  
  131. public RMQEfficient(int[] arr)
  132. {
  133. this.n = arr.length-1;
  134.  
  135. this.arr = new int[arr.length];
  136. for (int i=1; i<=n; i++)
  137. this.arr[i] = arr[i];
  138.  
  139. initPowMn();
  140. initRMQ();
  141. }
  142.  
  143. private void initPowMn()
  144. {
  145. pow = new int[31];
  146. pow[0] = 1;
  147.  
  148. mn = new int[n+1];
  149. Arrays.fill(mn, 0);
  150.  
  151. for (int i=1; i<=30; i++)
  152. {
  153. pow[i]=2*pow[i-1];
  154. if (pow[i]<=n)
  155. {
  156. try
  157. {
  158. mn[pow[i]] = i;
  159. }
  160. {
  161. System.out.println(n+" "+pow[i]+" "+i);
  162. }
  163. }
  164. }
  165.  
  166. for (int i=1; i<=n; i++)
  167. mn[i] = Math.max(mn[i-1], mn[i]);
  168. }
  169.  
  170. private void initRMQ()
  171. {
  172. rmq = new int[n+1][31];
  173. for (int i=1; i<=n; i++)
  174. Arrays.fill(rmq[i],-1);
  175. for (int i=1; i<=n; i++)
  176. for (int j=0; j<=30; j++)
  177. initRMQHelper(i,j);
  178. }
  179.  
  180. private int initRMQHelper(int x,int y)
  181. {
  182. if (x>n) return 0;
  183. else
  184. if (rmq[x][y]!=-1) return rmq[x][y];
  185. else
  186. if (y==0)
  187. rmq[x][y]=arr[x];
  188. else
  189. {
  190. int endSide = x+pow[y]-1;
  191. rmq[x][y]=Math.max(initRMQHelper(x,y-1),
  192. initRMQHelper(endSide-pow[y-1]+1,y-1));
  193. }
  194. return rmq[x][y];
  195. }
  196.  
  197. public int get(int l,int r)
  198. {
  199. if (r<l) return 0;
  200. //System.out.println("get "+l+" "+r);
  201. int length = (r-l+1);
  202. int step = mn[length];
  203. //System.out.println(step);
  204. try
  205. {
  206. return Math.max(rmq[l][step],rmq[r-pow[step]+1][step]);
  207. }
  208. {
  209. System.out.println(l+" "+r+" "+(r-pow[step]+1)+" "+step);
  210. return 0;
  211. }
  212. }
  213.  
  214. }
  215.  
  216. class Solve {
  217. Kattio io;
  218. int n,m,k;
  219. int[][] a;
  220. RMQEfficient[] ds;
  221. Solve(Kattio io) {
  222. this.io = io;
  223. }
  224.  
  225. boolean check(int l,int r)
  226. {
  227. //io.println("check "+l+" "+r);
  228. if (r>n) return false;
  229. int sum = 0;
  230. for (int i=1; i<=m; i++)
  231. {
  232. //io.println("check "+l+" "+r);
  233. sum+=ds[i].get(l, r);
  234. }
  235. return (sum<=k);
  236. }
  237.  
  238. void main() {
  239. n=io.getInt();
  240. m=io.getInt();
  241. k=io.getInt();
  242. a = new int[n+1][m+1];
  243. for (int i=1; i<=n; i++)
  244. for (int j=1; j<=m; j++)
  245. a[i][j]=io.getInt();
  246.  
  247. ds = new RMQEfficient[m+1];
  248.  
  249. for (int i=1; i<=m; i++)
  250. {
  251. int[] init = new int[n+1];
  252. for (int j=1; j<=n; j++)
  253. init[j]=a[j][i];
  254. ds[i] = new RMQEfficient(init);
  255. }
  256.  
  257. //for (int i=1; i<=m; i++)
  258. // io.println(ds[i]);
  259.  
  260. int res = 0;
  261. int vtres = 0;
  262. for (int i=1; i<=n; i++)
  263. {
  264. int dau = i, cuoi = n;
  265. do
  266. {
  267. int giua=(dau+cuoi)/2;
  268. if (check(i,giua)) dau=giua+1;
  269. else cuoi=giua-1;
  270. }
  271. while (dau<=cuoi);
  272.  
  273. if (cuoi-i+1>res)
  274. {
  275. res=cuoi-i+1;
  276. vtres=i;
  277. }
  278. }
  279.  
  280. for (int i=1; i<=m; i++)
  281. io.print(ds[i].get(vtres,vtres+res-1)+" ");
  282.  
  283. }
  284. }
  285.  
  286. class Kattio extends PrintWriter {
  287. public Kattio(InputStream i) {
  288. super(new BufferedOutputStream(System.out));
  289. }
  290.  
  291. public Kattio(InputStream i, OutputStream o) {
  292. super(new BufferedOutputStream(o));
  293. }
  294.  
  295. public boolean hasMoreTokens() {
  296. return peekToken() != null;
  297. }
  298.  
  299. public int getInt() {
  300. return Integer.parseInt(nextToken());
  301. }
  302.  
  303. public double getDouble() {
  304. return Double.parseDouble(nextToken());
  305. }
  306.  
  307. public long getLong() {
  308. return Long.parseLong(nextToken());
  309. }
  310.  
  311. public String getWord() {
  312. return nextToken();
  313. }
  314.  
  315. private BufferedReader r;
  316. private String line;
  317. private StringTokenizer st;
  318. private String token;
  319.  
  320. private String peekToken() {
  321. if (token == null)
  322. try {
  323. while (st == null || !st.hasMoreTokens()) {
  324. line = r.readLine();
  325. if (line == null)
  326. return null;
  327. st = new StringTokenizer(line);
  328. }
  329. token = st.nextToken();
  330. } catch (IOException e) {
  331. }
  332. return token;
  333. }
  334.  
  335. private String nextToken() {
  336. String ans = peekToken();
  337. token = null;
  338. return ans;
  339. }
  340. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:14: error: class R2D2AndDroidArmy is public, should be declared in a file named R2D2AndDroidArmy.java
public class R2D2AndDroidArmy {
       ^
1 error
stdout
Standard output is empty