码迷,mamicode.com
首页 > 其他好文 > 详细

"高教社杯"第三届福建省大学生程序设计竞赛

时间:2016-08-25 21:46:24      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

技术分享 A.Problem 2102 Solve equation

Accept: 1032    Submit: 2471
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

You are given two positive integers A and B in Base C. For the equation:

A=k*B+d

We know there always existing many non-negative pairs (k, d) that satisfy the equation above. Now in this problem, we want to maximize k.

For example, A="123" and B="100", C=10. So both A and B are in Base 10. Then we have:

(1) A=0*B+123

(2) A=1*B+23

As we want to maximize k, we finally get one solution: (1, 23)

The range of C is between 2 and 16, and we use ‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘ to represent 10, 11, 12, 13, 14, 15, respectively.

技术分享 Input

The first line of the input contains an integer T (T≤10), indicating the number of test cases.

Then T cases, for any case, only 3 positive integers A, B and C (2≤C≤16) in a single line. You can assume that in Base 10, both A and B is less than 2^31.

技术分享 Output

For each test case, output the solution “(k,d)” to the equation in Base 10.

技术分享 Sample Input

32bc 33f 16123 100 101 1 2

技术分享 Sample Output

(0,700)(1,23)(1,0)

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给你两个数,他们是任意进制的,进制要求已经限制在2进制和16进制之间,然后求解让A = k* B + d这个等式成立的最大的k值。
解题思路:将A, B两个数先转换为十进制(因为题目要求最终输出的k和d的值必须是十进制的),然后对A % B = d,求解出d的值,然后k = (A - d) / B,即可。
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
typedef pair<int, int> PII;

const double eps = 1e-10;

const int MAXN = 1e2 + 5;
const int INF  = 0x3f3f3f3f;

int T, N, M;
int c;
char a[MAXN], b[MAXN];

int get(char* s)
{
    int t = 1, res = 0, len = strlen(s);;
    for (int i = len - 1; i >= 0; i--, t *= c) {
        if (s[i] >= 'a' && s[i] <= 'z')
            res += (s[i] - 'a' + 10) * t;
        else
            res += (s[i] - '0') * t;
    }
    return res;
}

int main() {
#ifndef ONLINE_JUDGE
     FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    int cas = 0, res;
    scanf("%d", &T);
    while (T--) {
        scanf("%s%s%d", a, b, &c);
        int aa = get(a);
        int bb = get(b);
        printf("(%d,%d)\n", aa / bb, aa % bb);
    }
    return 0;
}

技术分享 C.Problem 2104 Floor problem

Accept: 1076    Submit: 1257
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

In this problem, we have f(n,x)=Floor[n/x]. Here Floor[x] is the biggest integer such that no larger than x. For example, Floor[1.1]=Floor[1.9]=1, Floor[2.0]=2.

You are given 3 positive integers n, L and R. Print the result of f(n,L)+f(n,L+1)+...+f(n,R), please.

技术分享 Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only 3 integers n, L and R (1≤n, L, R≤10,000, L≤R).

技术分享 Output

For each test case, print the result of f(n,L)+f(n,L+1)+...+f(n,R) in a single line.

技术分享 Sample Input

31 2 3100 2 100100 3 100

技术分享 Sample Output

0382332

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:求解给定数N,在[L,R]区间中的floor(N / i)的和
解题思路:直接暴力循环求和即可
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
typedef pair<int, int> PII;

const double eps = 1e-10;

const int MAXN = 1e7 + 5;
const int INF  = 0x3f3f3f3f;

int T, N, M;

int main() {
#ifndef ONLINE_JUDGE
    FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    int cas = 0, res;
    int L, R;
    scanf ("%d", &T);
    while (T --) {
        res = 0;
        scanf ("%d %d %d", &N, &L, &R);
        for (int i = L; i <= R; i++) {
            res += N / i;
        }
        printf ("%d\n", res);
    }
    return 0;
}
技术分享 F.Problem 2107 Hua Rong Dao

Accept: 372    Submit: 806
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

Cao Cao was hunted down by thousands of enemy soldiers when he escaped from Hua Rong Dao. Assuming Hua Rong Dao is a narrow aisle (one N*4 rectangle), while Cao Cao can be regarded as one 2*2 grid. Cross general can be regarded as one 1*2 grid.Vertical general can be regarded as one 2*1 grid. Soldiers can be regarded as one 1*1 grid. Now Hua Rong Dao is full of people, no grid is empty.

