被自己蠢哭了。。。。

250-point problem

国际象棋棋盘上给出两个坐标,问象从一个走到还有一个最少要几步。

黑格象仅仅能走黑格,白格象仅仅能走白格,仅仅要推断两个坐标的颜色是否同样就能推断是否可达,观察棋盘能够发现坐标的奇偶性决定了格子的颜色;可达的情况下最多两步就能达到,所以仅仅要推断格子是否在同一条斜线上即可了。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm
#define maxn using namespace std; class BishopMove
{
public:
int howManyMoves(int r1, int c1, int r2, int c2)
{
if (r1==r2 && c1==c2) return 0;
if (((r1+c1)&1) == ((r2+c2)&1))
{
if (abs(r1-r2)==abs(c1-c2)) return 1; return 2;
} return -1;
}
}; int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE return 0;
}

500-point problem

给出一个括号序列,X可被括号替换,问这串序列是否可能合法。由于当时没看见X的数量不超过5就用了区间DP,有一处下标竟然没控制好结果RE。。。。事实上能够用DFS求出全部可能的序列,然后用栈推断;区间DP仅仅要求出对这段序列最少加多少括号使其合法即可,假设为0就是合法的,要注意对匹配括号的推断。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm
#define maxn using namespace std; class BracketExpressions
{
public:
bool check(char x,char y)
{
if (x=='(' && y==')' || x=='['&& y==']' ||x=='{' && y=='}')
return true; if (x=='X' && (y==']' || y=='}' || y==')' || y=='X')) return true; if (y=='X' && (x=='(' || x=='[' || x=='{')) return true; return false;
}
string ifPossible(string str)
{
int l=str.size(); int dp[60][60];
memset(dp,0,sizeof dp);
for(int i = 0; i<l; i++)
{
dp[i][i] = 1;
} for (int m=1;m<l;m++)
{
for (int i=0;i<l;i++)
{
int j=i+m;
if (j>=l) break;
dp[i][j]=INF;
if (check(str[i],str[j]))
dp[i][j]=min(dp[i][j],dp[i+1][j-1]);
for (int k=i;k<j;k++)
{
dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
}
}
} if (dp[0][l-1]==0) return "possible";
else
return "impossible";
}
}; int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE return 0;
}

1000-point problem

时间不够,比赛的时候没来得及敲完。。。。。

题意:

给出一个有n个元素的集合E={ x | 0<=x<n , x为整数 },和一段序列f={ xi | 0<=xi,i<n },对于集合E的子集S,有随意i属于S,f[i]=xi 也属于S,问这种子集有多少个。

分析:

这事实上是个图论。

对于每一个i,都相应一个f[i]=xi,假设我取i,那我必然也要取f[i]=xi,即i依赖于f[i]=xi。于是建立有向边i---->f[i]=xi;对于图中的每一个强联通分量,这个分量中的全部点一定是同一时候取的。我们能够进行强联通缩点(tarjan),剩下的DAG中每一个点都有取和不取两种关系。应用乘法原理和加法原理,反向建图后通过树形DP求得最后的方案数:对于节点u,取这个节点的方案数是它全部子节点的方案数相乘,不取这个节点的方案数为1。

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define maxm 555555
#define maxn 55 using namespace std; int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
long long w[maxn];
int n; int pre[maxn],low[maxn],sccno[maxn];
int st[maxn],top;
int dfs_clock,scc_cnt; int deg[maxn]; void tarjan_dfs(int _u)
{
pre[_u]=low[_u]=++dfs_clock;
st[++top]=_u;
for (int e=fir[_u];e!=-1;e=nex[e])
{
int _v=v[e];
if (pre[_v]==0)
{
tarjan_dfs(_v);
low[_u]=min(low[_u],low[_v]);
}
else
{
if (sccno[_v]==0)
low[_u]=min(low[_u],pre[_v]);
}
} if (low[_u]==pre[_u])
{
scc_cnt++;
while (true)
{
int x=st[top--];
sccno[x]=scc_cnt;
if (x==_u) break;
}
}
} void find_scc()
{
dfs_clock=scc_cnt=0;
top=-1;
memset(sccno,0,sizeof sccno);
memset(pre,0,sizeof pre);
for (int i=0;i<n;i++)
{
if (pre[i]==0) tarjan_dfs(i);
}
} long long dfs(int _u)
{
long long temp=1;
for (int e=fir[_u];~e;e=nex[e])
{
temp*=dfs(v[e]);
} return temp+1;
} class InvariantSets
{
public:
long long countSets(vector <int> f)
{
n=f.size(); memset(fir,-1,sizeof fir); for (int i=0;i<n;i++)
{
u[i]=i;v[i]=f[i];
nex[i]=fir[i];fir[i]=i;
} find_scc(); memset(fir,-1,sizeof fir);
memset(deg,0,sizeof deg); int e=0; for (int i=0;i<n;i++)
{
if (sccno[u[i]]==sccno[v[i]]) continue; u[e]=sccno[u[i]];v[e]=sccno[v[i]]; swap(u[e],v[e]); nex[e]=fir[u[e]];
deg[v[e]]++;
fir[u[e]]=e++;
} for (int i=1;i<=scc_cnt;i++)
{
if (deg[i]==0)
{
u[e]=i;v[e]=0;
swap(u[e],v[e]);
nex[e]=fir[u[e]];
fir[u[e]]=e++;
}
} return dfs(0)-1; }
}; int main()
{
#ifndef ONLINE_JUDGE
freopen("/home/fcbruce/文档/code/t","r",stdin);
#endif // ONLINE_JUDGE InvariantSets a;
itn x,y;
while (~scanf("%d",&x))
{
vector <int> v;
for (int i=0;i<x;i++)
{
scanf("%d",&y);
v.push_back(y);
} printf("%lld\n",a.countSets(v));
} return 0;
}

