Codeforces Round#308

A题,看样例就知道要求什么,   水过去

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <algorithm>

 5 #include <iostream>

 6 #include <queue>

 7 #include <stack>

 8 #include <vector>

 9 #include <map>

10 #include <set>

11 #include <string>

12 #include <math.h>

13 using namespace std;

14 #pragma warning(disable:4996)

15 typedef long long LL;                   

16 const int INF = 1<<30;

17 /*

18 

19 */

20 int vis[101][101];

21 int main()

22 {

23     int n, a, b, c, d;

24     scanf("%d", &n);

25     for (int i = 1; i <= n; ++i)

26     {

27         scanf("%d%d%d%d", &a, &b, &c, &d);

28         for (int j = a; j <= c; ++j)

29         for (int k = b; k <= d; ++k)

30             vis[j][k]++;

31     }

32     int ans = 0;

33     for (int i = 1; i <= 100; ++i)

34     for (int j = 1; j <= 100; ++j)

35     if (vis[i][j])

36         ans += vis[i][j];

37     printf("%d\n", ans);

38     return 0;

39 }
View Code

 

B题 给定一个数字n, 问从1到n的数字的总位数之和,刚开始的时候想到了数位dp,后来发现并不用这样

位数为1的数有9个, 位数为2的数有90个,位数为3的数,有900个,依次类推。

所以对于给定数字n=123, 9*1 + 90 * 2, 然后算出位数为3的数字有多少个即可。

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <algorithm>

 5 #include <iostream>

 6 #include <queue>

 7 #include <stack>

 8 #include <vector>

 9 #include <map>

10 #include <set>

11 #include <string>

12 #include <math.h>

13 using namespace std;

14 #pragma warning(disable:4996)

15 typedef __int64 LL;                   

16 const int INF = 1<<30;

17 /*

18 

19 */

20 

21 int main()

22 {

23     LL n;

24     scanf("%I64d", &n);

25     char str[11];

26     LL m = 1;

27     sprintf(str, "%I64d", n);

28     LL len = strlen(str);

29     LL ans = 0;

30     LL t = 9;

31     LL i = 0;

32     for (i = 0; i < len - 1; ++i)

33     {

34         ans = ans + t * (i + 1);

35         t *= 10;//位数为i+1的数字有多少个

36         m = m * 10;

37     }

38     ans += (i+1) * (n - m + 1);

39     printf("%I64d\n", ans);

40     return 0;

41 }
View Code

 

C题 给定一个w和m,   那么我们就拥有w^0, w^1,w^2,w^3...w^100  g的砝码, 

问称货物m,能不能使得天平两端平衡,(砝码可以加在两端)

刚开始的思路是  只要砝码相减的值是m或者m+1或者m-1

即 w(x-y) = m   ||  w(x-y) = m - 1  || w(x-y) = m + 1  就可以平衡           为什么+1 或者-1呢, 因为w^0是一个特殊的值

两边同除w --->  x-y = m/w || x-y = (m+1)/y || x-y = (m-1)/y

后来发现就算能整除,砝码可能凑不出(x-y), 所以问题就转为了, 砝码能不能凑出(x-y), 

就变成了程序原来的问题,因为两边同除w,所以就变为w^0, w^1,w^2,w^3...w^99的砝码能不能使得x-y在天平上平衡

所以这是一个递归的问题,  只要x-y能够为1,那么砝码便能使得天平平衡(因为总是存在1g的砝码)

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <algorithm>

 5 #include <iostream>

 6 #include <queue>

 7 #include <stack>

 8 #include <vector>

 9 #include <map>

10 #include <set>

11 #include <string>

12 #include <math.h>

13 using namespace std;

14 #pragma warning(disable:4996)

15 typedef long long LL;                   

16 const int INF = 1<<30;

17 /*

18 

19 */

20 bool flag;

21 void dfs(int w, int m)

22 {

23     if (m == 1)

24     {

25         flag = true;

26         return;

27     }

28     if ((m - 1) % w == 0)

29         dfs(w, (m - 1) / w);

30     else if (m%w == 0)

31         dfs(w, m / w);

32     else if ((m + 1) % w == 0)

33         dfs(w, (m + 1) / w);

34 }

35 int main()

36 {

37     int w, m;

38     scanf("%d%d", &w, &m);

39     flag = false;

40     dfs(w, m);

41     if (flag)

42         puts("YES");

43     else

44         puts("NO");

45     return 0;

46 }
View Code

 

D题,给定n个点,问能构成多少个三角形, 暴力居然可以过,  2000*2000*2000  可以在4s的时间内跑过

 1 #include <stdio.h>

 2 #include <string.h>

 3 #include <stdlib.h>

 4 #include <algorithm>

 5 #include <iostream>

 6 #include <queue>

 7 #include <stack>

 8 #include <vector>

 9 #include <map>

10 #include <set>

11 #include <string>

12 #include <math.h>

13 using namespace std;

14 #pragma warning(disable:4996)

15 typedef long long LL;                   

16 const int INF = 1<<30;

17 /*

18 给定n个点,问可以生成多少个三角形

19 */

20 const int N = 2000 + 10;

21 struct Point

22 {

23     int x, y;

24     bool operator<(const Point&rhs)const

25     {

26         if (x == rhs.x)

27             return y < rhs.y;

28         return x < rhs.x;

29     }

30 }a[N];

31 int main()

32 {

33     int n,i,j,k;

34     scanf("%d", &n);

35     for (i = 0; i < n; ++i)

36     {

37         scanf("%d%d", &a[i].x, &a[i].y);

38     }

39     int ans = 0;

40     for (i = 0; i < n; ++i)

41     for (j = i + 1; j < n; ++j)

42     for (k = j + 1; k < n; ++k)

43     {

44         if (a[k].x>a[j].x && a[k].y > a[j].y)

45         {

46             ans += n - k;

47             break;

48         }

49         if (a[i].x != a[j].x || a[i].x != a[k].x || a[k].x != a[i].x)

50         {

51             if(a[i].y != a[j].y || a[i].y != a[k].y || a[k].y != a[i].y)

52                 ans++;

53         }

54     }

55     printf("%d\n", ans);

56     return 0;

57 }
View Code

 

你可能感兴趣的:(codeforces)