There is only one Cao Cao. The number of Cross general, vertical general, and soldier is not fixed. How many ways can all the people stand?

技术分享 Input

There is a single integer T (T≤4) in the first line of the test data indicating that there are T test cases.

Then for each case, only one integer N (1≤N≤4) in a single line indicates the length of Hua Rong Dao.

技术分享 Output

For each test case, print the number of ways all the people can stand in a single line.

技术分享 Sample Input

212

技术分享 Sample Output

018

技术分享 Hint

Here are 2 possible ways for the Hua Rong Dao 2*4.

技术分享

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给你一个N * 4(1 <= N <= 4)的矩形,让你用四种不同的矩形覆盖,这些矩形分别为1*1,2*1,2*1,2*2,其中2*2的矩形有且只有一块,而其它三种矩形的个数没有限制,最终必须覆盖N*4的所有地方,然后求解有多少种覆盖方法
解题思路:直接DFS不断用着四种方法进行覆盖求解就可以了
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
typedef pair<int, int> PII;

const double eps = 1e-10;

const int MAXN = 1e7 + 5;
const int INF  = 0x3f3f3f3f;

int T, N, M;
LL ans = 0;
bool cc, vis[5][5];
void dfs (int x, int y) {
	if (x == N) {
		if (!cc)
			ans++;
		return;
	}
	int xx = x, yy = y + 1;
	if (yy > 3) {
		xx++;
		yy = 0;
	}
	if (vis[x][y]) {
		dfs (xx, yy);
	} else {
		for (int i = 0; i < 4; i++) {
			if (i == 0) { // 2 * 2
				if (!cc)
					continue;
				else {
					if (x + 1 < N && y + 1 < 4 && !vis[x + 1][y] && !vis[x][y + 1] && !vis[x + 1][y + 1]) {
						cc = false;
						vis[x][y] = vis[x + 1][y] = vis[x][y + 1] = vis[x + 1][y + 1] = true;
						dfs (xx, yy);
						cc = true;
						vis[x][y] = vis[x + 1][y] = vis[x][y + 1] = vis[x + 1][y + 1] = false;
					}
				}
			} else if (i == 1) { // 2 * 1
				if (x + 1 < N && !vis[x + 1][y]) {
					vis[x][y] = vis[x + 1][y] = true;
					dfs (xx, yy);
					vis[x][y] = vis[x + 1][y] = false;
				}
			} else if (i == 2) { // 1 * 2
				if (y + 1 < 4 && !vis[x][y + 1]) {
					vis[x][y] = vis[x][y + 1] = true;
					dfs (xx, yy);
					vis[x][y] = vis[x][y + 1] = false;
				}
			} else if (i == 3) { // 1 * 1
				vis[x][y] = true;
				dfs (xx, yy);
				vis[x][y] = false;
			}
		}
	}
}

int main() {
#ifndef ONLINE_JUDGE
	FIN;
	// FOUT;
#endif // ONLINE_JUDGE
	int cas = 0, res, ans[5] = {0, 0, 18, 284, 4862};
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &N);
		printf("%d\n", ans[N]);
	}
	return 0;
}<span style="font-size:12px;">
</span>
G.Problem 2108 Mod problem

Accept: 104    Submit: 432
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

Given one non-negative integer A and one positive integer B, it’s very easy for us to calculate A Mod B. Here A Mod B means the remainder of the answer after A is divided by B. For example, 7 Mod 5 = 2, 12 Mod 3 = 0, 0 Mod 3 = 0.

In this problem, we use the following rules to express A.

(1) One non-empty string that only contains {0,1,2,3,4,5,6,7,8,9} is valid.

For example, 123, 000213, 99213. (Leading zeros is OK in this problem)

(2) If w is valid, then [w]x if valid. Here x is one integer that 0<x<10.

For example, [012]2=012012, [35]3[7]1=3535357.

(3) If w and v are valid, then wv is valid.

For example, w=[231]2 and v=1, then wv=[231]21 is valid (which is 2312311).

Now you are given A and B. Here A is express as the rules above and B is simply one integer, you are expected to output the A Mod B.

技术分享 Input

The first line of the input contains an integer T(T≤10), indicating the number of test cases.

Then T cases, for any case, only two lines.

The first line is one non-empty and valid string that expresses A, the length of the string is no more than 1,000.

The second line is one integer B(0<B<2,000,000,000).

You may assume that the length of number A in decimal notation will less than 2^63.

技术分享 Output

For each test case, output A Mod B in a single line.

技术分享 Sample Input

