zoukankan      html  css  js  c++  java
  • 省赛1



    Shopping

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

    Saya and Kudo go shopping together.
    You can assume the street as a straight line, while the shops are some points on the line.
    They park their car at the leftmost shop, visit all the shops from left to right, and go back to their car.
    Your task is to calculate the length of their route.

    输入

    The input consists of several test cases.
    The first line of input in each test case contains one integer N (0<N<100001), represents the number of shops.
    The next line contains N integers, describing the situation of the shops. You can assume that the situations of the shops are non-negative integer and smaller than 2^30.
    The last case is followed by a line containing one zero.

    输出

     For each test case, print the length of their shopping route.

    示例输入

    4
    24 13 89 37
    6
    7 30 41 14 39 42
    0

    示例输出

    152
    70

    提示

    Explanation for the first sample: They park their car at shop 13; go to shop 24, 37 and 89 and finally return to shop 13. The total length is (24-13) + (37-24) + (89-37) + (89-13) = 152
    //贪心
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 200010;
    
    
    int main()
    {
    	//freopen("in.txt", "r", stdin);
    	int n, num[100005], sum;
    	while (scanf("%d", &n) != EOF)
    	{
    		sum = 0;
    		if(n == 0)
    		{
    			break;
    		}
    		for (int i = 1; i <= n; i++)
    		{
    			scanf("%d", &num[i]);
    		}
    		sort(num + 1, num + n + 1);
    		for (int i = 1; i < n; i++)
    		{
    			sum += num[i+1] - num[i];
    		}
    		sum += num[n] - num[1];
    		printf("%d\n", sum);
    	}
    	return 0;
    }
     
    
    
    
    /**************************************
    	Problem id	: SDUT OJ 2154 
    	User name	: asm 
    	Result		: Accepted 
    	Take Memory	: 864K 
    	Take Time	: 40MS 
    	Submit Time	: 2013-05-18 18:11:01  
    **************************************/
    


    Phone Number

    Time Limit: 1000MS Memory limit: 65536K

    题目描述

          We know that if a phone number A is another phone number B’s prefix, B is not able to be called. For an example, A is 123 while B is 12345, after pressing 123, we call A, and not able to call B.
          Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.

    输入

     The input consists of several test cases.
     The first line of input in each test case contains one integer N (0<N<1001), represent the number of phone numbers.
     The next line contains N integers, describing the phone numbers.
     The last case is followed by a line containing one zero.

    输出

     For each test case, if there exits a phone number that cannot be called, print “NO”, otherwise print “YES” instead.

    示例输入

    2
    012
    012345
    2
    12
    012345
    0

    示例输出

    NO
    YES
    好吧,最近刚做过类似的题,trie树的利用。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    #define MAX 10
    
    typedef struct Node
    {
    	bool issrr;
    	struct Node *next[MAX];
    }Node, *Trie;
    Trie root;
    bool flag;
    
    void createTrie(char *str)
    {
    	int len, i, j;
    	bool new_flag = false;
    	Node *current, *newnode;
    	len = strlen(str);
    	if(len == 0)
    	{
    		return;
    	}
    	current = root;
    	for(i = 0; i < len; i++)
    	{
    		if(current->issrr == true)
    		{
    			flag = false;
    			return;
    		}
    		int id = str[i] - '0';
    		if(current->next[id] == NULL)
    		{
    			new_flag = true;
    			newnode = (Trie)malloc(sizeof(Node));
    			newnode->issrr = false;
    			for(j = 0; j < MAX; j++)
    			{
    				newnode->next[j] = NULL;
    			}
    			current->next[id] = newnode;
    		}
    		current = current->next[id];
    	}
    	current->issrr = true;
    	if(!new_flag) flag = false;
    }
    
    int main()
    {
    	int n;
    	char text[1000] = {0};
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		flag = true;
    		root = (Trie)malloc(sizeof(Node));
    		for (int i = 0; i < MAX; i++)
    		{
    			root->next[i] = NULL;
    		}
    		while (n--)
    		{
    			scanf("%s", text);
    			if(flag)
    			{
    				createTrie(text);
    			}
    		}
    		if(flag)
    		{
    			printf("YES\n");
    		}
    		else
    		{
    			printf("NO\n");
    		}
    	}
    	return 0;
    }
     
    
    
    
    /**************************************
    	Problem id	: SDUT OJ 2151 
    	User name	: asm 
    	Result		: Accepted 
    	Take Memory	: 560K 
    	Take Time	: 0MS 
    	Submit Time	: 2013-05-18 18:48:47  
    **************************************/
    

    Hello World!

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

        We know that Ivan gives Saya three problems to solve (Problem F), and this is the first problem.
        “We need a programmer to help us for some projects. If you show us that you or one of your friends is able to program, you can pass the first hurdle.
        I will give you a problem to solve. Since this is the first hurdle, it is very simple.”
        We all know that the simplest program is the “Hello World!” program. This is a problem just as simple as the “Hello World!”
        In a large matrix, there are some elements has been marked. For every marked element, return a marked element whose row and column are larger than the showed element’s row and column respectively. If there are multiple solutions, return the element whose row is the smallest; and if there are still multiple solutions, return the element whose column is the smallest. If there is no solution, return -1 -1.
        Saya is not a programmer, so she comes to you for help
        Can you solve this problem for her?

    输入

        The input consists of several test cases.
        The first line of input in each test case contains one integer N (0<N1000), which represents the number of marked element.
        Each of the next N lines containing two integers r and c, represent the element’s row and column. You can assume that 0<r,c300. A marked element can be repeatedly showed.
        The last case is followed by a line containing one zero.

    输出

         For each case, print the case number (1, 2 …), and for each element’s row and column, output the result. Your output format should imitate the sample output. Print a blank line after each test case.

    示例输入

    3
    1 2
    2 3
    2 3
    
    0

    示例输出

    Case 1:
    2 3
    -1 -1
    -1 -1

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    
    typedef struct Point
    {
    	int x;
    	int y;
    }Point;
    
    bool cmp(Point a, Point b)
    {
    	if(a.x == b.x)
    	{
    		return a.y < b.y;
    	}
    	else
    	{
    		return a.x < b.x;
    	}
    }
    
    int main()
    {
    	//freopen("in.txt", "r", stdin);
    	int n, count, x, y;
    	bool flag;
    	count = 1;
    	Point point[1005], temp[1005];
    	while (scanf("%d", &n) != EOF)
    	{
    		if (n == 0)
    		{
    			break;
    		}
    		printf("Case %d:\n", count++);
    		for (int i = 1; i <= n; i++)
    		{
    			scanf("%d %d", &x, &y);
    			temp[i].x = x;
    			temp[i].y = y;
    
    			point[i].x = x;
    			point[i].y = y;
    		}
    		sort(point + 1, point + n + 1, cmp);
    		for (int i = 1; i <= n; i++)
    		{
    			flag = false;
    			for (int j = 1; j <= n; j++)
    			{
    				if(point[j].x > temp[i].x && point[j].y > temp[i].y)
    				{
    					flag = true;
    					printf("%d %d\n", point[j].x, point[j].y);
    					break;
    				}
    			}
    			if (!flag)
    			{
    				printf("-1 -1\n");
    			}
    		}
    		printf("\n");
    	}
    
    	return 0;
    }
     
    
    
    
    /**************************************
    	Problem id	: SDUT OJ 2158 
    	User name	: asm 
    	Result		: Accepted 
    	Take Memory	: 496K 
    	Take Time	: 0MS 
    	Submit Time	: 2013-05-31 00:27:30  
    **************************************/
    

    Balloons

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

             Both Saya and Kudo like balloons. One day, they heard that in the central park, there will be thousands of people fly balloons to pattern a big image.

    They were very interested about this event, and also curious about the image.

    Since there are too many balloons, it is very hard for them to compute anything they need. Can you help them?

    You can assume that the image is an N*N matrix, while each element can be either balloons or blank.

    Suppose element and element B are both balloons. They are connected if:

    i) They are adjacent;
           ii) There is a list of element C1C2, … , Cn, while A and C1 are connected, C1 and C2are connected …Cn and B are connected.

    And a connected block means that every pair of elements in the block is connected, while any element in the block is not connected with any element out of the block.

    To Saya, element A(xa,ya)and B(xb,yb) is adjacent if |xa-xb| + |ya-yb|  1 
           But to Kudo, element A(xa,ya) and element B (xb,yb) is adjacent if |xa-xb|≤1 and |ya-yb|1
           They want to know that there’s how many connected blocks with there own definition of adjacent?

    输入

    The input consists of several test cases.
    The first line of input in each test case contains one integer N (0<N100), which represents the size of the matrix.
    Each of the next N lines contains a string whose length is N, represents the elements of the matrix. The string only consists of 0 and 1, while 0 represents a block and 1represents balloons.
    The last case is followed by a line containing one zero.

    输出

     For each case, print the case number (1, 2 …) and the connected block’s numbers with Saya and Kudo’s definition. Your output format should imitate the sample output. Print a blank line after each test case.

    示例输入

    5
    11001
    00100
    11111
    11010
    10010
    
    0

    示例输出

    Case 1: 3 2

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 105;
    int n;
    char map_saya[MAXN][MAXN], map_kudo[MAXN][MAXN];
    const int saya_moves[4][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}};
    const int kudo_moves[8][2] = {{0, -1}, {0, 1}, {1, 0}, {-1, 0}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}};
    
    void saya_dfs(int x, int y)
    {
    	int p, q;
    	map_saya[x][y] = '0';
    	for (int i = 0; i < 4; i++)
    	{
    		p = x + saya_moves[i][0];
    		q = y + saya_moves[i][1];
    		if(map_saya[p][q] == '1' && p >= 1 && p <= n && q >= 1 && q <= n)
    		{
    			saya_dfs(p, q);
    		}
    	}
    }
    
    void kudo_dfs(int x, int y)
    {
    	int p, q;
    	map_kudo[x][y] = '0';
    	for (int i = 0; i < 8; i++)
    	{
    		p = x + kudo_moves[i][0];
    		q = y + kudo_moves[i][1];
    		if(map_kudo[p][q] == '1' && p >= 1 && p <= n && q >= 1 && q <= n)
    		{
    			kudo_dfs(p, q);
    		}
    	}
    }
    
    int main()
    {
    	//freopen("in.txt", "r", stdin);
    	char c;
    	int saya_cnt, kudo_cnt;
    	int cnt = 1;
    	while (scanf("%d", &n) != EOF)
    	{
    		if(n == 0)
    		{
    			break;
    		}
    		for (int i = 1; i <= n; i++)
    		{
    			getchar();
    			for (int j = 1; j <= n; j++)
    			{
    				scanf("%c", &c);
    				map_saya[i][j] = c;
    				map_kudo[i][j] = c;
    			}
    		}
    		saya_cnt = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			for (int j = 1; j <= n; j++)
    			{
    				if(map_saya[i][j] == '1')
    				{
    					saya_dfs(i, j);
    					saya_cnt++;
    				}
    			}
    		}
    
    		kudo_cnt = 0;
    		for (int i = 1; i <= n; i++)
    		{
    			for (int j = 1; j <= n; j++)
    			{
    				if(map_kudo[i][j] == '1')
    				{
    					kudo_dfs(i, j);
    					kudo_cnt++;
    				}
    			}
    		}
    		printf("Case %d: ", cnt++);
    		printf("%d %d\n", saya_cnt, kudo_cnt);
    		printf("\n");
    	}
    	return 0;
    } 
    
    
    
    /**************************************
    	Problem id	: SDUT OJ 2152 
    	User name	: asm 
    	Result		: Accepted 
    	Take Memory	: 560K 
    	Take Time	: 0MS 
    	Submit Time	: 2013-05-31 07:52:31  
    **************************************/
    

    Greatest Number

    Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

    题目描述

        Saya likes math, because she think math can make her cleverer.
        One day, Kudo invited a very simple game:
        Given N integers, then the players choose no more than four integers from them (can be repeated) and add them together. Finally, the one whose sum is the largest wins the game. It seems very simple, but there is one more condition: the sum shouldn’t larger than a number M.
        Saya is very interest in this game. She says that since the number of integers is finite, we can enumerate all the selecting and find the largest sum. Saya calls the largest sum Greatest Number (GN). After reflecting for a while, Saya declares that she found the GN and shows her answer.
        Kudo wants to know whether Saya’s answer is the best, so she comes to you for help.
        Can you help her to compute the GN?

    输入

        The input consists of several test cases.
        The first line of input in each test case contains two integers N (0<N1000) and M(0<M 1000000000), which represent the number of integers and the upper bound.
        Each of the next N lines contains the integers. (Not larger than 1000000000)
        The last case is followed by a line containing two zeros.

    输出

        For each case, print the case number (1, 2 …) and the GN.
        Your output format should imitate the sample output. Print a blank line after each test case.

    示例输入

    2 10
    100
    2
    
    0 0

    示例输出

    Case 1: 8

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<map>
    #include<cmath>
    #include<vector>
    #include<algorithm>
    #include<set>
    #include<string>
    #include<queue>
    #include <stack>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 1000005;
    int n, m;
    int num[MAXN];
    int sum[MAXN];
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int ans;
    	int cnt = 1;
    	int count, x, left, right, Mid, temp;
    	while (scanf("%d %d", &n, &m) != EOF)
    	{
    		if(n == 0 && m == 0)
    		{
    			break;
    		}
    		for (int i = 1; i <= n; i++)
    		{
    			scanf("%d", &x);
    			if(x <= m)
    			{
    				num[i] = x;
    			}
    			else
    			{
    				i--;
    				n--;
    			}
    		}
    		count = 1;
    		for (int i = 1; i <= n; i++)
    		{
    			for (int j = 1; j <= n; j++)
    			{
    				if(num[i] + num[j] <= m)
    				{
    					sum[count++] = num[i] + num[j];
    				}
    			}
    		}
    		sort(sum + 1, sum + count);
    		ans = 0;
    		for (int i = 1; i < count; i++)
    		{
    			left = i;
    			right = count - 1;
    			while (left <= right)
    			{
    				Mid = (left + right) / 2;
    				temp = sum[i] + sum[Mid];
    				if (temp > m)
    				{
    					right = Mid - 1;
    				}
    				else
    				{
    					left = Mid + 1;
    					if(ans < temp)
    					{
    						ans = temp;
    					}
    				}
    			}
    		}
    		printf("Case %d: ", cnt++);
    		printf("%d\n", ans);
    		printf("\n");
    	}
    	return 0;
    }
    



  • 相关阅读:
    读《大道至简》第一章有感
    jdk和jre的区别
    题解 LA2911
    题解 UVa11461
    题解 UVa10791
    题解 UVa11489
    题解 LA2889
    题解 UVa11609
    题解 UVa11076
    题解 UVa11752
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835046.html
Copyright © 2011-2022 走看看