Topcoder SRM 628 DIV 2的更多相关文章

  1. TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E

    传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...

  2. TopCoder SRM 667 Div.2题解

    概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...

  3. [topcoder]SRM 646 DIV 2

    第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...

  4. [topcoder]SRM 633 DIV 2

    第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...

  5. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  6. Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索

    最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...

  7. Topcoder SRM 648 (div.2)

    第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...

  8. 【topcoder SRM 702 DIV 2 250】TestTaking

    Problem Statement Recently, Alice had to take a test. The test consisted of a sequence of true/false ...

  9. TopCoder SRM 639 Div.2 500 AliceGameEasy

    题意: 一个游戏有n轮,有A和B比赛,谁在第 i 轮得胜,就获得 i 分,给出x,y,问A得x分,B得y分有没有可能,如果有,输出A最少赢的盘数 解题思路: 首先判断n(n+1)/2 = (x+y)是 ...

随机推荐

  1. 如何知道 win10 的激活到期时间和期限等

    在“运行”里输入cmd,出来dos对话框后,输入下面的东西后,按Enterslmgr.vbs -dli (显示:操作系统版本.部分产品密钥.许可证状态)slmgr.vbs -dlv (显示:最为详尽的 ...

  2. [渣译文] SignalR 2.0 系列: SignalR 自托管主机

    原文:[渣译文] SignalR 2.0 系列: SignalR 自托管主机 英文渣水平,大伙凑合着看吧…… 这是微软官方SignalR 2.0教程Getting Started with ASP.N ...

  3. Handler消息源代码分析

    public static final Looper myLooper() { return (Looper)sThreadLocal.get(); } 首先到Handler运行过程的总结: 1. L ...

  4. JS call与apply

    JS的call与apply call和apply是JS中比较重要的两个方法, 一般在框架和组件设计中用的较多,比如jQuery Code. 那么这两个方法是做什么的呢,下面我们通过代码来了解: 1 f ...

  5. resharper 设置代码颜色

  6. 2014阿里巴巴web前实习生项目分析(1)

    以下简化CSS代码: div.container{ width:500px; background-image:url(/img/sprite.png); background-repeat:no-r ...

  7. NGUI 3.5教程(四)Atlas和Sprite(制作图片button)

    Atlas是NGUI的图集.我的理解是:Atlas把你的一些零散的图片,合并成一张图.这样做的优点是,能够减少Draw Call.我不了解它的底层运作机制,我猜应该也是再行进DXT之类的纹理压缩,所以 ...

  8. Chapter 1 Securing Your Server and Network(7):禁用SQL Server Browse

    原文:Chapter 1 Securing Your Server and Network(7):禁用SQL Server Browse 原文出处:http://blog.csdn.net/dba_h ...

  9. Objective-C语言的一些基础特性

    OC与C++.Java等面向对象语言有很多的类似之处,不过在很多方面也是有所差别的.若是用过某一种面向对象语言,那么就很容易理解OC语言所用的范式和模板了.但是在语法使用上,也许会显得陌生.因为OC语 ...

  10. (hdu step 6.3.7)Cat vs. Dog(当施工方规则:建边当观众和其他观众最喜爱的东西冲突,求最大独立集)

    称号: Cat vs. Dog Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...