3
[0]9[[1]2]3
10007
[[213231414343214231]5]1
10007
[0012]1
1

技术分享 Sample Output

1034
3943
0

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给你一个字符串A和一个数字B,其中这个字符串中存在数字0-9以及‘[‘和‘]‘,字符串处理如下:
[123]2=123123
[123]21=1231231
最终让你求解这个字符串最后形成的数字对B的取模
解题思路:可以通过ret = (ret * 10 % B + A[i] - ‘0‘) % B的思路来求解,然后通过递归思想,将[]中的数字一一求出即可
/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, int>PLI;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
	int len = bes.length();
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
	int d = p < q ? 1 : -1;
	cout << "Debug out : ";
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
#endif
}

template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
	int len = bes.length();
	cout << "Debug out : ";
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
#endif
}

void IO_Init() {
	ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
	return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e3 + 5;
const int INF = 0x3f3f3f3f;

/*头文件模板*/

int T, slen;
char A[MAXN];
LL b;
stack<char>C;

LL mod_pow(LL x, int p, LL mod) {
	LL ret = 1;
	while(p > 0) {
		if(p & 1) ret = ret * x % mod;
		x = x * x % mod;
		p >>= 1;
	}
	return ret;
}

PLI solve(int &l) {
	int cnt = 0,len = 0;
	LL ans = 0;
	while(l < slen) {
		if(A[l] == '[') {
			int r = ++ l;
			PLI a = solve(r);
			int k = A[r + 1] - '0';
			for(int i = 0; i < k; i ++) {
				ans = (ans * mod_pow(10, a.second, b) % b + a.first) % b;
			}
			len += k * a.second;
			l = r + 2;
		} else if(A[l] == ']') {
			break;
		} else {
			ans = (ans * 10 % b + A[l ++] - '0') % b;
			len ++;
		}
	}
	return PLI(ans, len);
}

int main() {
#ifndef ONLINE_JUDGE
	//FIN;
	//FOUT;
#endif
	IO_Init();
	scanf("%d", &T);
	while(T --) {
		scanf("%s%I64d", A, &b);
		slen = strlen(A);
		int k = 0;
		printf("%I64d\n", solve(k).first);
	}
	return 0;
}
技术分享 H.Problem 2109 Mountain Number

Accept: 228    Submit: 571
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

One integer number x is called "Mountain Number" if:

(1) x>0 and x is an integer;

(2) Assume x=a[0]a[1]...a[len-2]a[len-1](0≤a[i]≤9, a[0] is positive). Any a[2i+1] is larger or equal to a[2i] and a[2i+2](if exists).

For example, 111, 132, 893, 7 are "Mountain Number" while 123, 10, 76889 are not "Mountain Number".

Now you are given L and R, how many "Mountain Number" can be found between L and R (inclusive) ?

技术分享 Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only two integers L and R (1≤L≤R≤1,000,000,000).

技术分享 Output

For each test case, output the number of "Mountain Number" between L and R in a single line.

技术分享 Sample Input

31 101 1001 1000

技术分享 Sample Output

954384

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给你一个区间[L,R]求解这个区间中满足奇数位大于相邻偶数位的数字的个数。
解题思路:典型的数位DP,基本是道裸题,直接从高位处理,然后用在找寻下一位的时候,用一个变量标记当前是奇数位还是偶数位,然后用一个变量标记是否是最高位,因为最高位没有pre(上一位),所以它需要特殊判断。
/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    while (p != q) {
        if (flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if (p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
    int len = bes.length();
    if (len >= 2) cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
    int d = p < q ? 1 : -1;
    cout << "Debug out : ";
    while (p != q) {
        if (flag) cout << Gap[0] << *p << Gap[1];
        else cout << *p;
        p += d;
        if (p != q && !flag) cout << Gap;
    }
    cout << endl;
}

template<typename T>
void debug (const T &a, string bes = "") {
    int len = bes.length();
    cout << "Debug out : ";
    if (len >= 2) cout << bes[0] << a << bes[1] << endl;
    else cout << a << endl;
}

void IO_Init() {
    ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
    return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 32000 + 5;
const int INF = 0x3f3f3f3f;

/*头文件模板*/
LL dp[20][10][2];
int bit[20];
LL dfs (int pos, int pre, int z, int odd, int limit) {
    if (pos < 0) return 1;
    if (!limit && dp[pos][pre][odd] != -1)
        return  dp[pos][pre][odd];
    int ends = limit ? bit[pos] : 9;
    LL ret = 0;
    for (int i = 0; i <= ends; i ++)
        if (z) {
            ret += dfs (pos - 1, i, 0, !odd, limit && (i == ends) );
        } else if (odd && pre <= i) {
            ret += dfs (pos - 1, i, 0, !odd, limit && (i == ends) );
        } else if (!odd && pre >= i) {
            ret += dfs (pos - 1, i, 0, !odd, limit && (i == ends) );
        }
    if (!limit)
        dp[pos][pre][odd] = ret;
    return ret;
}
void init() {
    mem (dp, -1);
}
LL solve (int x) {
    int len = 0;
    while (x) {
        bit[len ++] = x % 10;
        x /= 10;
    }
    return dfs (len - 1, 9, 1, 0, 1);
}

int main() {
#ifndef ONLINE_JUDGE
    //FIN;
    //FOUT;
#endif
    IO_Init();
    int T;
    scanf ("%d", &T);
    int l, r;
    while (T --) {
        init();
        scanf ("%d%d", &l, &r);
        printf ("%I64d\n", solve (r) - solve (l - 1) );
    }
    return 0;
}

技术分享 I.Problem 2110 Star

Accept: 930    Submit: 2773
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

Overpower often go to the playground with classmates. They play and chat on the playground. One day, there are a lot of stars in the sky. Suddenly, one of Overpower’s classmates ask him: “How many acute triangles whose inner angles are less than 90 degrees (regarding stars as points) can be found? Assuming all the stars are in the same plane”. Please help him to solve this problem.

技术分享 Input

The first line of the input contains an integer T (T≤10), indicating the number of test cases.

For each test case:

The first line contains one integer n (1≤n≤100), the number of stars.

The next n lines each contains two integers x and y (0≤|x|, |y|≤1,000,000) indicate the points, all the points are distinct.

技术分享 Output

For each test case, output an integer indicating the total number of different acute triangles.

技术分享 Sample Input

130 010 05 1000

技术分享 Sample Output

1

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给定n个点,求解这些能够构成多少个锐角三角形。
解题思路:通过数据范围,我们就可以意识到这道题目直接暴力完全没有任何压力,通过a^2 + b^2>c^2来判断是否为锐角三角形
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
typedef pair<int, int> PII;

const double eps = 1e-10;

const int MAXN = 1e2 + 5;
const int INF  = 0x3f3f3f3f;

int T, N, M, x[MAXN], y[MAXN];

bool judge(int x2, int y2, int x3, int y3, int x4, int y4)
{
    LL a = (LL)(x2 - x3) * (x2 - x3) + (LL)(y2 - y3) * (y2 - y3);
    LL b = (LL)(x2 - x4) * (x2 - x4) + (LL)(y2 - y4) * (y2 - y4);
    LL c = (LL)(x4 - x3) * (x4 - x3) + (LL)(y4 - y3) * (y4 - y3);
    return (a < b + c) && (b < a + c) && (c < a + b);
}

int main() {
#ifndef ONLINE_JUDGE
     FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    int cas = 0, res;
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
            scanf("%d%d", &x[i], &y[i]);
        int cnt = 0;
        for (int i = 0; i < N; i++) {
            for (int j = i + 1; j < N; j++) {
                for (int k = j + 1; k < N; k++) {
                    if (judge(x[i], y[i], x[j], y[j], x[k], y[k]))
                        cnt++;
                }
            }
        }
        printf("%d\n", cnt);
;    }
    return 0;
}

技术分享 J.Problem 2111 Min Number

Accept: 857    Submit: 1705
Time Limit: 1000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

Now you are given one non-negative integer n in 10-base notation, it will only contain digits (‘0‘-‘9‘). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].

For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.

Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?

Please note that in this problem, leading zero is not allowed!

技术分享 Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.

技术分享 Output

For each test case, output the minimum number we can get after no more than M operations.

技术分享 Sample Input

39012 09012 19012 2

技术分享 Sample Output

901210921029

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛

题意:给定一个数,你可以对它进行一种操作:交换任意两个数,然后求解通过x次操作后,得到的最小的数是多少?
解题思路:纯暴力,有多少次操作就循环多少次,然后一次一次的模拟交换高位中大的数到低位即可

#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

//#pragma comment(linker, "/STACK:1024000000,1024000000")

#define FIN             freopen("input.txt","r",stdin)
#define FOUT            freopen("output.txt","w",stdout)
#define fst             first
#define snd             second

typedef __int64  LL;
typedef pair<int, int> PII;

const double eps = 1e-10;

const int MAXN = 1000 + 5;
const int INF  = 0x3f3f3f3f;

int T, N, M;
char buf[MAXN];
int main() {
#ifndef ONLINE_JUDGE
    FIN;
    // FOUT;
#endif // ONLINE_JUDGE
    int cas = 0, res, b;
    scanf ("%d", &T);
    while (T --) {
        scanf ("%s %d", buf, &M);
        int len = strlen (buf);
        if (!M) {
            printf ("%s\n", buf);
            continue;
        }
        b = -1;
        if (buf[0] >= '2') {
            for (int j = len - 1; j > 0; j--) {
                if (buf[j] == '0') continue;
                if (buf[j] >= buf[0]) continue;
                if (b == -1 || buf[b] > buf[j]) {
                    b = j;
                }
            }
            if (b != -1) {
                swap (buf[b], buf[0]);
                M --;
            }
        }
        for (int k = 0, i = 1; k < M && i < len; i++) {
            b = -1;
            for (int j = len - 1; j > i; j --) {
                if (buf[j] >= buf[i]) continue;
                if (b == -1 || buf[b] > buf[j]) {
                    b = j;
                }
            }
            if (b != -1) {
                swap (buf[i], buf[b]);
                k ++;
            }
        }
        printf ("%s\n", buf);
    }
    return 0;
}

技术分享 K.Problem 2112 Tickets

Accept: 479    Submit: 840
Time Limit: 3000 mSec    Memory Limit : 32768 KB

技术分享 Problem Description

You have won a collection of tickets on luxury cruisers. Each ticket can be used only once, but can be used in either direction between the 2 different cities printed on the ticket. Your prize gives you free airfare to any city to start your cruising, and free airfare back home from wherever you finish your cruising.

You love to sail and don‘t want to waste any of your free tickets. How many additional tickets would you have to buy so that your cruise can use all of your tickets?

Now giving the free tickets you have won. Please compute the smallest number of additional tickets that can be purchased to allow you to use all of your free tickets.

技术分享 Input

There is one integer T (T≤100) in the first line of the input.

Then T cases, for any case, the first line contains 2 integers n, m (1≤n, m≤100,000). n indicates the identifier of the cities are between 1 and n, inclusive. m indicates the tickets you have won.

Then following m lines, each line contains two integers u and v (1≤u, v≤n), indicates the 2 cities printed on your tickets, respectively.

技术分享 Output

For each test case, output an integer in a single line, indicates the smallest number of additional tickets you need to buy.

技术分享 Sample Input

35 31 31 24 56 51 31 21 61 51 43 21 21 2

技术分享 Sample Output

120

技术分享 Source

“高教社杯”第三届福建省大学生程序设计竞赛
题意:给定n个城市和m张免费票,你可以从任意城市出发,终点站可以是任意城市,最终你需要将这m张票都使用完,并且如果从某个城市到另一个城市没有票可以用,那么你就必须得买票,求解最少的买票数。
解题思路:这道题是比较遗憾的,自己接近一年没有刷过关于图论的题目了,对于欧拉回路和欧拉道路的性质记得不太清了,导致这道题目不知道怎么去处理,后来翻看欧拉道路的性质就明白了,我们只需要记录一个点的度数位奇数的点的个数,为什么呢,按照欧拉道路的说法,如果一个图可以一笔画完,那么除了终点和起点是奇数个度,其他的点由于机油出度也有入度所以为二的倍数即为偶数,所以在这道题目中,一个点为奇数的话,要么是终点或起点就是一个会中断的点(何为中断的点,即必须要新建一条边才能接着走),所以我们将最终求解为(度数为奇数的点的个数-起点和终点)/2,除以2是因为我们求解的是新建的边有多少种,而一条边有两个点,所以要除以二。
以上题解是针对单个联通块不会形成度数都是偶数的情况
而如果要处理度数为偶数的情况,用并查集先得到多少个联通块,依旧跟前面一样,得到度数为奇数的点的个数ans,然后得到给定的边形成的联通块的个数x,因为有多少个联通块就最少有x-1条边然后加上开始和结束两个点,然后ans - (x - 1 + 1) * 2,接下来ans/2得到多少条边,然后就进行处理就可以了。
最终代码很简单,大家自己懵逼去吧
这份代码有问题,但是对于题目给出的数据依旧可以过,可能题目的要求没有给出,单个联通块不会形成一个度数都是偶数的环。
/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
	int len = bes.length();
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
	int d = p < q ? 1 : -1;
	cout << "Debug out : ";
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
#endif
}

template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
	int len = bes.length();
	cout << "Debug out : ";
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
#endif
}

