CF:Problem 426B - Sereja and Mirroring 二分或者分治

这题解法怎么说呢,因为我是把行数逐步除以2暴力得到的答案,所以有点二分的意思,但是昨天琦神说是有点像分治的意思,反正总的来说:就是从大逐步细化找到最优答案。

但是昨晚傻B了,靠!多写了点东西,然后就错了,刚才一练习,拿昨晚的代码一看,就把6行代码删去就过了,靠!昨晚应该是脑子进水了!!!!!

昨晚的代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 4105
#define MN 105
#define INF 100004
#define eps 1e-7
using namespace std;
typedef long long ll;
int  n,m,i,j,k,a[MN][MN];
string s,ss;
int main()
{
    sc(n,m);
    for(i=0; i<n; i++)
        for(j=0; j<m; j++) sca(a[i][j]);
    if(n&1) cout<<n<<endl;
    else
    {
        int r=n/2,flag=1,ff=1;
        int aa=a[0][0];
        for(i=1; i<n; i++) //就是这个多判断了,因为自己给出的样列是1列,所以……靠,本来在下面的sum值那里已经处理了,然后忘了把这删了,所以就在第一列相同的时候出错了,导致昨晚没debug出来!嘛嘛呀!!!!
            if(aa!=a[i][0])
            {
                ff=0;
                break;
            }
        if(ff) cout<<1<<endl;
        else
        {
            int sum=0;
            while(r&&flag)
            {
                for(i=0; i<n&&flag; i+=r*2)
                {
                    for(j=i,k=i+r+r-1; j<k&&flag; j++,k--)
                    {
                        for(int p=0; p<m&&flag; p++)
                            if(a[j][p]!=a[k][p])
                                flag=0;
                    }
                }
                if(flag&&(r%2==0)) r/=2;
                if(flag&&(r&1)) sum++;
                if(sum>3) break;
                //cout<<"--"<<flag<<‘ ‘<<r<<endl;
            }
            if(!flag) cout<<r*2<<endl;
            else cout<<r<<endl;
        }
    }
    return 0;
}

AC代码:

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define PI acos(-1.0)
#define mem(a,b) memset(a,b,sizeof(a))
#define sca(a) scanf("%d",&a)
#define sc(a,b) scanf("%d%d",&a,&b)
#define pri(a) printf("%d\n",a)
#define lson i<<1,l,mid
#define rson i<<1|1,mid+1,r
#define MM 4105
#define MN 105
#define INF 100004
#define eps 1e-7
using namespace std;
typedef long long ll;
int  n,m,i,j,k,a[MN][MN];
string s,ss;
int main()
{
    sc(n,m);
    for(i=0; i<n; i++)
        for(j=0; j<m; j++) sca(a[i][j]);
    if(n&1) cout<<n<<endl;
    else
    {
        int r=n/2,flag=1,ff=1;
        int sum=0;
        while(r&&flag)
        {
            for(i=0; i<n&&flag; i+=r*2)
            {
                for(j=i,k=i+r+r-1; j<k&&flag; j++,k--)
                {
                    for(int p=0; p<m&&flag; p++)
                        if(a[j][p]!=a[k][p])
                            flag=0;
                }
            }
            if(flag&&(r%2==0)) r/=2;
            if(flag&&(r&1)) sum++;
            if(sum>3) break;
            //cout<<"--"<<flag<<‘ ‘<<r<<endl;
        }
        if(!flag) cout<<r*2<<endl;
        else cout<<r<<endl;
    }
    return 0;
}

CF:Problem 426B - Sereja and Mirroring 二分或者分治,码迷,mamicode.com

时间: 2024-08-05 06:22:14

CF:Problem 426B - Sereja and Mirroring 二分或者分治的相关文章

CF:Problem 425A - Sereja and Swaps 区间交换最大值

这题比赛的时候不会做,原来是区间暴力. 其实理解起来也觉得挺简单的,可能是看题的时候被交换这个思想束缚了自己的解题吧,所以一直想不出什么好的做法,看了别人的解题茅舍顿开-- 解法:就是在这个数列中先选出一段我们要求的区间,如果在中间取的这段的话,那旁边两段就是剩余的段,也就是我们需要至少k次交换剩余段中最大的值与刚开始选出的段交换最小的值,然后求这选出的这段的和,如此下去更新最大值就得到结果了.选出的段为 [ i , j ],剩余的段就是:[ 0 , i - 1 ] 与 [ j+1 , n -

CF E. Till I Collapse 整体二分+根号分治

本来模拟赛想出这个来的,但是感觉不太友好啊~ 主席树可以直接屎过去,但是空间消耗很大. 考虑用整体二分: code: #include <bits/stdc++.h> #define N 100003 #define ll long long #define setIO(s) freopen(s".in","r",stdin) using namespace std; int A[N],C[N],ans[N],n; int solve(int x) {

CF:Problem 427C - Checkposts强连通Tarjan算法

这题昨晚做了,刚开始看题的时候没想出好法子,然后就看D题了,一看D题发现是后缀数组,然后就把模板改了点就交了上去--不幸的是--WA了,然后重新看题,果然题目看漏了--不仅要用后缀数组和前缀数组求出公共子缀,还要是求最小的,而且在每个串里都不能重复的,这下就想了会不会了,然后看见大帝C过了,然后就重新回来看C了,看了会终于明天怎么做了. C题意:给个图,然后每个点都有权值,求最小的花费及方案数:最小的花费是这样的:因为是建立一个岗哨,然后这个岗哨可以管哪些呢,可以管 i = j 的,或者可以从

Codeforces Round #243 (Div. 2) B. Sereja and Mirroring

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n,m; cin >> n >> m; vector<vector<int> > a(n,vector<int>(m,0)); for(int i = 0; i < n; ++ i){ for(int j = 0 ;

CF:Problem 427C - Checkposts强连通 Tarjan算法

tarjan算法第一题 喷我一脸....把手写栈的类型开成了BOOL,一直在找错... #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define maxn 100005 const int MOD=1000000007; using namespace std; struct node { int to,next; }edge[maxn*3]; int

Codeforces Round #424 (Div. 2, rated, based on VK Cup Finals) Problem D (Codeforces 831D) - 贪心 - 二分答案

There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebo

CF 714D Searching Rectangles 交互题 二分

题意:交互题,已知一个n*n的地图,电脑藏了两个不相交的矩形(矩形的边和坐标轴平行).每次向电脑询问左下[x1,y1]右上[x2,y2]的矩形内完全包含多少个藏着的矩形.n<=2^16.请在200次内猜出两个矩形左下,右上坐标. 先二分出两个不相交矩形的分界线,要么横着要么竖着.接在在每一快中二分出矩形的四条边即可(最左,右,上,下边界) #include <bits/stdc++.h> using namespace std; int n,ans[20],cnt=0; inline i

CF:Problem 427C - Checkposts良好的沟通 Tarjan算法

tarjan算法的第一个问题 喷我的脸....手写叠式开成BOOL,我一直在找错了... #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define maxn 100005 const int MOD=1000000007; using namespace std; struct node { int to,next; }edge[maxn*3]; int

【CF】C. Glass Carving(二分 + 树状数组 + 优先队列 + 数组计数)

这题简直蛋疼死..... A了一下午 #include<cstdio> #include<queue> #include<cstring> #include<algorithm> using namespace std; typedef long long LL; const int maxn = 200005; int h,w,n; int C1[maxn],C2[maxn]; int vis1[maxn] = {0},vis2[maxn] = {0};