Topcoder SRM 628 DIV 2
被自己蠢哭了。。。。
国际象棋棋盘上给出两个坐标,问象从一个走到还有一个最少要几步。
黑格象仅仅能走黑格,白格象仅仅能走白格,仅仅要推断两个坐标的颜色是否同样就能推断是否可达,观察棋盘能够发现坐标的奇偶性决定了格子的颜色;可达的情况下最多两步就能达到,所以仅仅要推断格子是否在同一条斜线上即可了。
#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;
}
给出一个括号序列,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;
}
时间不够,比赛的时候没来得及敲完。。。。。
题意:
给出一个有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的更多相关文章
- TopCoder SRM 560 Div 1 - Problem 1000 BoundedOptimization & Codeforces 839 E
传送门:https://284914869.github.io/AEoj/560.html 题目简述: 定义"项"为两个不同变量相乘. 求一个由多个不同"项"相 ...
- TopCoder SRM 667 Div.2题解
概览: T1 枚举 T2 状压DP T3 DP TopCoder SRM 667 Div.2 T1 解题思路 由于数据范围很小,所以直接枚举所有点,判断是否可行.时间复杂度O(δX × δY),空间复 ...
- [topcoder]SRM 646 DIV 2
第一题:K等于1或者2,非常简单.略.K更多的情况,http://www.cnblogs.com/lautsie/p/4242975.html,值得思考. 第二题:http://www.cnblogs ...
- [topcoder]SRM 633 DIV 2
第一题,http://community.topcoder.com/stat?c=problem_statement&pm=13462&rd=16076 模拟就可以了. #includ ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- Topcoder SRM 656 (Div.1) 250 RandomPancakeStack - 概率+记忆化搜索
最近连续三次TC爆零了,,,我的心好痛. 不知怎么想的,这题把题意理解成,第一次选择j,第二次选择i后,只能从1~i-1.i+1~j找,其实还可以从j+1~n中找,只要没有被选中过就行... [题意] ...
- Topcoder SRM 648 (div.2)
第一次做TC全部通过,截图纪念一下. 终于蓝了一次,也是TC上第一次变成蓝名,下次就要做Div.1了,希望div1不要挂零..._(:зゝ∠)_ A. KitayutaMart2 万年不变的水题. # ...
- 【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 ...
- 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)是 ...
随机推荐
- Android访问设置
在需求 AndroidManifest.xml 中增加下面代码: (1)假设应用程序须要訪问网络权限 <uses-permission android:name="android.pe ...
- HDFS建筑与shell操作
一个.hadoop1.1.0演示 hadoop它适合于大容量数据存储和分布式计算平台 hadoop核心由hdfs和mapreduce组成 hdfs这是一个主从结构,仅有一个.是namenode:从节点 ...
- HDU 3415 Max Sum of Max-K-sub-sequence(单调队列)
转载请注明出处:http://blog.csdn.net/u012860063 Max Sum of Max-K-sub-sequence Time Limit: 2000/1000 MS (Java ...
- 11gR2 Database Services for "Policy" and "Administrator" Managed Databases (文件 ID 1481647.1)
In this Document _afrLoop=1459311711568804&id=1481647.1&displayIndex=6&_afrWindowMode= ...
- SQL Server 2005,2008 正则表达式 替换函数应用详解
CREATE function dbo.regexReplace ( @source ntext, --原字符串 ), --正则表达式 ), --替换值 , --是否是全局替换 --是否忽略大小写 ) ...
- php 无错误提示 的解决方法
问:我在win7安装了PHP,浏览器是IE9.我代码写错了,浏览器一点错误提示都没有,一片空白.如果写对了,就能正常运行显示出来.请问这是怎么回事,应该怎么弄?你们两个的方法都试过,但都没有提示(注: ...
- c# 判断字符是否是全角, 获取字符串的字节数 , 获取字符串指定长度字节数的字符串
1 Encoding.Default.GetByteCount(checkString); =2 全角 =1 半角 /// <summary> /// 获取字符串的字节长度 /// &l ...
- [three.js] 地图不能解决重复的问题 Solving with Texture RepeatWrapping Fail Issue
有些事情,如果你正在寻找侯,怎么也找不到. 有的东西,不经意间,到处: 我认为这是生活中常有的事. 然而,在互联网的浩瀚大海,这同样适用. 正常的一小会儿的积累, 投入少, 积累, 洋大海, 载起一帆 ...
- ng-repeat出现环路输出Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique
采用ng-repeat循环发生错误时,如下面的输出对象: Duplicates in a repeater are not allowed. Use 'track by' expression to ...
- 【浅墨著作】《OpenCV3编程入门》内容简单介绍&勘误&配套源码下载
经过近一年的沉淀和总结,<OpenCV3编程入门>一书最终和大家见面了. 近期有为数不少的小伙伴们发邮件给浅墨建议最好在博客里面贴出这本书的文件夹,方便大家更好的了解这本书的内容.事实上近 ...