void IO_Init() {
	ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
	return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;

/*头文件模板*/

int n, m;
int d[MAXN];
int main() {
#ifndef ONLINE_JUDGE
	//FIN;
	//FOUT;
#endif
	IO_Init();
	int T, u, v;
	scanf("%d", &T);
	while(T --) {
		mem(d, 0);
		scanf("%d%d", &n, &m);
		for(int i = 0; i < m; i ++) {
			scanf("%d%d", &u, &v);
			d[v] ++;
			d[u] ++;
		}
		int ans = 0;
		for(int i = 1; i <= n; i ++) {
			if(d[i] & 1) ans ++;
		}
		if(ans <= 2) {
			ans = 0;
		} else {
			ans = (ans - 2) / 2;
		}
		printf("%d\n", ans);
	}

	return 0;
}


并查集的使用:
/*头文件模板*/

#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cctype>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iomanip>
#include <typeinfo>
#include <iostream>
#include <algorithm>
#include <functional>

using namespace std;

#define pb push_back
#define mp make_pair
#define mem(a, x) memset(a, x, sizeof(a))
#define copy(a, b) memcpy(a, b, sizeof(a))
#define lson rt << 1, l, mid
#define rson rt << 1|1, mid + 1, r
#define FIN freopen("input.txt", "r", stdin)
#define FOUT freopen("output.txt", "w", stdout)

typedef long long LL;
typedef pair<int, int > PII;
typedef pair<int, string> PIS;
typedef pair<LL, LL>PLL;
typedef unsigned long long uLL;

template<typename T>
void print (T* p, T* q, string Gap = " ", bool flag = false) {
	int d = p < q ? 1 : -1;
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
}

template<typename T>
void print (const T &a, string bes = "") {
	int len = bes.length();
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
}

template<typename T>
void debug (T* p, T* q, string Gap = " ", bool flag = false) {
#ifndef ONLINE_JUDGE
	int d = p < q ? 1 : -1;
	cout << "Debug out : ";
	while (p != q) {
		if (flag) cout << Gap[0] << *p << Gap[1];
		else cout << *p;
		p += d;
		if (p != q && !flag) cout << Gap;
	}
	cout << endl;
#endif
}

template<typename T>
void debug (const T &a, string bes = "") {
#ifndef ONLINE_JUDGE
	int len = bes.length();
	cout << "Debug out : ";
	if (len >= 2) cout << bes[0] << a << bes[1] << endl;
	else cout << a << endl;
#endif
}

void IO_Init() {
	ios::sync_with_stdio (false);
}

LL LLabs (const LL a) {
	return a >= 0 ? a : -a;
}

const double PI = 3.1415926535898;
const double eps = 1e-10;
const int MAXM = 1e5 + 5;
const int MAXN = 1e5 + 5;
const int INF = 0x3f3f3f3f;

/*头文件模板*/

int n, m;
int d[MAXN];
int par[MAXN];
void init() {
	for(int i = 0; i < MAXN; i ++) {
		par[i] = i;
	}
}
int finds(int x) {
	return par[x] == x ? x : par[x] = finds(par[x]);
}

void unite(int x, int y, int <) {
	x = finds(x);
	y = finds(y);
	if(x != y) {
		par[x] = y, lt --;
	}
}
int main() {
#ifndef ONLINE_JUDGE
	//FIN;
	//FOUT;
#endif
	IO_Init();
	int T, u, v;
	scanf("%d", &T);
	while(T --) {
		mem(d, 0);
		init();
		scanf("%d%d", &n, &m);
		int lt = 0;
		for(int i = 0; i < m; i ++) {
			scanf("%d%d", &u, &v);
			if(!d[v]) lt ++;
			if(!d[u]) lt ++;
			d[v] ++;
			d[u] ++;
			unite(u, v, lt);
		}
		int ans = 0;
		for(int i = 1; i <= n; i ++) {
			if(d[i] & 1) ans ++;
		}
		lt --;
		ans -= (lt + 1) * 2;
		ans /= 2;
		debug(lt);
		if(ans <= 0) {
			printf("%d\n", lt);
		} else {
			printf("%d\n", ans + lt);
		}
	}
	return 0;
}

 






"高教社杯"第三届福建省大学生程序设计竞赛

标签:

原文地址:http://blog.csdn.net/qq_18661257/article/details/52254389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!