C問題 Separated Lunch
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <atcoder/all>
#define INF (1LL<<30)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; }
else return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; }
return false;
}
long long popcount(long long n) {
long long ans = 0;
for (int x = 0; x <= 60; x++) if (((n >> x) & 1) == 1) ans++;
return ans;
}
struct TreeNode {
std::vector<int> Children;
bool IsVisited = false;
};
int main() {
int N;
std::cin >> N;
std::vector<long long> K(N);
for (int n = 0; n < N; n++) std::cin >> K[n];
long long ans = INF;
for (int i = 0; i < (1 << N); i++) {
long long a = 0, b = 0;
for (int n = 0; n < N; n++) {
if (((i >> n) & 1) == 1) a += K[n];
else b += K[n];
}
chmin(ans, std::max(a, b));
}
std::cout << ans << std::endl;
}
2のN乗を管理するときはbitを使いましょうねということを失念してたね。
きちんと自分の引き出しを整理して覚えておきましょう
D問題 Laser Marking
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <atcoder/all>
#include <math.h>
#include <iomanip>
#define INF (1LL<<30)
template<class T> inline bool chmin(T& a, T b) {
if (a > b) { a = b; return true; }
else return false;
}
template<class T> inline bool chmax(T& a, T b) {
if (a < b) { a = b; return true; }
return false;
}
long long popcount(long long n) {
long long ans = 0;
for (int x = 0; x <= 60; x++) if (((n >> x) & 1) == 1) ans++;
return ans;
}
struct TreeNode {
std::vector<int> Children;
bool IsVisited = false;
};
int main() {
int N, S, T;
std::cin >> N >> S >> T;
std::vector<std::pair<std::pair<int, int>, std::pair<int, int>>> L;
for (int n = 0; n < N; n++) {
int a, b, c, d;
std::cin >> a >> b >> c >> d;
L.push_back({ {a,b}, {c,d} });
}
auto caldD = [](int x, int y, int xx, int yy) {
//return std::sqrtl((long double)((xx - x) * (xx - x) + (yy - y) * (yy - y)));
return std::sqrt((long double)((xx - x) * (xx - x) + (yy - y) * (yy - y)));
};
std::vector<int> idx(N);
for (int n = 0; n < N; n++) idx[n] = n;
double ans = INF;
//while (0 < L.size()) {
do{
for (int p = 0; p < (1 << N); p++) {
int tmpX = 0, tmpY = 0;
//int tmpL = 0;
//long double tmpD = INF;
int se = 3;
double tmpAns = 0;
for (int i = 0; i < L.size(); i++) {
double tmpD = INF;
if ((p & (1 << idx[i])) == 0) {
tmpD = caldD(tmpX, tmpY, L[idx[i]].first.first, L[idx[i]].first.second);
se = 1;
}
else {
tmpD = caldD(tmpX, tmpY, L[idx[i]].second.first, L[idx[i]].second.second);
se = 2;
}
tmpAns += (tmpD / S);
tmpAns += (caldD(L[idx[i]].first.first, L[idx[i]].first.second, L[idx[i]].second.first, L[idx[i]].second.second) / T);
if (se == 1) {
tmpX = L[idx[i]].second.first;
tmpY = L[idx[i]].second.second;
}
else if (se = 2) {
tmpX = L[idx[i]].first.first;
tmpY = L[idx[i]].first.second;
}
}
chmin(ans, tmpAns);
}
//int tmpX = 0, tmpY = 0;
////int tmpL = 0;
////long double tmpD = INF;
//int se = 3;
//long double tmpAns = 0;
//for (int i = 0; i < L.size(); i++) {
// long double tmpD = INF;
// if (chmin(tmpD, caldD(tmpX, tmpY, L[idx[i]].first.first, L[idx[i]].first.second))) {
// //tmpL = i;
// se = 1;
// }
// if (chmin(tmpD, caldD(tmpX, tmpY, L[idx[i]].second.first, L[idx[i]].second.second))) {
// //tmpL = i;
// se = 2;
// }
// tmpAns += (tmpD / S);
// tmpAns += (caldD(L[idx[i]].first.first, L[idx[i]].first.second, L[idx[i]].second.first, L[idx[i]].second.second) / T);
// if (se == 1) {
// tmpX = L[idx[i]].second.first;
// tmpY = L[idx[i]].second.second;
// }
// else if (se = 2) {
// tmpX = L[idx[i]].first.first;
// tmpY = L[idx[i]].first.second;
// }
//}
//chmin(ans, tmpAns);
///*ans += (tmpD / S);
//ans += (caldD(L[tmpL].first.first, L[tmpL].first.second, L[tmpL].second.first, L[tmpL].second.second) / T);
//if (se == 1) {
// tmpX = L[tmpL].second.first;
// tmpY = L[tmpL].second.second;
//}
//else if (se = 2) {
// tmpX = L[tmpL].first.first;
// tmpY = L[tmpL].first.second;
//}*/
////L.erase(begin(L) + tmpL);
} while (std::next_permutation(begin(idx),end(idx)));
std::cout << std::fixed << std::setprecision(20) << ans << std::endl;
}線の選び方+線の方向を全探索する。
問題文をきちんと捉え、探索方針を適切に策定する力が大事。
本番では線の方向を考慮できなかった。。




コメント