Advertisement
dzocesrce

[NP] Telecomoperators

Apr 30th, 2025
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.64 KB | None | 0 0
  1. //june-22 harder one
  2. //killing this brought back my confidence
  3.  
  4. class CellularNetworkTest {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.         String name = scanner.nextLine();
  8.         String[] cells = scanner.nextLine().split("\\s+");
  9.         String[] cellIds = new String[cells.length];
  10.         int[] capacities = new int[cells.length];
  11.         for (int i = 0; i < cells.length; ++i) {
  12.             String[] parts = cells[i].split(":");
  13.             cellIds[i] = parts[0];
  14.             capacities[i] = Integer.parseInt(parts[1]);
  15.         }
  16.         CellularNetwork cellularNetwork = new CellularNetwork(name, cellIds, capacities);
  17.         int n = scanner.nextInt();
  18.         scanner.nextLine();
  19.         System.out.println("----- Making calls -----");
  20.         for (int i = 0; i < n; ++i) {
  21.             String line = scanner.nextLine();
  22.             String[] parts = line.split("\\s+");
  23.             try {
  24.                 cellularNetwork.makeCall(parts[0], parts[1]);
  25.             } catch (CellFullException e) {
  26.                 System.out.println(e.getMessage());
  27.             }
  28.         }
  29.         System.out.println();
  30.         n = scanner.nextInt();
  31.         scanner.nextLine();
  32.         System.out.println("----- Making handovers -----");
  33.         for (int i = 0; i < n; ++i) {
  34.             String line = scanner.nextLine();
  35.             String[] parts = line.split("\\s+");
  36.             try {
  37.                 cellularNetwork.handover(parts[0], parts[1], parts[2]);
  38.             } catch (CellFullException e) {
  39.                 System.out.println(e.getMessage());
  40.             }
  41.         }
  42.         System.out.println("===== Find numbers =====");
  43.         n = scanner.nextInt();
  44.         scanner.nextLine();
  45.         for (int i = 0; i < n; ++i) {
  46.             String number = scanner.nextLine();
  47.             cellularNetwork.findNumber(number);
  48.         }
  49.         System.out.println("===== Load =====");
  50.         cellularNetwork.load();
  51.         scanner.close();
  52.     }
  53. }
  54.  
  55. class CellFullException extends Exception{
  56.     public CellFullException(String cellId) {
  57.         super(String.format("CellFullException: {%s}",cellId));
  58.     }
  59. }
  60.  
  61. class Cell {
  62.     String cellId;
  63.     int capacity;
  64.     List<String> numbers;
  65.  
  66.     public Cell(String cellId, int capacity) {
  67.         this.cellId = cellId;
  68.         this.capacity = capacity;
  69.         this.numbers = new ArrayList<String>();
  70.     }
  71.  
  72.     public String getCellId() {
  73.         return cellId;
  74.     }
  75.  
  76.     public void removeNumber(String number) {
  77.         this.numbers.remove(number);
  78.     }
  79.  
  80.     public void addNumber(String number) throws CellFullException {
  81.         if(numbers.size()==capacity)
  82.             throw new CellFullException(cellId);
  83.         numbers.add(number);
  84.     }
  85.  
  86.     public double getPercentOfFilledCapacity(){
  87.         return numbers.size() * 100.0 / capacity;
  88.     }
  89.  
  90.     public String getNumberOfStars(){
  91.         String stars="";
  92.         for(int i=0;i<(int)getPercentOfFilledCapacity()/10;i++){
  93.             stars+="*";
  94.         }
  95.         return stars;
  96.     }
  97.  
  98.  
  99.     @Override
  100.     public String toString() {
  101.         return String.format("ID: %s\n" + "%s %.2f%%", cellId,getNumberOfStars(), getPercentOfFilledCapacity());
  102.     }
  103. }
  104.  
  105. class CellularNetwork {
  106.     String name;
  107.     Map<String,Cell> cells;
  108.     Map<String,List<String>> cellsPerNumber;
  109.  
  110.     public CellularNetwork(String name, String[] cellIds, int[] capacities) {
  111.         this.name = name;
  112.         this.cells= new HashMap<>();
  113.         for(int i=0;i<capacities.length;i++){
  114.             cells.putIfAbsent(cellIds[i],new Cell(cellIds[i],capacities[i]));
  115.         }
  116.         this.cellsPerNumber= new HashMap<>();
  117.     }
  118.  
  119.     public void makeCall(String cellId, String number) throws CellFullException {
  120.         cells.get(cellId).addNumber(number);
  121.         cellsPerNumber.putIfAbsent(number, new ArrayList<>());
  122.         cellsPerNumber.get(number).add(cellId);
  123.     }
  124.  
  125.     public void handover(String number, String fromCellId, String toCellId) throws CellFullException {
  126.         cells.get(fromCellId).removeNumber(number);
  127.         cells.get(toCellId).addNumber(number);
  128.         if(cellsPerNumber.containsKey(number))
  129.             cellsPerNumber.get(number).add(toCellId);
  130.     }
  131.  
  132.     public void findNumber(String number) {
  133.         if(!cellsPerNumber.containsKey(number)){
  134.             System.out.println(String.format("Number '%s' not found",number));
  135.             return ;
  136.         }
  137.         System.out.print(String.format("Number '%s' : ",number));
  138.         System.out.println(String.join(" --> ",
  139.                 cellsPerNumber.get(number).stream()
  140.                         .map(i->i.toString())
  141.                         .toList()
  142.         ));
  143.  
  144.     }
  145.     public void load() {
  146.         cells.values().stream().sorted(Comparator.comparing(Cell::getCellId)).forEach(i-> System.out.println(i));
  147.     }
  148. }
  149.  
  150. //**
  151. Telefonica
  152. cell-0:41 cell-1:39 cell-2:93 cell-3:91 cell-4:62 cell-5:26 cell-6:15 cell-7:49 cell-8:99 cell-9:30 cell-10:60 cell-11:57 cell-12:96
  153. 330
  154. cell-9 077155515
  155. cell-6 070744086
  156. cell-1 070609890
  157. cell-11 076057080
  158. cell-12 077497349
  159. cell-0 071320920
  160. cell-5 075227471
  161. cell-9 078393089
  162. cell-6 077967341
  163. cell-0 077937849
  164. cell-5 071420410
  165. cell-10 071105206
  166. cell-6 075405765
  167. cell-1 070432921
  168. cell-2 075361572
  169. cell-5 075957288
  170. cell-4 071350470
  171. cell-10 070101894
  172. cell-6 076756301
  173. cell-11 070166441
  174. cell-0 076784732
  175. cell-4 078252256
  176. cell-12 078050893
  177. cell-2 075265495
  178. cell-6 077599636
  179. cell-2 077088456
  180. cell-11 075132077
  181. cell-9 071787188
  182. cell-6 078349455
  183. cell-9 076308844
  184. cell-4 075788786
  185. cell-6 077511714
  186. cell-1 071206729
  187. cell-1 077225406
  188. cell-0 078995064
  189. cell-8 076518717
  190. cell-11 075475784
  191. cell-11 078112403
  192. cell-7 076885126
  193. cell-8 077835995
  194. cell-3 077863009
  195. cell-12 078440649
  196. cell-5 070311600
  197. cell-11 071602445
  198. cell-0 075897919
  199. cell-11 078779310
  200. cell-2 070085002
  201. cell-0 077841966
  202. cell-3 076981213
  203. cell-0 078222571
  204. cell-8 070892766
  205. cell-5 070631652
  206. cell-1 076234814
  207. cell-1 075789230
  208. cell-7 070608330
  209. cell-6 075358940
  210. cell-2 077834717
  211. cell-1 071058947
  212. cell-9 075892863
  213. cell-9 077459874
  214. cell-4 077823816
  215. cell-12 071882640
  216. cell-9 071587652
  217. cell-6 070123300
  218. cell-12 070405057
  219. cell-11 076707709
  220. cell-11 075703343
  221. cell-11 071901151
  222. cell-1 071715302
  223. cell-3 070669243
  224. cell-3 071804647
  225. cell-12 071003966
  226. cell-2 076700085
  227. cell-1 070058434
  228. cell-0 076125303
  229. cell-0 078306973
  230. cell-10 070582459
  231. cell-6 077659477
  232. cell-8 071089086
  233. cell-9 077194356
  234. cell-10 075301384
  235. cell-0 071135213
  236. cell-11 077139026
  237. cell-3 077077456
  238. cell-6 075948033
  239. cell-7 076155912
  240. cell-1 075432243
  241. cell-4 077667155
  242. cell-10 076977582
  243. cell-6 070807120
  244. cell-7 071251065
  245. cell-12 077565705
  246. cell-3 075229803
  247. cell-11 070197268
  248. cell-7 071491512
  249. cell-5 075054032
  250. cell-3 078270554
  251. cell-11 078318865
  252. cell-9 077744153
  253. cell-4 070747703
  254. cell-3 075694804
  255. cell-9 075628531
  256. cell-7 076070930
  257. cell-0 071289586
  258. cell-12 077617091
  259. cell-9 070808663
  260. cell-5 076551671
  261. cell-5 070849521
  262. cell-3 071745033
  263. cell-9 076628770
  264. cell-7 070449804
  265. cell-4 070134300
  266. cell-4 078403144
  267. cell-1 075398340
  268. cell-11 077389994
  269. cell-0 077173029
  270. cell-1 071103496
  271. cell-4 077757368
  272. cell-2 071803357
  273. cell-12 071690066
  274. cell-8 076020624
  275. cell-12 071074644
  276. cell-9 078888494
  277. cell-0 078160840
  278. cell-4 071815847
  279. cell-4 078744181
  280. cell-6 075792835
  281. cell-9 070759142
  282. cell-3 071569811
  283. cell-0 070405154
  284. cell-5 075407763
  285. cell-3 078180219
  286. cell-8 077785601
  287. cell-11 075266823
  288. cell-12 076110915
  289. cell-7 078482517
  290. cell-2 077486472
  291. cell-5 078910455
  292. cell-12 078534640
  293. cell-12 078017267
  294. cell-8 076885275
  295. cell-3 075486180
  296. cell-7 076296393
  297. cell-1 070686864
  298. cell-3 078698077
  299. cell-3 077837389
  300. cell-11 078473523
  301. cell-8 078139851
  302. cell-4 070947963
  303. cell-10 070195799
  304. cell-0 071253651
  305. cell-7 077094909
  306. cell-2 071724602
  307. cell-4 070007528
  308. cell-3 070828915
  309. cell-6 077263014
  310. cell-10 077276449
  311. cell-11 076403671
  312. cell-8 071005558
  313. cell-6 076781768
  314. cell-3 078248736
  315. cell-3 078881943
  316. cell-9 078992082
  317. cell-8 077146081
  318. cell-7 070796697
  319. cell-4 071922530
  320. cell-7 071639310
  321. cell-3 076415059
  322. cell-10 076532259
  323. cell-7 070316676
  324. cell-8 070458009
  325. cell-9 070004311
  326. cell-10 071483014
  327. cell-0 075752481
  328. cell-2 071388271
  329. cell-1 076241700
  330. cell-8 077305991
  331. cell-5 070575116
  332. cell-1 071913324
  333. cell-4 077114475
  334. cell-5 077643997
  335. cell-2 076206471
  336. cell-8 075981654
  337. cell-8 078597302
  338. cell-1 076848901
  339. cell-2 075992107
  340. cell-1 070150773
  341. cell-4 076748611
  342. cell-0 070818687
  343. cell-8 078289636
  344. cell-4 070350947
  345. cell-5 075343488
  346. cell-7 078027919
  347. cell-6 070040376
  348. cell-3 071976213
  349. cell-11 070849566
  350. cell-9 075392512
  351. cell-6 071281219
  352. cell-8 075716273
  353. cell-5 070323353
  354. cell-8 071024444
  355. cell-7 077755786
  356. cell-11 075710684
  357. cell-3 075163085
  358. cell-7 077428338
  359. cell-3 070424763
  360. cell-5 070694886
  361. cell-3 077193061
  362. cell-0 077500132
  363. cell-9 075202837
  364. cell-12 071284142
  365. cell-10 076234076
  366. cell-6 075873978
  367. cell-3 070146385
  368. cell-4 070036091
  369. cell-0 078283135
  370. cell-8 070885727
  371. cell-0 070898394
  372. cell-8 077448814
  373. cell-0 070374409
  374. cell-12 070152703
  375. cell-6 070168340
  376. cell-3 071362213
  377. cell-10 071105419
  378. cell-6 077362619
  379. cell-1 077878760
  380. cell-5 071114646
  381. cell-10 075561325
  382. cell-12 075467046
  383. cell-8 078615866
  384. cell-12 076047326
  385. cell-5 078153795
  386. cell-0 070183074
  387. cell-5 078733420
  388. cell-5 077597983
  389. cell-9 075071132
  390. cell-0 077584310
  391. cell-6 071640772
  392. cell-11 075889025
  393. cell-1 078575495
  394. cell-4 075990971
  395. cell-3 070837311
  396. cell-5 075690895
  397. cell-11 070385578
  398. cell-10 070144571
  399. cell-12 071518753
  400. cell-3 078440202
  401. cell-0 070019101
  402. cell-7 077365910
  403. cell-6 078894198
  404. cell-11 077594916
  405. cell-1 078275090
  406. cell-5 076254701
  407. cell-6 077954062
  408. cell-4 075027042
  409. cell-12 075645658
  410. cell-4 071775154
  411. cell-9 071164722
  412. cell-1 077500385
  413. cell-10 078243710
  414. cell-11 076639513
  415. cell-9 070141797
  416. cell-7 075685644
  417. cell-9 071388976
  418. cell-11 070458071
  419. cell-11 070969271
  420. cell-9 076055723
  421. cell-10 071863138
  422. cell-1 076860630
  423. cell-2 071422609
  424. cell-12 076237828
  425. cell-2 070345532
  426. cell-5 075637310
  427. cell-10 076210785
  428. cell-2 076734410
  429. cell-0 078452918
  430. cell-10 077120357
  431. cell-12 071464421
  432. cell-11 078391251
  433. cell-7 070565965
  434. cell-5 070328685
  435. cell-11 078422606
  436. cell-0 071078986
  437. cell-6 076658075
  438. cell-10 070653035
  439. cell-4 077117554
  440. cell-11 078138676
  441. cell-9 071308316
  442. cell-11 071354288
  443. cell-3 078422797
  444. cell-1 076416262
  445. cell-8 078996760
  446. cell-10 076215870
  447. cell-4 078286907
  448. cell-4 075409879
  449. cell-10 070978111
  450. cell-5 075919824
  451. cell-11 071098132
  452. cell-12 071586869
  453. cell-10 070313696
  454. cell-0 075679501
  455. cell-3 071185119
  456. cell-4 075656867
  457. cell-3 077772570
  458. cell-7 077695010
  459. cell-8 070797365
  460. cell-4 070983023
  461. cell-5 076358621
  462. cell-1 076899300
  463. cell-12 076523362
  464. cell-8 070829120
  465. cell-12 076284725
  466. cell-3 076063172
  467. cell-5 070717538
  468. cell-3 071190981
  469. cell-5 070693868
  470. cell-12 071630052
  471. cell-3 076718376
  472. cell-7 077387602
  473. cell-7 071162466
  474. cell-4 070409325
  475. cell-6 078103726
  476. cell-12 071333194
  477. cell-1 078612077
  478. cell-6 077873663
  479. cell-2 078575011
  480. cell-1 076689806
  481. cell-4 077093233
  482. cell-0 071078155
  483. cell-2 070136103
  484. 62
  485. 071640772 cell-6 cell-7
  486. 075685644 cell-7 cell-8
  487. 076899300 cell-1 cell-2
  488. 071639310 cell-7 cell-8
  489. 077193061 cell-3 cell-4
  490. 071320920 cell-0 cell-1
  491. 076707709 cell-11 cell-12
  492. 070007528 cell-4 cell-5
  493. 076981213 cell-3 cell-4
  494. 075358940 cell-6 cell-7
  495. 078888494 cell-9 cell-10
  496. 075628531 cell-9 cell-10
  497. 076308844 cell-9 cell-10
  498. 077954062 cell-6 cell-7
  499. 075628531 cell-10 cell-11
  500. 075637310 cell-5 cell-6
  501. 070818687 cell-0 cell-1
  502. 078575495 cell-1 cell-2
  503. 071354288 cell-11 cell-12
  504. 075990971 cell-4 cell-5
  505. 071190981 cell-3 cell-4
  506. 070004311 cell-9 cell-10
  507. 070316676 cell-7 cell-8
  508. 078779310 cell-11 cell-12
  509. 076784732 cell-0 cell-1
  510. 075266823 cell-11 cell-12
  511. 070797365 cell-8 cell-9
  512. 070759142 cell-9 cell-10
  513. 070058434 cell-1 cell-2
  514. 070892766 cell-8 cell-9
  515. 077146081 cell-8 cell-9
  516. 070796697 cell-7 cell-8
  517. 077659477 cell-6 cell-7
  518. 077835995 cell-8 cell-9
  519. 076210785 cell-10 cell-11
  520. 078615866 cell-8 cell-9
  521. 075266823 cell-12 cell-0
  522. 070759142 cell-10 cell-11
  523. 070892766 cell-9 cell-10
  524. 071098132 cell-11 cell-12
  525. 070686864 cell-1 cell-2
  526. 070146385 cell-3 cell-4
  527. 078243710 cell-10 cell-11
  528. 077093233 cell-4 cell-5
  529. 078222571 cell-0 cell-1
  530. 071388271 cell-2 cell-3
  531. 076718376 cell-3 cell-4
  532. 075229803 cell-3 cell-4
  533. 070582459 cell-10 cell-11
  534. 075054032 cell-5 cell-6
  535. 076628770 cell-9 cell-10
  536. 075710684 cell-11 cell-12
  537. 076047326 cell-12 cell-0
  538. 070166441 cell-11 cell-12
  539. 078286907 cell-4 cell-5
  540. 078160840 cell-0 cell-1
  541. 070717538 cell-5 cell-6
  542. 071602445 cell-11 cell-12
  543. 070019101 cell-0 cell-1
  544. 077834717 cell-2 cell-3
  545. 075637310 cell-6 cell-7
  546. 077155515 cell-9 cell-10
  547. 18
  548. 071690066
  549. 077263014
  550. 070978111
  551. 070759142
  552. 070686864
  553. 078306973
  554. 076237828
  555. 076781768
  556. 078248736
  557. 076977582
  558. 076403671
  559. 075637310
  560. 078612077
  561. 076063172
  562. 071005558
  563. 075992107
  564. 070101894
  565. 075710684
  566. **//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement