/** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-18 Runda 5B. Fiolki. * * Pamiec: 128 MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; final class Mixture { public static Mixture[] all; public static ArrayList<MixturePair> interacting; private int id; private int quantity; private boolean active; public Mixture(int id, int quantity) { this.id = id; this.quantity = quantity; this.active = false; } public int getID() { return id; } public void setActive(boolean active) { this.active = active; } public boolean isActive() { if (quantity > 0) { return active; } else { return false; } } public static Mixture GetById(int id) { return all[id]; } public static long interact(int idA, int idB) { Mixture a = GetById(idA); Mixture b = GetById(idB); long r = Math.min(a.quantity, b.quantity); a.quantity -= r; b.quantity -= r; return r + r; } } final class MixturePair { private int priority; private int idA; private int idB; public MixturePair(int proirity, int idA, int idB) { set(priority, idA, idB); } public void set(int priority, int idA, int idB) { this.priority = priority; if (idA < idB) { this.idA = idA; this.idB = idB; } else { this.idA = idB; this.idB = idA; } } public int getIdA() { return idA; } public int getIdB() { return idB; } public int getPriority() { return priority; } } final class MixturePairByPriority implements Comparator<MixturePair> { @Override public int compare(MixturePair x, MixturePair y) { if (y == null) return -1; // < else if (x.getPriority() > y.getPriority()) return 1; // > else if (x.getPriority() < y.getPriority()) return -1; // < else return 0; } } final class MixturePairById implements Comparator<MixturePair> { @Override public int compare(MixturePair x, MixturePair y) { if (y == null) return -1; // < if (x.getIdA() > y.getIdA()) return 1; // > else if (x.getIdA() < y.getIdA()) return -1; // < else if (x.getIdB() > y.getIdB()) return 1; // > else if (x.getIdB() < y.getIdB()) return -1; // < else return 0; } } final class Vial { private static MixturePairById mixturePairById = new MixturePairById(); private static MixturePairByPriority mixturePairByPriority = new MixturePairByPriority(); private ArrayList<Mixture> mixtures = new ArrayList<Mixture>(); public Vial(Mixture mixture) { mixtures.add(mixture); } public long add(Vial vial) { long r = 0; Mixture a, b; MixturePair p = new MixturePair(0, 0, 0); ArrayList<MixturePair> interact = new ArrayList<MixturePair>(); for (int i = 0; i < vial.mixtures.size(); i++) { a = vial.mixtures.get(i); if (a.isActive()) { for (int j = 0; j < this.mixtures.size(); j++) { b = this.mixtures.get(j); if (b.isActive()) { p.set(0, a.getID(), b.getID()); int ind = Collections.binarySearch(Mixture.interacting, p, mixturePairById); if (ind >= 0) { interact.add(Mixture.interacting.get(ind)); } } } } } if (interact.size() > 0) { Collections.sort(interact, mixturePairByPriority); for (int i = 0; i < interact.size(); i++) { r += Mixture.interact(interact.get(i).getIdA(), interact.get(i) .getIdB()); } } // usun niepotrzebne mikstury for (int j = 0; j < this.mixtures.size(); j++) { b = this.mixtures.get(j); if (!b.isActive()) { this.mixtures.remove(j); } } // dodaj nowe mikstury for (int i = 0; i < vial.mixtures.size(); i++) { a = vial.mixtures.get(i); if (a.isActive()) { this.mixtures.add(a); } } return r; } } public final class fio { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 0..200 000 int m = nextInteger(); // 0..200 000, m<n int k = nextInteger(); // 0..500 000 Mixture.all = new Mixture[n + 1]; Vial[] vials = new Vial[n + 1]; for (int i = 1; i <= n; i++) { Mixture.all[i] = new Mixture(i, nextInteger()); vials[i] = new Vial(Mixture.all[i]); } int[][] steps = new int[m + 1][2]; for (int i = 1; i <= m; i++) { steps[i] = new int[2]; steps[i][0] = nextInteger(); steps[i][1] = nextInteger(); } Mixture.interacting = new ArrayList<MixturePair>(k + 1); for (int i = 1; i <= k; i++) { int a = nextInteger(); int b = nextInteger(); Mixture.interacting.add(new MixturePair(i, a, b)); Mixture.GetById(a).setActive(true); Mixture.GetById(b).setActive(true); } Collections.sort(Mixture.interacting, new MixturePairById()); long r = 0; for (int i = 1; i <= m; i++) { // wlej A do B r += vials[steps[i][1]].add(vials[steps[i][0]]); } System.out.println(r); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | /** * @author Tadeusz Faber (SI-Consulting SA). * * Potyczki Algorytmiczne 2014. * * 2014-05-18 Runda 5B. Fiolki. * * Pamiec: 128 MB. * Czas: ?s. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; final class Mixture { public static Mixture[] all; public static ArrayList<MixturePair> interacting; private int id; private int quantity; private boolean active; public Mixture(int id, int quantity) { this.id = id; this.quantity = quantity; this.active = false; } public int getID() { return id; } public void setActive(boolean active) { this.active = active; } public boolean isActive() { if (quantity > 0) { return active; } else { return false; } } public static Mixture GetById(int id) { return all[id]; } public static long interact(int idA, int idB) { Mixture a = GetById(idA); Mixture b = GetById(idB); long r = Math.min(a.quantity, b.quantity); a.quantity -= r; b.quantity -= r; return r + r; } } final class MixturePair { private int priority; private int idA; private int idB; public MixturePair(int proirity, int idA, int idB) { set(priority, idA, idB); } public void set(int priority, int idA, int idB) { this.priority = priority; if (idA < idB) { this.idA = idA; this.idB = idB; } else { this.idA = idB; this.idB = idA; } } public int getIdA() { return idA; } public int getIdB() { return idB; } public int getPriority() { return priority; } } final class MixturePairByPriority implements Comparator<MixturePair> { @Override public int compare(MixturePair x, MixturePair y) { if (y == null) return -1; // < else if (x.getPriority() > y.getPriority()) return 1; // > else if (x.getPriority() < y.getPriority()) return -1; // < else return 0; } } final class MixturePairById implements Comparator<MixturePair> { @Override public int compare(MixturePair x, MixturePair y) { if (y == null) return -1; // < if (x.getIdA() > y.getIdA()) return 1; // > else if (x.getIdA() < y.getIdA()) return -1; // < else if (x.getIdB() > y.getIdB()) return 1; // > else if (x.getIdB() < y.getIdB()) return -1; // < else return 0; } } final class Vial { private static MixturePairById mixturePairById = new MixturePairById(); private static MixturePairByPriority mixturePairByPriority = new MixturePairByPriority(); private ArrayList<Mixture> mixtures = new ArrayList<Mixture>(); public Vial(Mixture mixture) { mixtures.add(mixture); } public long add(Vial vial) { long r = 0; Mixture a, b; MixturePair p = new MixturePair(0, 0, 0); ArrayList<MixturePair> interact = new ArrayList<MixturePair>(); for (int i = 0; i < vial.mixtures.size(); i++) { a = vial.mixtures.get(i); if (a.isActive()) { for (int j = 0; j < this.mixtures.size(); j++) { b = this.mixtures.get(j); if (b.isActive()) { p.set(0, a.getID(), b.getID()); int ind = Collections.binarySearch(Mixture.interacting, p, mixturePairById); if (ind >= 0) { interact.add(Mixture.interacting.get(ind)); } } } } } if (interact.size() > 0) { Collections.sort(interact, mixturePairByPriority); for (int i = 0; i < interact.size(); i++) { r += Mixture.interact(interact.get(i).getIdA(), interact.get(i) .getIdB()); } } // usun niepotrzebne mikstury for (int j = 0; j < this.mixtures.size(); j++) { b = this.mixtures.get(j); if (!b.isActive()) { this.mixtures.remove(j); } } // dodaj nowe mikstury for (int i = 0; i < vial.mixtures.size(); i++) { a = vial.mixtures.get(i); if (a.isActive()) { this.mixtures.add(a); } } return r; } } public final class fio { private static Reader in = null; /** * Zwroc kolejna liczbe calkowita ze strumienia in * * @return wczytana liczba */ private static int nextInteger() { int r = 0; boolean minus = false; char c; try { do { c = (char) in.read(); } while (c == ' ' || c == '\n' || c == '\r'); if (c == '-') { minus = true; c = (char) in.read(); } do { r = r * 10 + (int) c - 48; c = (char) in.read(); } while (c != ' ' && c != '\n' && c != '\r' && c != (char) -1); } catch (Exception ex) { ex.printStackTrace(); } if (minus) { return -r; } else { return r; } } /** * @param args */ public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); int n = nextInteger(); // 0..200 000 int m = nextInteger(); // 0..200 000, m<n int k = nextInteger(); // 0..500 000 Mixture.all = new Mixture[n + 1]; Vial[] vials = new Vial[n + 1]; for (int i = 1; i <= n; i++) { Mixture.all[i] = new Mixture(i, nextInteger()); vials[i] = new Vial(Mixture.all[i]); } int[][] steps = new int[m + 1][2]; for (int i = 1; i <= m; i++) { steps[i] = new int[2]; steps[i][0] = nextInteger(); steps[i][1] = nextInteger(); } Mixture.interacting = new ArrayList<MixturePair>(k + 1); for (int i = 1; i <= k; i++) { int a = nextInteger(); int b = nextInteger(); Mixture.interacting.add(new MixturePair(i, a, b)); Mixture.GetById(a).setActive(true); Mixture.GetById(b).setActive(true); } Collections.sort(Mixture.interacting, new MixturePairById()); long r = 0; for (int i = 1; i <= m; i++) { // wlej A do B r += vials[steps[i][1]].add(vials[steps[i][0]]); } System.out.println(r); } } |