【FZU 2200、2202~2204】FOJ有奖月赛 2015.10 个人的部分题解(C、E、F、G)
来源:互联网 发布:3d打印机软件 编辑:程序博客网 时间:2024/05/17 02:44
比赛地址:http://acm.fzu.edu.cn/contest/ranklist.php?cid=145
目前只会CEFG这4个题,下面的题解按过题的顺序来排。
Problem E 犯罪嫌疑人
http://acm.fzu.edu.cn/problem.php?pid=2202
看题转述+推理一下。
存在犯罪嫌疑人,当他是实际犯罪者的时候,恰好有且仅有M句话是真话。
那么,如果我们假定一个人是犯罪嫌疑人,有几句话是真话呢?
假设有A句是说xxx是犯罪者的句式,B句是说xxx不是犯罪者的句式。
那么当某人X为犯罪嫌疑人的时候,
真话句数=说X为犯罪者的数量+(B-说X不是犯罪者的数量)//非常自然对吧
然后我们可以得知,哪些人可能是犯罪者了。
如果只有1个人,那么每个人说的话的真假是直接被确定了,毫无疑问。
如果有多个人,
- 如果某人说的是xxx是犯罪者的句式,如果xxx可能为犯罪者,则他的话可真可假;如果xxx不可能是犯罪者,那他说的一定是假话。
- 如果某人说的是xxx不是犯罪者的句式,如果xxx可能为犯罪者,则他的话可真可假;如果xxx不可能是犯罪者,那他说的一定是真话。
所以O(n)统计每个人被指认多少次+每个人被排除嫌疑多少次,O(n)来判断每个人是不是可能是犯罪嫌疑人。最后O(n)推断每句话的真假,没了。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>using namespace std;typedef long long ll;int data[100005];int votea[100005],voteb[100005];bool iscrime[100005];int main(){ int T; int n,m; for(scanf("%d",&T);T--;){ scanf("%d%d",&n,&m); fill(votea,votea+1+n,0); fill(voteb,voteb+1+n,0); fill(iscrime,iscrime+1+n,0); int upcnt=0,downcnt=0; for(int i=1;i<=n;i++){ scanf("%d",&data[i]); if(data[i]>0)votea[data[i]]++; else voteb[-data[i]]++; if(data[i]>0)upcnt++; else downcnt++; } int crimecnt=0; for(int i=1;i<=n;i++){ int tmp=votea[i]+(downcnt-voteb[i]); if(tmp==m){ iscrime[i]=true; crimecnt++; } } if(crimecnt==1){ for(int i=1;i<=n;i++){ if(data[i]>0){ if(iscrime[data[i]]){ puts("Truth"); }else puts("Lie"); }else{ data[i]=-data[i]; if(iscrime[data[i]]){ puts("Lie"); }else puts("Truth"); } } }else{ for(int i=1;i<=n;i++){ if(data[i]>0){ if(iscrime[data[i]]){ puts("Not defined"); }else puts("Lie"); }else{ data[i]=-data[i]; if(iscrime[data[i]]){ puts("Not defined"); }else puts("Truth"); } } } } return 0;}
Problem F 单纵大法好
http://acm.fzu.edu.cn/problem.php?pid=2203
首先,下面给的炮弹落点是按时间顺序给的,所以不要排序。
其次,落点越多,可放的地方越少,明显会从没被打到到不得不被打到了,于是可以二分时刻。
对一个给定的时刻,先炮弹落下去,然后数剩下的所有连续可放的地方能放船的数量是多少(注意每2个船之间要一个空格,所以2个船在一段连续空白中最小占地是2*a+1),O(n)的时间复杂度。
所以总体复杂度是O(nlogn),反正1s还是不太紧张的,时限5s就太客气了,多谢多谢。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>using namespace std;typedef long long ll;int n,k,a,m;int times[200005];bool isb[200005];bool check(int t){ fill(isb,isb+n+1,true); for(int i=0;i<t;i++){ isb[times[i]]=false; } int preblank=0,pcnt=0; for(int i=1;i<=n;i++){ if(isb[i])preblank++; else{ if(preblank>=a)pcnt+=1+(preblank-a)/(a+1); preblank=0; } } if(preblank>=a)pcnt+=1+(preblank-a)/(a+1); return pcnt>=k;}int main(){ while(scanf("%d%d%d%d",&n,&k,&a,&m)!=EOF){ for(int i=0;i<m;i++){ scanf("%d",×[i]); } int l=0,r=m,mid; while(r-l>=3){ mid=(l+r)/2; if(check(mid))l=mid; else r=mid; } bool found=false; for(int i=l+1;i<=r;i++){ if(check(i-1)==true&&check(i)==false){ printf("%d\n",i); found=true; break; } } if(!found)puts("-1"); } return 0;}
Problem G 7
http://acm.fzu.edu.cn/problem.php?pid=2204
上来以为是打表找规律,于是bitset暴力计算打表好。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>#include <bitset>using namespace std;typedef long long ll;const int n=22;bool check(bitset<n> s){ for(int i=0;i<n;i++){ int cnt=0; for(int j=0;j<7;j++){ if(s[i])if(s[(i+j)%n])cnt++; if(!s[i])if(!s[(i+j)%n])cnt++; } if(cnt==7)return false; } return true;}int main(){ int ans=0; for(int i=0;i<(1<<n);i++){ bitset<n> s(i); if(check(s)) ans++; } printf("%d %d %d\n",ans,1<<n,(1<<n)-ans); return 0;}
兴冲冲的发现
然后往后拍,发现后面规律就散乱的让人抓狂,于是gg,放弃打表的念想。转而想DP。
考虑黑白交替的放1~6个球这个行为,然后写dp状态:
f[一开始放了什么颜色的][在这个位置放了什么颜色的][放到第几个位置了]
好,现在有个问题:我最后放的球如果和一开始放的是相同的颜色,那我最后放球的个数还受制于一开始放的球的个数,现在这个dp状态没法很好的搞定这个问题。
那没关系,多一个状态:
f[一开始放了什么颜色的][一开始放了几个同色的球][在这个位置放了什么颜色的][放到第几个位置了]
然后可以发现,先放黑的方案和先放白的方案是一一对应的,所以第一个状态没用,最后算出来的结果*2就好了,删删删。
继续,你会写出
f[1][0][1]=1;f[2][0][2]=1;f[3][0][3]=1;f[4][0][4]=1;f[5][0][5]=1;f[6][0][6]=1;
太丑了吧……而且f[1]\f[2]\f[3]\……这之后的2维之间的dp明显的相互独立,来来来,再删一维,变成做6遍dp,然后加6个结果就行。
于是现在dp状态定下了:
f[在这个位置放了什么颜色的][放到第几个位置了]
外层for循环6次,从1到6,表示一开始放下了某种颜色A的球的数量,初始化dp,然后后面对当前色彩,合法的去取最后是另一种色彩的球的方案数,加起来,当要放最后一个球的时候,记得考虑,如果和一开始放的球同色,与一开始放的球连起来不能超过6个。
不过,真的感觉哔了狗的是,dp写完以后一个地方越界没考虑过,交上去WA,本地补上考虑越界,这100000这种情况跑出来的结果和没补,跑出来一模一样,以为还有别的问题,找不到了就交一发,过了。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>using namespace std;typedef long long ll;const int tomod=2015;int f[2][100005];int main(){ int T,Case=1,n; for(scanf("%d",&T);Case<=T;Case++){ scanf("%d",&n); if(n<=6){ printf("Case #%d: %d\n",Case,1<<n); continue; } int ans=0; for(int start=1;start<=6;start++){ memset(f,0,sizeof(f)); f[0][start]=1; for(int i=start+1;i<=n;i++){ for(int j=0;j<=1;j++){ if(i==n&&j==0){ for(int k=1;k<=6-start;k++){ if(i-k>=1)f[j][i]+=f[1-j][i-k]; } f[j][i]%=tomod; }else{ for(int k=1;k<=6;k++){ if(i-k>=1)f[j][i]+=f[1-j][i-k]; } f[j][i]%=tomod; } } } ans+=f[0][n]+f[1][n]; ans=(ans%tomod+tomod)%tomod; } ans*=2;ans%=tomod; printf("Case #%d: %d\n",Case,ans); } return 0;}
Problem C cleaning
http://acm.fzu.edu.cn/problem.php?pid=2200
我DP渣,写DP写不过你们,我打表总可以吧?bitset打表再来!
鉴于Clarification里说,n>=3,那从3开始打:对每个n,输出k为0~n时的解,排起来。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>#include <bitset>using namespace std;typedef long long ll;const int n=16;int ans[n+1];bool acc(bitset<n> s){ for(int i=0;i<n;i++){ if(s[i]){ if(s[(i+2)%n]||s[(i-2)%n])return false; } } return true;}int main(){ for(int i=0;i<(1<<n);i++){ bitset<n> s(i); if(acc(s)) ans[s.count()]++; } for(int i=0;i<=n;i++){ printf("%d ",ans[i]); } return 0;}
表大约长这个样子:
1 3 0 0 //21 4 4 0 0 //31 5 5 0 0 0 //31 6 9 0 0 0 0 //31 7 14 7 0 0 0 0 //41 8 20 16 4 0 0 0 0 //51 9 27 30 9 0 0 0 0 0 //51 10 35 50 25 0 0 0 0 0 0 //51 11 44 77 55 11 0 0 0 0 0 0 //61 12 54 112 105 36 4 0 0 0 0 0 0 //71 13 65 156 182 91 13 0 0 0 0 0 0 0 //71 14 77 210 294 196 49 0 0 0 0 0 0 0 0 //71 15 90 275 450 378 140 15 0 0 0 0 0 0 0 0 //81 16 104 352 660 672 336 64 4 0 0 0 0 0 0 0 0 0 //9//(后面注释表示,这一行非0数有几个)
有以下规律
- 以n==3这行开始,每4行一组,每组第一行是n/2+1个非0数,之后3行每行是n/2+2个非0数
- 一组中,第1行和第3行最后一个数和这行的第二个数一样(如果是第
i 行,这两个值都是i ),第2行的最后一个数一定是4,第4行的最后一个数的值=上面一行正对的数+上面一行正对的数的斜左上角的数UPD: 另一种规律是,第4行最后一个数的值=第4行的实际行号2/4 ,比如第14行的最后一个数,是142/4=49 其他的数的值=上面一行正对的数+上面一行正对的数的斜左上角的数(比如1 16 104 … 这一行,这里104=90+14)
好了,规律都找出来了,然后10min写个在线生成表的程序,提交就1A,0ms+5MB内存的事情。
#include <stdio.h>#include <ctype.h>#include <string.h>#include <stdlib.h>#include <limits.h>#include <math.h>#include <algorithm>using namespace std;typedef long long ll;const int tomod=1000000007;int lastx[1005];int ans[1005][1005];int main(){ memset(ans,0,sizeof(ans)); for(int i=3;i<=1000;i++){ ans[i][0]=1;ans[i][1]=i; } for(int i=3;i<=1000;i+=4){ lastx[i]=i/2; lastx[i+1]=lastx[i+2]=lastx[i+3]=i/2+1; ans[i][lastx[i]]=i; ans[i+1][lastx[i+1]]=4; ans[i+2][lastx[i+2]]=i+2; ans[i+3][lastx[i+3]]=-1; } for(int i=3;i<=1000;i++){ for(int j=2;j<=lastx[i];j++){ if(ans[i][j]<=0){ ans[i][j]=(ans[i-1][j]+ans[i-2][j-1])%tomod; } } } int T,n,k; for(scanf("%d",&T);T--;){ scanf("%d%d",&n,&k); printf("%d\n",ans[n][k]); } return 0;}
- 【FZU 2200、2202~2204】FOJ有奖月赛 2015.10 个人的部分题解(C、E、F、G)
- FOJ有奖月赛-2016年4月(F)
- FOJ有奖月赛-2014年11月 题解
- 福州大学有奖月赛2015.11C题(foj 2207)以撒的结合
- FOJ 2014.3 有奖月赛
- FOJ有奖月赛-2015年11月-Problem F 攻占计划
- FOJ有奖月赛-2015年11月 Problem F 攻占计划
- FOJ有奖月赛-2015年11月 Problem F 攻占计划
- FOJ有奖月赛-2015年11月 Problem F 攻占计划
- FOJ有奖月赛3月
- FOJ有奖月赛-2015年10月 题解【5/7】
- FOJ有奖月赛-2011年11月 E Another a+b
- FZU Problem 2222 ABCDEFG(水题)——FOJ有奖月赛-2016年4月(校赛热身赛)
- FOJ有奖月赛-2016年4月(校赛热身赛) D 小茗的魔法阵
- FOJ 有奖月赛 4月(校赛热身赛)D题 小茗的魔法阵
- FOJ 有奖月赛 2016-8 C Problem C Daxia & Suneast's problem
- 福州大学 FOJ有奖月赛-2017年4月(校赛热身赛)(DP)
- FOJ有奖月赛-2017年4月(校赛热身赛) random password(DP)
- __name__ = '__main__'问题
- java中成员变量与局部变量
- Fragement的使用
- modelsim仿真xilinx ip核的方法举例
- 第4周项目2建立“单链表”算法库
- 【FZU 2200、2202~2204】FOJ有奖月赛 2015.10 个人的部分题解(C、E、F、G)
- PHP glob() 函数
- 原码-反码-补码
- XAMPP Apache启动失败
- 致凯利定理(Cayley公式)
- 软件功能测试步骤
- 让应用支持64位
- 【bzoj2721】【Violet 5】樱花【数论】
- ns-2与TCP拥塞模拟 之一 一个简单的示例说明基本要素