总结:第一天的三道题除了第三题代码长度有点超过我的接受范围之外,一二题难度还是比较适中滴,只可惜第二题我打的暴力忘了小小的优化一下,结果没得全分,有点难过了。
T1
这道题的思路就是输入过后倒着寻找符合条件的地毯,因为越后放的地毯在越上面
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN = 1e5+5, INF = 1e5+1;
int n;
int x0, y0;
int answer;
struct name {
int sta_x, sta_y;
int end_x, end_y;
}p[MAXN];
int main()
{
// freopen("carpet.in","r",stdin);
// freopen("carpet.out","w",stdout);
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
scanf("%d%d%d%d", &p[i].sta_x, &p[i].sta_y, &p[i].end_x, &p[i].end_y);
p[i].end_x = p[i].end_x + p[i].sta_x;
p[i].end_y = p[i].end_y + p[i].sta_y;
}
scanf("%d%d", &x0, &y0);
answer = INF;
for(int i = n; i >= 1; i--)
{
if(x0>=p[i].sta_x && x0<=p[i].end_x && y0>=p[i].sta_y && y0<=p[i].end_y)
{
answer = i;
break;
}
}
if(answer == INF) printf("-1");
else cout << answer << endl;
return 0;
}
T2
这道题我们先枚举第二个客栈的位置,然后去倒推第一个客栈的位置。当时做这道题的时候我好像是枚举的第一个客栈的位置,然后再去枚举第二个客栈,就只有60分,不过我想如果枚举第一个客栈能稍微优化一下应该也是能勉强卡过的
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 2e5+5;
int n, k, p, b, ans;
int a[MAXN], co[MAXN][55], jp[MAXN];
int main()
{
cin >> n >> k >> p;
int i = 0, j = 0;
for(i=1;i<=n;i++)
{
cin >> a[i] >> b;
for(j = 0; j < k; j++)
co[i][j] = co[i-1][j];
co[i][a[i]]++;
if(b <= p) jp[i] = i;
else jp[i] = jp[i-1];
}
for(i = 1; i <= n; i++)
{
ans += co[jp[i]][a[i]];
if(jp[i] == i) ans--;
}
printf("%d\n", ans);
return 0;
}
T3
这道题代码比较长,还是比较好想的
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
int a[10][10], Tmp, jie1[10], jie2[10], jie3[10];
void check()
{
bool flag = true;
for(int i = 1; i <= 5; i++)
if(a[i][1]) flag = false;
if(flag)
{
for(int i = 1; i <= n; i++)
printf("%d %d %d\n", jie1[i]-1, jie2[i]-1, jie3[i]);
exit(0);
}
}
bool func()
{
bool s[10][10] = {0};
bool flag = false;
for(int i = 1; i <= 5; i++)
{
for(int j = 1; a[i][j]; j++)
{
int k = i+1;
for( ; k <= 5; k++)
if(a[k][j] != a[i][j]) break;
if(k-i >= 3)
{
for(int l = i; l < k; l++)
s[l][j] = true;
flag = true;
}
k = j+1;
for( ; k <= 7; k++)
if(a[i][j] != a[i][k]) break;
if(k-j >= 3)
{
for(int l = j; l < k; l++)
s[i][l] = true;
flag = true;
}
}
}
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 7; j++)
if(s[i][j])
a[i][j] = 0;
if(!flag) return false;
for(int i = 1; i <= 5; i++)
for(int j = 1; j <= 7; j++)
if(a[i][j] == 0){
int k = j+1;
for( ; k <= 7; k++)
if(a[i][k]) break;
a[i][j] = a[i][k];
a[i][k] = 0;
}
return true;
}
void dfs(int x)
{
for(int i = 1; i <= 5; i++)
{
for(int j = 1; a[i][j]; j++)//
{
if(i!=5 && a[i][j]!=a[i+1][j] && a[i+1][j])
{
int temp_1[10][10];
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
temp_1[k][l] = a[k][l];
}
}
int temp_2 = a[i][j];
a[i][j] = a[i+1][j];
a[i+1][j] = temp_2;
jie1[x] = i;
jie2[x] = j;
jie3[x] = 1;
while(func()) ;
if(x == n) check();
else dfs(x+1);
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
a[k][l] = temp_1[k][l];
}
}
}
if(i!=5 && !a[i+1][j])
{
int temp_1[10][10];
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
temp_1[k][l] = a[k][l];
}
}
int m;
for(m = j-1; m >= 1; m--)
if(a[i+1][m]) break;
m++;
a[i+1][m] = a[i][j];
a[i][j] = 0;
jie1[x] = i;
jie2[x] = j;
jie3[x] = 1;
for(int p = j; p <= 6; p++)
{
a[i][p] = a[i][p+1];
a[i][p+1] = 0;
}
while(func()) ;
if(x == n) check();
else dfs(x+1);
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
a[k][l] = temp_1[k][l];
}
}
}
if(i!=1 && !a[i-1][j])
{
int temp_1[10][10];
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
temp_1[k][l] = a[k][l];
}
}
int m;
for(m = j-1; m >= 1; m--)
if(a[i-1][m]) break;
m++;
a[i-1][m] = a[i][j];
a[i][j] = 0;
jie1[x] = i;
jie2[x] = j;
jie3[x] = -1;
for(int p = j; p <= 6; p++)
{
a[i][p] = a[i][p+1];
a[i][p+1] = 0;
}
while(func()) ;
if(x == n) check();
else dfs(x+1);
for(int k = 0; k <= 9; k++)
{
for(int l = 0; l <= 9; l++)
{
a[k][l] = temp_1[k][l];
}
}
}
}
}
}
int main()
{
cin >> n;
for(int i = 1; i <= 5; i++)
{
int tot = 0;
while(cin >> Tmp)
{
if(!Tmp) break;
a[i][++tot] = Tmp;
}
}
dfs(1);
printf("-1");
return 0;
}