6.14比赛河西周六下午教室3分析
比赛传送门:点击进入
T1:质数筛
错误原因: 1.思路错误: 1. 数据开小了2. 循环开小了
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,x;
bool f[780000910];
void init(){
f[0]=f[1]=1;
for(ll i = 2;i<=5000;i++){
if(f[i]) continue;
for(ll j = 2;j<=5000;j++){
f[i*j]=1;
}
}
}
int main(){
init();
cin >> n;
for(ll i = 1;i<=n;i++){
cin >> x;
if(f[x]==0) cout << x << ' ';
}
return 0;
}正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 1e5 + 10;
bool vis[N];
int n;
void shai() {
vis[1] = 1;
for (int i = 2; i <= 100000; i++) {
if (vis[i] == 0) {
for(int j = 2*i; j <= 100000; j+=i){
vis[j] = 1;
}
}
}
}
int main() {
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
shai();
cin >> n;
for (int i = 1, x; i <= n; i++) {
cin >> x;
if (vis[x] == 0) {
cout << x << " ";
}
}
return 0;
}正确思路: 先用埃氏筛标记再输入判断是否被标记
T2:陶陶摘苹果
错误原因: 1.思路错误: 1. 循环条件写错
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,s,g,h,ans;
struct node{
ll a,b;
}q[5100];
bool cmp(node x,node y){
return x.b<y.b;
}
int main(){
cin >> n >> s >> g >> h;
for(int i = 1;i<=n;i++){
cin >> q[i].a >> q[i].b;
}
sort(q+1,q+1+n,cmp);
for(int i = 1;i<=n&&s-q[i].b>0;i++){
if(g+h>=q[i].a) {
ans++;
s-=q[i].b;
}
}
cout << ans;
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
struct node {
int x, y;
}ap[5005];
bool cmp (node a, node b) {
return a.y < b.y;
}
int n, s, a, b, cnt;
int main()
{
cin >> n >> s >> a >> b;
for (int i = 1; i <= n; i++) {
cin >> ap[i].x >> ap[i].y;
}
sort(ap + 1, ap + 1 + n, cmp);
for (int i = 1; i <= n; i++) {
if (s >= ap[i].y && a + b >= ap[i].x) {
s -= ap[i].y;
cnt++;
}
}
cout << cnt;
return 0;
}正确思路: 先输入再排序然后循环判断
T3:单词的长度
错误原因: 1.细节错误: 1.一直输入
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
string s;
int main(){
cin >> s;
cout << s.size();
while(1) {
cin >> s;
cout << ',' << s.size();
}
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
bool b=false;
string s;
int main(){
while(cin>>s){
if(b){
cout<<",";
}else{
b=true;
}
cout<<s.size();
}
return 0;
}正确思路: while循环条件改为cin>>s,然后判断是否是第一个单词要不要输出逗号再输出长度
T4:迷宫出口
错误原因: 1.思路错误: 1.dfs使用错误
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n,ex,ey,bx,by,a[500][500];
bool vis[500][500];
bool ans=0;
void dfs(int x,int y){
if(vis[x][y]==1||a[x][y]==1) return ;
vis[x][y]=1;
if(x<1||y<1||x>n||y>n) return ;
if(x==ex&&y==ey) {
ans=1;
return ;
}
dfs(x+1,y);
dfs(x,y+1);
dfs(x-1,y);
dfs(x,y-1);
vis[x][y]=0;
}
int main(){
cin >> n;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
cin >> a[i][j];
}
}
cin >> bx >> by >> ex >> ey;
dfs(bx,by);
if(ans==1) cout << "YES";
else cout << "NO";
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
int n,m,ex,ey,bx,by;
int dx[5]={0,0,-1,1};
int dy[5]={-1,1,0,0};
char c[5000][5000];
bool f,vis[200][200];
void dfs(int x,int y){
if(x==ex&&y==ey) {
f=1;
return ;
}
for(int i = 0;i<4;i++){
int xx=x+dx[i],yy=y+dy[i];
if(xx>=1&&yy>=1&&xx<=n&&yy<=m&&vis[xx][yy]==0&&c[xx][yy]=='0'){
vis[xx][yy]=1;
dfs(xx,yy);
}
}
}
int main(){
cin >> n >> m;
for(int i = 1;i<=n;i++){
for(int j = 1;j<=m;j++){
cin >> c[i][j];
}
}
cin >> bx >> by >> ex >> ey;
dfs(bx,by);
if(f) cout << "YES";
else cout << "NO";
return 0;
}正确思路: 不回溯
T5:选数
错误原因: 1.思路错误: 1.标记提前回溯
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool isp(ll x){
if(x<=1) return 0;
for(ll i = 2;i<=sqrt(x);i++){
if(x%i==0) return 0;
}
return 1;
}
ll n,k,a[50],ans;
bool vis[50];
ll box[50];
void dfs(ll u,ll p){
if(u==n+1){
ll sum=0;
for(int i= 1;i<=n;i++){
if(vis[i]==1) {
sum+=box[i];
}
}
if(p!=k) return ;
if(isp(sum)==0) return ;
ans++;
return ;
}
vis[u]=1;
box[p]=a[u];
dfs(u+1,p+1);
vis[u]=0;
dfs(u+1,p);
}
int main(){
cin >> n >> k;
for(int i= 1;i<=n;i++) cin >> a[i];
dfs(0,0);
cout << ans;
return 0;
}
正确代码:
#include<bits/stdc++.h>
using namespace std;
bool isp(int x){
if(x<=1) return 0;
for(int i = 2;i<=sqrt(x);i++){
if(x%i==0) return 0;;
}
return 1;
}
bool vis[30];
int a[30],n,k,ans;
void dfs(int u){
if(u==n+1) {
int sum=0,mus=0;
for(int i = 1;i<=n;i++){
if(vis[i]==1) {
sum+=a[i];
mus++;
}
}
if(isp(sum)==0||mus!=k) return ;
ans++;
return ;
}
vis[u]=1;
dfs(u+1);
vis[u]=0;
dfs(u+1);
}
int main(){
cin >> n >> k;
for(int i = 1;i<=n;i++) cin >> a[i];
dfs(1);
cout << ans;
return 0;
}// 红烧洛谷 清蒸洛谷正确思路: 数量参数要在统计时判断
T6:海战
赛时代码:
(无)正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e5 + 10;
char mp[1005][1005];
int n, m;
int dx[] = {0,0,1,-1};
int dy[] = {1,-1,0,0};
void dfs(int x, int y){
for(int i=0; i<4; i++){
int xx=x+dx[i], yy=y+dy[i];
if(xx>0&&xx<=n&&yy>0&&yy<=m&&mp[xx][yy]=='#'){
mp[xx][yy] = '.';
dfs(xx, yy);
}
}
}
int main(){
cin >> n >> m;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
cin >> mp[i][j];
}
}
//检查是否出现非矩形
for(int i=1; i<=n-1; i++){
for(int j=1; j<=m-1; j++){
int x = 0;
if(mp[i][j]=='#') x++;
if(mp[i][j+1]=='#') x++;
if(mp[i+1][j]=='#') x++;
if(mp[i+1][j+1]=='#') x++;
if(x==3) {cout<<"Bad placement."; return 0;}
}
}
int cnt = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=m; j++){
if(mp[i][j]=='#'){
mp[i][j] = '.';
dfs(i, j);
cnt++;
}
}
}
cout << "There are " << cnt << " ships.";
return 0;
}正确思路: 先录入再判断正方形最后DFS
T7:全排列问题
赛时代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int box[50];
bool vis[50];
void dfs(int u){
if(u==n+1){
for(int i= 1;i<=n;i++){
cout << setw(5) << box[i];
}
cout << endl;
return ;
}
for(int i= 1;i<= n;i++){
if(vis[i]==1) continue;
vis[i]=1;
box[u]=i;
dfs(u+1);
vis[i]=0;
}
}
int main(){
cin >> n;
dfs(1);
return 0;
}
正确代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int n;
int box[50];
bool vis[50];
void dfs(int u){
if(u==n+1){
for(int i= 1;i<=n;i++){
cout << setw(5) << box[i];
}
cout << endl;
return ;
}
for(int i= 1;i<= n;i++){
if(vis[i]==1) continue;
vis[i]=1;
box[u]=i;
dfs(u+1);
vis[i]=0;
}
}
int main(){
cin >> n;
dfs(1);
return 0;
}
正确思路: DFS时要回溯(题目可以打表)
T8:
赛时代码:
(无)正确代码:
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N = 2e3 + 10;
int a[N][N], n, q;
int sum[N][N];
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin >> n;
for(int i=1; i<=n + 1; i++){
for(int j=1; j<=n + 1; j++){
if(i==n+1 && j==n+1) continue;
else if(i == n + 1 || j == n + 1) cin >> a[i][j];
else
{
char c; cin >> c;
if(c == 'R') a[i][j] = -1;
else a[i][j] = -2;
}
}
}
for(int i=1; i<=n + 1; i++){
for(int j=1; j<=n + 1; j++){
if(i<=n && j<=n)sum[i][j] = 1;
if(a[i][j-1] == -1) sum[i][j] += sum[i][j-1];
if(a[i-1][j] == -2) sum[i][j] += sum[i-1][j];
}
}
int ans = 0;
for(int i=1; i<=n; i++){
ans += sum[i][n+1] * a[i][n+1];
}
for(int j=1; j<=n; j++){
ans += sum[n+1][j] * a[n+1][j];
}
cout << ans << endl;
cin >> q;
while(q--){
int x, y;
cin >> x >> y;
if(a[x][y] == -1)
{
int xx = x, yy = y;
while(1){
if(a[xx][yy] == -1) {
yy++;
sum[xx][yy] -= sum[x][y];
} else {
xx++;
sum[xx][yy] -= sum[x][y];
}
if(xx==n+1 || yy==n+1) break;
}
a[x][y] = -2;
xx = x; yy = y;
while(1){
if(a[xx][yy] == -1) {
yy++;
sum[xx][yy] += sum[x][y];
} else {
xx++;
sum[xx][yy] += sum[x][y];
}
if(xx==n+1 || yy==n+1) break;
}
}
else
{
int xx = x, yy = y;
while(1){
if(a[xx][yy] == -1) {
yy++;
sum[xx][yy] -= sum[x][y];
} else {
xx++;
sum[xx][yy] -= sum[x][y];
}
if(xx==n+1 || yy==n+1) break;
}
a[x][y] = -1;
xx = x; yy = y;
while(1){
if(a[xx][yy] == -1) {
yy++;
sum[xx][yy] += sum[x][y];
} else {
xx++;
sum[xx][yy] += sum[x][y];
}
if(xx==n+1 || yy==n+1) break;
}
}
ans = 0;
for(int i=1; i<=n; i++){
ans += sum[i][n+1] * a[i][n+1];
}
for(int j=1; j<=n; j++){
ans += sum[n+1][j] * a[n+1][j];
}
cout << ans << endl;
}
return 0;
}正确思路: 用嵌套循环