1111import java .util .StringTokenizer ;
1212import java .io .BufferedWriter ;
1313import java .io .OutputStreamWriter ;
14+ import java .nio .charset .StandardCharsets ;
1415
1516// https://contest.yandex.ru/contest/24414/run-report/160043341/
1617
@@ -19,6 +20,9 @@ class FindSystem {
1920 private static final int MAX_DOCUMENTS = 10_000 ;
2021 private static final int MAX_QUERIES = 10_000 ;
2122 private static final int MAX_LINE_LENGTH = 10_000 ;
23+ private static final long MAX_TOTAL_DOCUMENT_CHARS = 2_000_000 ;
24+ private static final long MAX_TOTAL_QUERY_CHARS = 2_000_000 ;
25+ private static final long MAX_POSTING_VISITS = 20_000_000 ;
2226
2327 /*
2428 * Принцип работы алгоритма:
@@ -85,6 +89,17 @@ private static HashMap<String, ArrayList<int[]>> buildIndex(String[] docs) {
8589 }
8690
8791 private static String processQuery (String query , HashMap <String , ArrayList <int []>> index ) {
92+ try {
93+ return processQuery (query , index , Long .MAX_VALUE ).output ();
94+ } catch (IOException impossible ) {
95+ throw new AssertionError (impossible );
96+ }
97+ }
98+
99+ private static QueryResult processQuery (
100+ String query ,
101+ HashMap <String , ArrayList <int []>> index ,
102+ long postingVisitBudget ) throws IOException {
88103 HashSet <String > uniqueWords = new HashSet <>();
89104 StringTokenizer st = new StringTokenizer (query );
90105
@@ -93,12 +108,17 @@ private static String processQuery(String query, HashMap<String, ArrayList<int[]
93108 }
94109
95110 HashMap <Integer , Integer > relevance = new HashMap <>();
111+ long postingVisits = 0 ;
96112
97113 for (String word : uniqueWords ) {
98114 ArrayList <int []> docs = index .get (word );
99115 if (docs == null ) {
100116 continue ;
101117 }
118+ if (docs .size () > postingVisitBudget - postingVisits ) {
119+ throw new IOException ("Aggregate query workload exceeds limit" );
120+ }
121+ postingVisits += docs .size ();
102122
103123 for (int [] pair : docs ) {
104124 int docId = pair [0 ];
@@ -134,7 +154,10 @@ private static String processQuery(String query, HashMap<String, ArrayList<int[]
134154 sb .append (best .get (i )[0 ]);
135155 }
136156
137- return sb .toString ();
157+ return new QueryResult (sb .toString (), postingVisits );
158+ }
159+
160+ private record QueryResult (String output , long postingVisits ) {
138161 }
139162
140163 private static boolean isBetter (int docId1 , int score1 , int docId2 , int score2 ) {
@@ -149,18 +172,32 @@ private static void solve() throws Exception {
149172
150173 int n = reader .nextInt (MAX_DOCUMENTS );
151174 String [] docs = new String [n ];
175+ long documentChars = 0 ;
152176 for (int i = 0 ; i < n ; i ++) {
153177 docs [i ] = reader .nextLine (MAX_LINE_LENGTH );
178+ documentChars += docs [i ].length ();
179+ if (documentChars > MAX_TOTAL_DOCUMENT_CHARS ) {
180+ throw new IOException ("Aggregate document input exceeds limit" );
181+ }
154182 }
155183
156184 HashMap <String , ArrayList <int []>> index = buildIndex (docs );
157185
158186 int m = reader .nextInt (MAX_QUERIES );
159- BufferedWriter out = new BufferedWriter (new OutputStreamWriter (System .out ));
187+ BufferedWriter out = new BufferedWriter (
188+ new OutputStreamWriter (System .out , StandardCharsets .UTF_8 ));
189+ long queryChars = 0 ;
190+ long postingVisitsLeft = MAX_POSTING_VISITS ;
160191
161192 for (int i = 0 ; i < m ; i ++) {
162193 String query = reader .nextLine (MAX_LINE_LENGTH );
163- out .write (processQuery (query , index ));
194+ queryChars += query .length ();
195+ if (queryChars > MAX_TOTAL_QUERY_CHARS ) {
196+ throw new IOException ("Aggregate query input exceeds limit" );
197+ }
198+ QueryResult result = processQuery (query , index , postingVisitsLeft );
199+ postingVisitsLeft -= result .postingVisits ();
200+ out .write (result .output ());
164201 out .newLine ();
165202 }
166203
@@ -212,11 +249,7 @@ public static void main(String[] args) throws Exception {
212249 if (System .getProperty ("os.name" ).startsWith ("Windows" )) {
213250 test ();
214251 } else {
215- try {
216- solve ();
217- } catch (IOException ignored ) {
218- // Invalid or excessive input is rejected without exhausting memory or CPU.
219- }
252+ solve ();
220253 }
221254 }
222255 private static class FastReader {
0 commit comments