Codeforces Round#288(Div.2)
来源:互联网 发布:千里眼淘宝版下载 编辑:程序博客网 时间:2024/05/17 00:54
A. Pasha and Pixels
题意:有一个n*m的矩形格阵,刚开始的时候都是白色的。现在每次进行一个操作,把白方块或黑方块(已变颜色)变为黑色。注意变为黑色后不能转化为白色。输出最早形成2*2黑色正方形的步数。
题解:很简单的思路,数组标记,调用函数判断。有一点需要注意一下,在使用bits/stdc++.h时包含所有c++内头文件,但是c里的输入输出不在其内,所以一些c里的还是需要添加的。错了两次,一次是编译错误,一次是疏忽,还是得再细心一点。
附代码:
#include <iostream>#include <cstring>#include <stdio.h>#include <stdlib.h>#define MAX_N 1005using namespace std;int maze[MAX_N][MAX_N];bool if_form(int x,int y);int main(){ int n,m,k; int x,y; int result=0; cin>>n>>m>>k; memset(maze,0,sizeof(maze)); for( int i = 1; i <= k; i++ ) { cin>>x>>y; if( maze[x][y] == 1 ) continue; maze[x][y] = 1; if( if_form(x,y) && result == 0) result = i; } printf("%d\n",result); return 0;}bool if_form(int x,int y){ if(maze[x-1][y]==1 && maze[x][y+1]==1 && maze[x-1][y+1]==1) return true; else if(maze[x+1][y]==1 && maze[x][y+1]==1 && maze[x+1][y+1]==1) return true; else if(maze[x+1][y]==1 && maze[x][y-1]==1 && maze[x+1][y-1]==1) return true; else if(maze[x-1][y]==1 && maze[x][y-1]==1 && maze[x-1][y-1]==1) return true; else return false;}
B. Anton and currency you all know
题意:给你一个2~10^5位的奇数,需要你通过交换大数里的两位得到一个最大的偶数,不能得到的话输出-1.
题解:这道题纠结了很久,一直到最后都没有解出来。现在再回过头看这道题,我觉得还是要有一个清晰地头脑,分析现有的条件,逐一对应的去寻找最合适的数据结构与算法。首先,题目要求500ms,比一般都小,这就要求我们要很注意数据的操作,精简操作步骤,防止超时;其次,大数据,10^5,不可能用int 或 long long 去存储,所以只能用数组去存储,char型数组是最合适的;最后题目要求最大的偶数,既然时间复杂度上要非常注意,如何比较去得到最大呢?最初想对每一个偶数位进行操作比较,尝试了很久依然会超时。很费解。最后回归到本质,既然想要求最大的偶数,我们从最高位向右比较,如果遇到比最后一位小的偶数,直接替换之后break,如果所有偶数都大于最后一位,交换最后一个偶数,这样得到的偶数是最小的。只需要对每一位比较不储存,最后交换一次,时间复杂度够用。但是究竟500ms的时间复杂度能承受多大还是很模糊,还是要请教一下学长。。。
附代码:
#include <bits/stdc++.h>#include <stdio.h>#include <stdlib.h>#define MAX_N 100005using namespace std;int show[12];char save[MAX_N];char num[MAX_N];long long length;int pos;bool change(int x,int y);int main(){ cin>>num; memset(show,0,sizeof(show)); length=strlen(num); save[0]='0'; pos=-1; for( int i = 0; i < length-1; i++) { if( (num[i]-'0')%2==0 ) { pos = i; if( num[length-1] > num[i] ) break; } } swap(num[pos],num[length-1]); if( pos== -1 ) printf("-1\n"); else printf("%s\n",num); return 0;}bool change(int x,int y){ swap(num[x],num[y]); if( num[pos] > save[pos] ) { if( strlen(save) != 1 ) { swap(save[pos],save[y]); swap(save[x],save[y]); pos = x; } else { strcpy(save,num); pos=x; } } swap(num[x],num[y]); return true;}
这次感觉要比上次难,只做出来了一道题。但是还好吧,慢慢进步,自己需要学的还很多,每一次都总结一下经验,收获一些东西,挺好的。加油!!
- Codeforces Round #288 (Div. 2)
- Codeforces Round#288(Div.2)
- Codeforces Round #288 (Div. 2)
- Codeforces Round #288 (Div. 2)
- Codeforces Round #288 (Div. 2)
- Codeforces Round #288 (Div. 2)
- Codeforces Round #288 (Div. 2)
- Codeforces Round #288 (Div. 2) 合集
- Codeforces Round #288 (Div. 2) ABC
- Codeforces Round #288 (Div. 2) 总结
- Codeforces Round #288 (Div. 2) 题解
- Codeforces Round #102 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #103 (Div. 2)
- Codeforces Round #104 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #105 (Div. 2)
- Codeforces Round #107 (Div. 2)
- 实现HTTP断点续传下载工具(附源代码)
- Can not perform this action after onSaveInstanceState
- 互联网创业准备
- cordova调用Notification插件部分手机报错JNI ERROR (app bug): accessed stale local reference
- shell 函数返回值接收问题
- Codeforces Round#288(Div.2)
- 二叉查找树
- c#序列化和反序列化
- Linux下程序调试相关工具与技巧
- 对面向切面编程的简单理解
- 说说做二飞日志的目的
- 玻璃瓶 给产品穿上最靓丽的“外衣”
- hdu 3516 Tree Construction(四边形优化)
- java反射测试