2014-10-06 15:13:20
这场CF有点丧病QAQ。
A:打表画图题,先用二维数组存图,然后输出。(写的搓搓的~)
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sun 05 Oct 2014 09:02:46 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <queue> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 #define lp (p << 1) 20 #define rp (p << 1|1) 21 #define getmid(l,r) (l + (r - l) / 2) 22 #define MP(a,b) make_pair(a,b) 23 typedef long long ll; 24 const int INF = 1 << 30; 25 26 int k; 27 int g[50][50]; 28 29 int main(){ 30 memset(g,0,sizeof(g)); 31 scanf("%d",&k); 32 int i,j; 33 if(k <= 4){ 34 for(i = 1; i <= k; ++i) 35 g[i][1] = 1; 36 } 37 else{ 38 for(i = 1; i <= 4; ++i) g[i][1] = 1; 39 k -= 4; 40 i = 1,j = 2; 41 while(k){ 42 g[i][j] = 1; 43 ++i; 44 if(i == 3) i = 4; 45 if(i > 4){ 46 i = 1; 47 ++j; 48 } 49 --k; 50 } 51 } 52 printf("+------------------------+\n"); 53 for(i = 1; i <= 2; ++i){ 54 printf("|"); 55 for(j = 1; j <= 11; ++j) 56 printf("%c.",g[i][j] ? 'O' : '#'); 57 if(i == 1) printf("|D|)\n"); 58 else printf("|.|\n"); 59 } 60 printf("|%c.......................|\n",g[3][1] ? 'O' : '#'); 61 printf("|"); 62 for(j = 1; j <= 11; ++j) 63 printf("%c.",g[4][j] ? 'O' : '#'); 64 printf("|.|)\n"); 65 printf("+------------------------+\n"); 66 return 0; 67 }
B:这题被我YY出来了(QAQ),只要判断四个角是否堵死就可以了,为什么呢?
因为如果四个角不堵死,那么最外圈形成环路,从任意一点出发先到最外圈环路,然后可以到任意一点。
1 /************************************************************************* 2 > File Name: b.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sun 05 Oct 2014 09:20:56 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <queue> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 #define lp (p << 1) 20 #define rp (p << 1|1) 21 #define getmid(l,r) (l + (r - l) / 2) 22 #define MP(a,b) make_pair(a,b) 23 typedef long long ll; 24 const int INF = 1 << 30; 25 26 int main(){ 27 int n,m; 28 char s1[50],s2[50]; 29 scanf("%d%d",&n,&m); 30 scanf("%s%s",s1 + 1,s2 + 1); 31 int flag = 1; 32 if(s1[1] == '<' && s2[1] == '^') flag = 0; 33 if(s1[1] == '>' && s2[m] == '^') flag = 0; 34 if(s1[n] == '<' && s2[1] == 'v') flag = 0; 35 if(s1[n] == '>' && s2[m] == 'v') flag = 0; 36 if(flag) printf("YES\n"); 37 else printf("NO\n"); 38 return 0; 39 }
C:比赛的时候想了一个看起来很神但写起来巨麻烦的算法.... 赛后看别人DFS好像很优雅QAQ。写不动了orz。
D:练思维的好题,其实没什么算法。思路:大题想法是枚举区间右端点。设当前读进来第 i 个数,且此时已经处理出前 i - 1个数所有可能的gcd值(而且求出该gcd值出现次数),那么将第 i 个数和所有可能gcd值再求一次gcd,需要保存的仅是该轮求出的所有gcd(相当于滚动数组思想),需要用map来记录gcd出现次数。 细节见代码。
第一次写了一个姿势比较丑的代码 400+ms QAQ
1 /************************************************************************* 2 > File Name: d.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 06 Oct 2014 12:50:08 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <queue> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 #define lp (p << 1) 20 #define rp (p << 1|1) 21 #define getmid(l,r) (l + (r - l) / 2) 22 #define MP(a,b) make_pair(a,b) 23 typedef long long ll; 24 const int INF = 1 << 30; 25 const int maxn = 100010; 26 27 int Gcd(int a,int b){ return b == 0 ? a : Gcd(b,a % b);} 28 29 int n,q; 30 int a[maxn],val[maxn],cnt[maxn],sz; 31 map<ll,ll> mp,mp1,mp2; 32 33 void Add(int v,int c){ 34 if(mp2.find(v) == mp2.end()) mp2[v] = 1; 35 mp1[v] += c; 36 } 37 38 int main(){ 39 scanf("%d",&n); 40 for(int i = 1; i <= n; ++i) 41 scanf("%d",a + i); 42 for(int i = 1; i <= n; ++i){ 43 mp1.clear(); 44 int tsz = sz; 45 sz = 0; 46 for(int j = 1; j <= tsz; ++j){ 47 int c = mp2[val[j]]; 48 val[++sz] = Gcd(val[j],a[i]); 49 Add(val[sz],c); 50 } 51 val[++sz] = a[i]; 52 Add(val[sz],1); 53 sort(val + 1,val + sz + 1); 54 sz = unique(val + 1,val + sz + 1) - val - 1; 55 for(int j = 1; j <= sz; ++j){ 56 if(mp.find(val[j]) == mp.end()) mp[val[j]] = 0; 57 mp[val[j]] += mp1[val[j]]; 58 } 59 //for(int j = 1; j <= sz; ++j){ 60 // printf("val :%d , num %d sum %d\n",val[j],mp1[val[j]],mp[val[j]]); 61 //} 62 mp2 = mp1; 63 } 64 scanf("%d",&q); 65 int x; 66 while(q--){ 67 scanf("%d",&x); 68 if(mp.find(x) == mp.end()) printf("0\n"); 69 else printf("%I64d\n",mp[x]); 70 } 71 return 0; 72 }
参考通神的代码,优化了下,100+ms
1 /************************************************************************* 2 > File Name: d.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Mon 06 Oct 2014 12:50:08 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <queue> 16 #include <iostream> 17 #include <algorithm> 18 using namespace std; 19 #define lp (p << 1) 20 #define rp (p << 1|1) 21 #define getmid(l,r) (l + (r - l) / 2) 22 #define MP(a,b) make_pair(a,b) 23 typedef long long ll; 24 const int INF = 1 << 30; 25 const int maxn = 100010; 26 27 int Gcd(int a,int b){ return b == 0 ? a : Gcd(b,a % b);} 28 29 int n,q; 30 int a[maxn],sz,tsz; 31 map<int,ll> mp; 32 pair<int,int> val[maxn]; 33 34 int main(){ 35 scanf("%d",&n); 36 for(int i = 1; i <= n; ++i) 37 scanf("%d",a + i); 38 sz = 0; 39 for(int i = 1; i <= n; ++i){ 40 for(int j = 1; j <= sz; ++j) 41 val[j].first = Gcd(val[j].first,a[i]); 42 val[++sz] = MP(a[i],1); 43 tsz = 1; 44 for(int j = 2; j <= sz; ++j){ 45 if(val[tsz].first == val[j].first) val[tsz].second += val[j].second; 46 else val[++tsz] = val[j]; 47 } 48 sz = tsz; 49 for(int j = 1; j <= sz; ++j){ 50 if(mp.find(val[j].first) == mp.end()) mp[val[j].first] = 0; 51 mp[val[j].first] += val[j].second; 52 } 53 } 54 scanf("%d",&q); 55 int x; 56 while(q--){ 57 scanf("%d",&x); 58 if(mp.find(x) == mp.end()) printf("0\n"); 59 else printf("%I64d\n",mp[x]); 60 } 61 return 0; 62 }