Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class Werhouse{
- public int x;
- public int y;
- public int[] storage;
- public Werhouse(int xx, int yy){
- x= xx;
- y=yy;
- storage = new int[Main.PRODUCT_TYPES];
- }
- public int items(){
- int sum = 0;
- for(int i:storage){
- sum+=i;
- }
- return sum;
- }
- }
- class Order{
- public int x;
- public int y;
- HashMap<Integer, Integer> products;
- public Order(int xx, int yy, HashMap<Integer, Integer> hash){
- x = xx;
- y = yy;
- products = hash;
- }
- }
- class Drone{
- public int currentWerhouse;
- public int currentLoad;
- public int wastedTurns;
- LinkedList<Integer> items;
- LinkedList<Integer> locations;
- public Drone(){
- currentWerhouse = 0;
- wastedTurns = 0;
- currentLoad = 0;
- items = new LinkedList<>();
- locations = new LinkedList<>();
- }
- // class DistanceComparator implements Comparator<Werhouse>{
- // int x;
- // int y;
- //
- // public DistanceComparator(int x2, int y2) {
- // x = x2;
- // y = y2;
- // }
- //
- // @Override
- // public int compare(Werhouse first, Werhouse second) {
- // int dist = Main.distance(x, y, first.x, first.y);
- // int dist2 = Main.distance(x, y, second.x, second.y);
- // if(first.items()==0) dist = Integer.MAX_VALUE;
- // if(second.items()==0) dist2 = Integer.MAX_VALUE;
- //
- // return dist - dist2;
- // }
- // }
- // public Werhouse findClosestWerhouse(int x, int y) {
- // TreeSet<Werhouse> houses= new TreeSet<Werhouse>(new DistanceComparator(x, y));
- //
- // for(int i = 0; i<Werhouse werhouse : Main.werhouses){
- // houses.add(werhouse);
- // }
- //
- // return houses.first();
- //
- // }
- public void deliver(int indeks) {
- int currentX = Main.werhouses[currentWerhouse].x;
- int currentY = Main.werhouses[currentWerhouse].y;
- while (!locations.isEmpty()) {
- int tmp = locations.removeFirst();
- int newX = Main.orders[tmp].x;
- int newY = Main.orders[tmp].y;
- wastedTurns += Main.distance(currentX, currentY, newX, newY);
- currentX = newX;
- currentY = newY;
- Main.log.append(String.format("%d D %d 1 %d\n", indeks, tmp, items.removeFirst()));
- Main.naredbi++;
- }
- //nov werhouse
- //TODO: print naredba
- Random r = new Random();
- currentWerhouse = r.nextInt(Main.werhouses.length);
- wastedTurns += Main.distance(currentX, currentY, Main.werhouses[currentWerhouse].x, Main.werhouses[currentWerhouse].y);
- }
- }
- public class Main {
- public static int PRODUCT_TYPES;
- public static Werhouse[] werhouses;
- public static Order[] orders;
- public static StringBuilder log;
- public static int naredbi;
- public static int distance(int x, int y, int xx, int yy) {
- xx-=x;
- xx = Math.abs(xx);
- xx*=xx;
- yy-=y;
- yy = Math.abs(yy);
- yy *=yy;
- return (int) Math.ceil(Math.sqrt(xx + yy)) ;
- }
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- int H, W, NUM_DRONES, TURNS, PAYLOAD, NUM_WERHOUSE;
- H = sc.nextInt();
- W = sc.nextInt();
- NUM_DRONES = sc.nextInt();
- Drone[] drones = new Drone[NUM_DRONES];
- for(int i = 0; i<NUM_DRONES; i++){
- drones[i] = new Drone();
- }
- TURNS = sc.nextInt();
- PAYLOAD = sc.nextInt();
- PRODUCT_TYPES = sc.nextInt();
- int[] weights = new int[PRODUCT_TYPES];
- for(int i = 0; i<PRODUCT_TYPES; i++){
- weights[i] = sc.nextInt();
- }
- NUM_WERHOUSE= sc.nextInt();
- werhouses = new Werhouse[NUM_WERHOUSE];
- for(int i = 0; i<NUM_WERHOUSE; i++){
- int x, y;
- x = sc.nextInt();
- y = sc.nextInt();
- werhouses[i] = new Werhouse(x, y);
- for(int j = 0; j<PRODUCT_TYPES; j++){
- werhouses[i].storage[j] = sc.nextInt();
- }
- }
- int NUM_ORDERS;
- NUM_ORDERS = sc.nextInt();
- orders = new Order[NUM_ORDERS];
- for(int i = 0; i<NUM_ORDERS; i++){
- int x = sc.nextInt();
- int y = sc.nextInt();
- int n = sc.nextInt();
- HashMap<Integer, Integer> hash = new HashMap<>();
- for(int j = 0; j<n; j++){
- int tmp = sc.nextInt();
- if(hash.containsKey(tmp)){
- Integer it = hash.remove(tmp);
- hash.put(tmp, it + 1);
- }
- else{
- hash.put(tmp, 1);
- }
- }
- orders[i] = new Order(x, y, hash);
- }
- //algoritam
- log = new StringBuilder();
- naredbi = 0;
- for(int i = 0; i < NUM_DRONES; i++){ //za seokoj dron
- Drone currentDrone = drones[i];
- while(currentDrone.wastedTurns < TURNS){ //dodeka ima benzin
- Werhouse currentWerhouse = werhouses[currentDrone.currentWerhouse];
- for(int j = 0; j < PRODUCT_TYPES; j++){ //za sekoj item
- if(currentWerhouse.storage[j] > 0){ // ako go ima vo currentWehouse
- for(int k = 0; k<NUM_ORDERS; k++){
- Order currentOrder = orders[k];
- if(currentOrder.products.containsKey(j) && currentOrder.products.get(j)>0 && PAYLOAD - currentDrone.currentLoad >= weights[i]){
- //stavi go itemot vo dronot
- currentDrone.currentLoad += weights[i]; //upgrade na tezinata na dronot
- // if(currentDrone.items.isEmpty() || currentDrone.items.getLast() != j){
- // currentDrone.wastedTurns++;
- // }
- // else{
- // k--;
- // }
- currentDrone.items.addLast(j);
- currentDrone.locations.addLast(k);
- Integer tmp = currentOrder.products.remove(j);
- currentOrder.products.put(j, tmp-1);
- currentWerhouse.storage[j]--;
- log.append(String.format("%d L 1 %d %d\n", i, j, currentDrone.currentWerhouse));
- naredbi++;
- }
- }
- }
- }
- currentDrone.deliver(i);
- }
- }
- System.out.println(naredbi);
- System.out.print(log);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement