[Offer收割] 编程练习赛1

时间:2021-12-14 13:00:41
A HihoCoder 1268 九宫

思路:

  一般类似于数独的题目都是使用回溯解决,这里由于题目数据较小同样可以直接DFS得出结果。这里我用了一个偷懒的方法(next_permutation),直接求出全排列,然后将位置一一对应。

// Asimple
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<" = "<<a<<endl
#define sysp system("pause")
using namespace std;
typedef long long ll;
const int maxn = + ;
ll T, n, sum, num, m, t, len, ans;
int a[] = {, , , , , , , , };
int res[][], Map[][], temp[][]; bool check() {
if(
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[] &&
a[]+a[]+a[] == a[]+a[]+a[]
)
return true;
return false;
} void input() {
for(int i=; i<; i++) {
for(int j=; j<; j++) {
scanf("%d", &Map[i][j]);
}
}
int flag = ;
do{
if( check() ) {
bool f = true;
for(int i=; i<; i++) {
for(int j=; j<; j++) {
temp[i][j] = a[i*+j];
if( Map[i][j] && Map[i][j] != temp[i][j] ) {
f = false;
break;
}
}
if( !f ) break;
}
if( f ) {
if( flag == ) {
flag = ; break;
} else flag = ;
for(int i=; i<; i++) {
for(int j=; j<; j++) {
res[i][j] = temp[i][j];
}
}
}
}
if( flag == ) break;
}while( next_permutation(a, a+));
if( flag == ) printf("Too Many\n");
else {
for(int i=; i<; i++) {
for(int j=; j<; j++) {
printf("%d", res[i][j]);
if( j == ) printf("\n");
else printf(" ");
}
}
}
//sysp;
} int main() {
input();
return ;
}
B HihoCoder 1269 优化延迟

思路:

  看到题目,第一眼就看到了给出的运行时间10s,果断一发暴力TLE。随后仔细看了下题目,发现当K越大,得出的结果也就越小,这个不是裸二分吗,然后直接上手,wa10+次,最后才发现自己的get_sp竟然是返回int,还以为是自己的二分有问题,改了好几版,-_-||。补充一下,k为什么不能为0呢?虽然为0时相当于没加缓冲。

// Asimple
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<" = "<<a<<endl
#define sysp system("pause")
using namespace std;
typedef long long ll;
const int maxn = + ;
ll T, n, sum, num, m, t, len, ans;
ll a[maxn]; ll get_sp(int cnt) {
priority_queue<ll> q;
ll sp = , c = , v;
int i = ;
while( !q.empty() || i < n ) {
while( q.size()<cnt && i<n ) q.push(a[i++]);
v = q.top(); q.pop();
sp += c*v;
c ++;
if( sp > num ) return num + ;
}
return sp;
} void input() {
scanf("%lld %lld", &n, &num);
ll sp = ; ans = -;
for(int i=; i<n; i++) scanf("%lld", &a[i]);
ll low = , high = maxn;
while( low <= high ) {
ll mid = (low+high)/;
if( get_sp(mid)<=num ) {
ans = mid;
high = mid-;
}
else low = mid+;
}
printf("%lld\n", ans);
//sysp;
} int main() {
input();
return ;
}

附上自己wa的截图:

[Offer收割] 编程练习赛1

C HihoCoder 1270 建造基地
 思路:
  这道题不由的让我想起了当初做区域赛的网络赛的场景,这完全是道读题目的题,题目看懂了其实还挺简单的。
简单来说就是一个类完全背包的问题,每一层在修建的时候都是在要花最小的代价去满足建筑值。当一层修建完成之后,便是将金属的建筑值更新。
// Asimple
#include <bits/stdc++.h>
#define debug(a) cout<<#a<<" = "<<a<<endl
#define sysp system("pause")
using namespace std;
typedef long long ll;
const int maxn = + ;
const ll INF = <<;
const ll mod = ;
ll T, n, sum, num, m, t, len, ans, k;
ll dp[maxn], a[maxn], b[maxn]; void input() {
while( cin >> T ) {
while( T -- ) {
ans = ;
cin >> n >> m >> k >> t;
for(int i=; i<=m; i++) cin >> a[i];
for(int i=; i<=m; i++) cin >> b[i];
for(int u=; u<=n; u++) {
for(int i = ; i<maxn; i++) dp[i] = INF;
dp[] = ;
for(int i=; i<=m; i++) {
for(int j=; j<=k; j++) {
if( j + b[i] > k ) dp[k] = min(dp[k], dp[j] + a[i]);
else dp[j+b[i]] = min(dp[j+b[i]], dp[j] + a[i]);
}
}
ans += dp[k];
for(int v=; v<=m; v++) b[v] /= t;
}
if( ans >= INF ) cout << "No Answer" << endl;
else cout << ans << endl;
}
}
//sysp;
} int main() {
input();
return ;
}
D HihoCoder 1271 舰队游戏

暂无思路。看了题解说是用贪心+状压过,状态压缩是个好东西,可惜我不会。

AC博客1:https://blog.csdn.net/qq_32400847/article/details/51087220

AC博客2:https://blog.csdn.net/tc_to_top/article/details/50825418