CF #176 div2 练习
2014-10-31 14:44:33
A:这个。。暴力即可。(cf的a,b基本就考考暴力、贪心,虽然在算法上没什么帮助,但能大大提高代码能力和准确性!)
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 30 Oct 2014 06:05:05 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 char g[5][5]; 28 29 int main(){ 30 for(int i = 0; i < 4; ++i) 31 scanf("%s",g[i]); 32 int c1,c2,flag = 0; 33 for(int i = 0; !flag && i < 3; ++i){ 34 for(int j = 0; j < 3; ++j){ 35 c1 = c2 = 0; 36 if(g[i][j] == '.') c1++; 37 else c2++; 38 if(g[i][j + 1] == '.') c1++; 39 else c2++; 40 if(g[i + 1][j] == '.') c1++; 41 else c2++; 42 if(g[i + 1][j + 1] == '.') c1++; 43 else c2++; 44 if(c1 == 3 || c1 == 4){ 45 flag = 1; 46 break; 47 } 48 if(c2 == 3 || c2 == 4){ 49 flag = 1; 50 break; 51 } 52 } 53 } 54 if(flag) printf("YES\n"); 55 else printf("NO\n"); 56 return 0; 57 }
B:怎么说呢,我是推公式出来的,看到有人用暴力直接搞出来,我也是醉了。
考虑:尽量先取大的管子,设取m个管子k,k-1,k-2...k-m+1,使得他们的和减去m-1的值(也就是组合后的管道数)>=n,可以证明只要找到刚好大于等于n的m的取值,那么一定可以通过换更小的管子(或者不换)使得管道总数恰好等于n。
方程:k+k-1+...+k-m+1 - (m-1) >= n
(2k-m+1)*m/2 - (m-1) >= n
-m^2 + (2k-1)m +2 - 2n >= 0
然后求根。再判断。
1 /************************************************************************* 2 > File Name: b.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 30 Oct 2014 11:34:02 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 ll n,k,x; 28 29 int main(){ 30 scanf("%I64d %I64d",&n,&k); 31 if(n > (k*k-k + 2) / 2){ 32 printf("-1\n"); 33 return 0; 34 } 35 x = ((2*k-1) - sqrt(4.0*k*k-4.0*k-8.0*n+9)) / 2.0; 36 for(; x <= k; ++x){ 37 if((2*k-x+1)*x-2*x+2 >= 2*n) 38 break; 39 } 40 printf("%I64d\n",x); 41 return 0; 42 }
C:构造题,列几个就可以发现:
n=4 , 2 4 1 3
n=5, 2 5 3 1 4
n=6, no
n=7, no
n=8, 2 8 4 6 3 5 1 7
n=8, 2 9 4 7 5 3 6 1 8
我们发现四个一组分配,n=8时,1278给第1,2,n-1,n个,3456给第3,4,n-3,n-2个。如果剩下一个就可以分配给自己,剩2 or 3个都不行。
1 /************************************************************************* 2 > File Name: c.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Thu 30 Oct 2014 11:54:21 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 int s[100010]; 29 30 int main(){ 31 scanf("%d",&n); 32 if(n % 4 == 2 || n % 4 == 3){ 33 printf("-1\n"); 34 } 35 else{ 36 int p1 = 1,p2 = n; 37 while(p1 < p2){ 38 s[p1] = p1 + 1; 39 s[p1 + 1] = p2; 40 s[p2] = p2 - 1; 41 s[p2 - 1] = p1; 42 p1 += 2; 43 p2 -= 2; 44 } 45 if(p1 == p2) s[p1] = p1; 46 for(int i = 1; i <= n; ++i){ 47 if(i != 1) printf(" "); 48 printf("%d",s[i]); 49 } 50 puts(""); 51 } 52 return 0; 53 }