Shopping
Time Limit: 1000MS Memory limit: 65536K
题目描述
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 first line of input in each test case contains one integerN(0<N<100001), represents the number of shops.
The next line containsNintegers, 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.
输出
示例输入
4 24 13 89 37 6 7 30 41 14 39 42 0
示例输出
152 70
提示
#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
题目描述
Given N phone numbers, your task is to find whether there exits two numbers A and B that A is B’s prefix.
输入
The first line of input in each test case contains one integerN(0<N<1001), represent the number of phone numbers.
The next line containsNintegers, describing the phone numbers.
The last case is followed by a line containing one zero.
输出
示例输入
2 012 012345 2 12 012345 0
示例输出
NO YES
#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 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 first line of input in each test case contains one integerN(0<N≤1000), which represents the number of marked element.
Each of the nextNlines containing two integersrandc, represent the element’s row and column. You can assume that 0<r,c≤300. A marked element can be repeatedly showed.
The last case is followed by a line containing one zero.
输出
示例输入
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有疑问?点这里^_^
题目描述
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 anN*Nmatrix, while each element can be either balloons or blank.
Suppose elementAand elementBare both balloons. They are connected if:
i) They are adjacent;
ii) There is a list of elementC1,C2, … ,Cn, whileAandC1are connected,C1andC2are connected …CnandBare 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, elementA(xa,ya)andB(xb,yb)is adjacent if |xa-xb| + |ya-yb|≤1
But to Kudo, elementA(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 first line of input in each test case contains one integerN(0<N≤100), which represents the size of the matrix.
Each of the nextNlines contains a string whose length isN, 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.
输出
示例输入
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有疑问?点这里^_^
题目描述
One day, Kudo invited a very simple game:
GivenNintegers, 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 numberM.
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 first line of input in each test case contains two integersN(0<N≤1000) andM(0<M≤1000000000), which represent the number of integers and the upper bound.
Each of the nextNlines contains the integers. (Not larger than 1000000000)
The last case is followed by a line containing two zeros.
输出
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;
}