CF #175 div2 练习
2014-10-31 17:19:18
总结:这场cf前三题都好做,D的话有点诡异,st:533/3003
A:简单构造,5min(太慢)
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 31 Oct 2014 03:09:44 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 <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int main(){ 28 int n,k; 29 scanf("%d%d",&n,&k); 30 int pos = n; 31 while(k >= 1){ 32 printf("%d ",pos--); 33 --k; 34 } 35 for(int i = 1; i <= pos; ++i) 36 printf("%d ",i); 37 puts(""); 38 return 0; 39 }
B:简单的DFS,13min(中速)
1 /************************************************************************* 2 > File Name: b.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 31 Oct 2014 03:16:26 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 <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int vis[100010]; 28 int next[100010]; 29 int n,s,t,cnt = 0; 30 31 bool Dfs(int p){ 32 vis[p] = 1; 33 if(p == t) 34 return true; 35 p = next[p]; 36 ++cnt; 37 if(!vis[p] && Dfs(p)) return true; 38 return false; 39 } 40 41 int main(){ 42 scanf("%d%d%d",&n,&s,&t); 43 for(int i = 1; i <= n; ++i) 44 scanf("%d",next + i); 45 cnt = 0; 46 if(Dfs(s)) 47 printf("%d\n",cnt); 48 else 49 printf("-1\n"); 50 return 0; 51 }
C:题目看清会发现是个贪心,将数列排个序后,逐个扫描即可,19min(快速)
1 /************************************************************************* 2 > File Name: c.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 31 Oct 2014 03:26:42 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 <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int n; 28 ll a[300010]; 29 ll sum; 30 31 int main(){ 32 scanf("%d",&n); 33 for(int i = 1; i <= n; ++i) 34 scanf("%I64d",a + i); 35 sort(a + 1,a + n + 1); 36 sum = 0; 37 ll tmp; 38 for(ll i = 1; i <= n; ++i){ 39 tmp = a[i] - i; 40 if(tmp < 0) tmp = -tmp; 41 sum += tmp; 42 } 43 printf("%I64d\n",sum); 44 return 0; 45 }
D:竟然是个暴力!!!自己随便敲个暴力程序找找规律,发现n为偶数时都为0,奇数的话暴力(用OEIS可以找出数列!然后直接用数组存答案),赛后A的(太慢)
1 /************************************************************************* 2 > File Name: dd.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Fri 31 Oct 2014 05:06: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 <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 const int mod = 1000000007; 27 28 ll n; 29 ll ans[10] = {0,1, 3, 15, 133, 2025, 37851, 1030367, 36362925}; 30 ll fac[20]; 31 32 void Pre(){ 33 fac[0] = 1; 34 for(ll i = 1; i <= 16; ++i) 35 fac[i] = (fac[i - 1] * i) % mod; 36 } 37 38 int main(){ 39 Pre(); 40 scanf("%I64d",&n); 41 if(n % 2 == 0) printf("0\n"); 42 else printf("%I64d\n",(ans[(n + 1) / 2] % mod) * (fac[n] % mod) % mod); 43 return 0; 44 }