commit 35b3ddc0210eb54074926d261d5e1f2af0006d64
parent d54dabb31cf1562412a91723331e1fb805fe2ecf
Author: Andrew Laack <andrew@laack.co>
Date: Mon, 9 Nov 2026 23:24:33 -0600
Started a dp problem
Diffstat:
4 files changed, 174 insertions(+), 0 deletions(-)
diff --git a/comp/wip/looking-for-order/ex1.in b/comp/wip/looking-for-order/ex1.in
@@ -0,0 +1,4 @@
+0 0
+2
+1 1
+-1 1
diff --git a/comp/wip/looking-for-order/ex2.in b/comp/wip/looking-for-order/ex2.in
@@ -0,0 +1,5 @@
+1 1
+3
+4 3
+3 4
+0 0
diff --git a/comp/wip/looking-for-order/main.cpp b/comp/wip/looking-for-order/main.cpp
@@ -0,0 +1,82 @@
+#include <iostream>
+#include <climits>
+#include <bitset>
+#include <vector>
+
+// - thoughts:
+// - there are objects in a room that have to be placed in a container
+// - the girl can only carry at most two objects at a time
+// - container can't move
+// - if an object is picked up, it must be moved directly to the handbag, not pickup / set down
+// - cover distance between two objects in time equal to squared length of the segment connecting the objects
+// - euclidean distance squared
+// - girl starts at the handbag
+// - find operations that minimizes time to get all objects in container
+
+// this feels dp.
+
+// what's the state?
+ // s(visited, holding, position)
+ // where visited is a bitmask
+ // at most 24 bits
+
+using namespace std;
+
+int distance(int x1,int y1,int x2,int y2) {
+ return ((x2-x1) * (x2-x1)) + ((y2-y1) * (y2-y1));
+}
+
+int solve(vector<int>& xVals, vector<int>& yVals, bitset<24> current, int holding, int x, int y, int cx, int cy) {
+
+ int shortest = INT_MAX;
+ bool found = false;
+
+ if(holding < 2) {
+ for(int i = 0; i < xVals.size(); ++i) {
+ if(current[i] == 0) {
+ bitset<24> updated = current;
+ updated[i] = 1;
+ int dist = distance(x,y,xVals[i], yVals[i]);
+ int cv = solve(xVals,yVals,updated, holding+1, xVals[i],yVals[i], cx, cy);
+ shortest = min(cv+dist,shortest);
+ found = true;
+ }
+ }
+ }
+
+ if(holding > 0) {
+ int dist = distance(x,y,cx,cy);
+ int cv = solve(xVals,yVals,current, 0, cx,cy, cx, cy);
+ shortest = min(cv+dist,shortest);
+ found = true;
+ }
+
+ if(!found) {
+ return 0; // done
+ }
+
+ return shortest;
+}
+
+int main() {
+ int n, hx, hy, cx, cy = 0;
+
+ cin >> hx;
+ cin >> hy;
+
+ cin >> n;
+
+ vector<int> xVals{};
+ vector<int> yVals{};
+
+ for(int i = 0; i < n; ++i) {
+ cin >> cx;
+ cin >> cy;
+ xVals.push_back(cx);
+ yVals.push_back(cy);
+ }
+
+ bitset<24> current {};
+
+ cout << solve(xVals,yVals,current, 0, hx,hy, hx, hy) << endl;
+}
diff --git a/comp/wip/looking-for-order/problem.txt b/comp/wip/looking-for-order/problem.txt
@@ -0,0 +1,83 @@
+C. Looking for Order
+time limit per test
+4 seconds
+memory limit per test
+512 megabytes
+
+Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess — all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag — her inherent sense of order does not let her do so.
+
+You are given the coordinates of the handbag and the coordinates of the objects in some Сartesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period.
+Input
+
+The first line of the input file contains the handbag's coordinates xs, ys. The second line contains number n (1 ≤ n ≤ 24) — the amount of objects the girl has. The following n lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer.
+Output
+
+In the first line output the only number — the minimum time the girl needs to put the objects into her handbag.
+
+In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to n), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them.
+
+---
+
+- thoughts:
+ - there are objects in a room that have to be placed in a container
+ - the girl can only carry at most two objects at a time
+ - container can't move
+ - if an object is picked up, it must be moved directly to the handbag, not pickup / set down
+ - cover distance between two objects in time equal to squared length of the segment connecting the objects
+ - euclidean distance squared
+ - girl starts at the handbag
+ - find operations that minimizes time to get all objects in container
+
+---
+
+examples
+
+0 0
+2
+1 1
+-1 1
+
+handbag location = 0,0
+n = 2
+object 1 location = 1,1
+object 2 location = -1,1
+
+0,0 -> 1,1 -> -1,1 -> 0,0
+
+distances at each step:
+
+(1**2 + 1 ** 2) + (2**2 + 0) + (1**2 + 1 ** 2)
+
+2 + 4 + 2 = 8
+
+sum = 8
+0 1 2 0
+
+(we start at zero, get 1, get 2, end at 0)
+
+---
+
+1 1
+3
+4 3
+3 4
+0 0
+
+hb location = 1,1
+o1 = 4,3
+o1 = 3,4
+o2 = 0,0
+
+do we always want to pickup as many as we can? not necessarily because in some cases where there are > 3 elements we want to get one and then the other two in another swoop
+
+1,1 -> 3,4 -> 4,3 -> 1,1 -> 0,0 -> 1,1
+
+2, 3
+1, 1
+3, 2
+1, 1
+1, 1
+
+4 + 9 + 1 + 1 + 9 + 4 + 1 + 1 + 1 + 1 = 32
+
+0 2 1 0 3 0