半夜睡不着在VJ上挂了5道水题练手:http://vjudge.net/contest/143771#overview
A:CF496B
题目传送门:http://codeforces.com/problemset/problem/496/B
类型:搜索
#include<string>
#include<cstdio>
#include<cstring>
#include<map>
#include<queue>
#include<iostream>
using namespace std;
char str[1005];
map<string,int> m;
string ans;
string change1(string str)
{
int len = str.length();
for (int i = 0; i < len; i++)
{
if (str[i] == '9') str[i] = '0';
else str[i] = str[i] + 1;
}
return str;
}
string change2(string str)
{
int len = str.length();
char tmp = str[len-1];
for (int i = len-2; i >= 0; i--)
str[i+1] = str[i];
str[0] = tmp;
return str;
}
void bfs(string a)
{
queue<string> q;
m[a] = 1;
q.push(a);
ans = a;
while (!q.empty())
{
string s = q.front();
q.pop();
if (s < ans) ans = s;
string s1 = change1(s);
string s2 = change2(s);
if (!m[s1])
{
q.push(s1);
m[s1] = 1;
}
if (!m[s2])
{
q.push(s2);
m[s2] = 1;
}
}
}
int main()
{
int n;
scanf("%d", &n);
string str;
cin >> str;
bfs(str);
cout << ans << endl;
return 0;
}
B:CF609C
题目传送门:http://codeforces.com/problemset/problem/609/C
类型:思维
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100005;
int num[maxn];
int main()
{
int n,sum = 0,Min,Max;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", &num[i]);
sum += num[i];
}
if (sum % n == 0)
{
Min = Max = sum / n;
}
else
{
Min = sum / n;
Max = Min + 1;
}
int ans1 = 0;
int ans2 = 0;
for (int i = 0; i < n; i++)
{
if (num[i] < Min)
ans1 += Min - num[i];
else if (num[i] > Max)
ans2 += num[i] - Max;
}
printf("%d\n",max(ans1,ans2));
}
C:CF560B
题目传送门:http://codeforces.com/problemset/problem/560/B
类型:简单思维
#include<cstdio>
#include<algorithm>
using namespace std;
#define ok {puts("YES");return 0;}
int main()
{
int a1,b1,a2,b2,a3,b3;
scanf("%d %d %d %d %d %d", &a1, &b1, &a2, &b2, &a3, &b3);
if (a2 + a3 <= a1 && max(b2,b3) <= b1) ok
if (a2 + b3 <= a1 && max(b2,a3) <= b1) ok
if (b2 + a3 <= a1 && max(a2,b3) <= b1) ok
if (b2 + b3 <= a1 && max(a2,a3) <= b1) ok
if (a2 + a3 <= b1 && max(b2,b3) <= a1) ok
if (a2 + b3 <= b1 && max(b2,a3) <= a1) ok
if (b2 + a3 <= b1 && max(a2,b3) <= a1) ok
if (b2 + b3 <= b1 && max(a2,a3) <= a1) ok
puts("NO");
return 0;
}
D:CF500C
题目传送门:http://codeforces.com/problemset/problem/500/C
类型:贪心
#include<cstdio>
int w[505];
int b[1005];
bool vis[505];
int num[505];
int main()
{
int n,m,cnt = 0,ans = 0;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; i++)
scanf("%d", &w[i]);
for (int i = 0; i < m; i++)
{
scanf("%d", &b[i]);
if (vis[b[i]] == 0)
{
num[cnt++] = b[i];
vis[b[i]] = true;
}
}
int j;
for (int i = 1; i < m; i++)
{
for (j = 0; j < cnt; j++)
{
if (b[i] == num[j])
break;
else
ans += w[num[j]];
}
for (int k = j-1; k >= 0; k--)
{
num[k+1] = num[k];
}
num[0] = b[i];
}
printf("%d\n",ans);
return 0;
}
E:HDU5626
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5626
类型:简单分类讨论
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
long long seed;
inline long long rand(long long l, long long r)
{
static long long mo = 1e9 + 7, g = 78125;
return l + ((seed *= g) %= mo) % (r-l+1);
}
const ll INF = 1e15;
int main()
{
int t,n;
ll x,y,Min1,Min2,Max1,Max2;
scanf("%d", &t);
while (t--)
{
// Min1 = Min2 = INF; //INF多次累加溢出,不要这样用
// Max2 = Max2 = -INF;//INF多次累加溢出,不要这样用
scanf("%d %I64d", &n, &seed);
x = rand(-1000000000, 1000000000);
y = rand(-1000000000, 1000000000);
Min1 = Max1 = x + y;
Min2 = Max2 = x - y;
// printf("%I64d\n",Min1);
for (int i = 1; i < n; i++)
{
x = rand(-1000000000, 1000000000),
y = rand(-1000000000, 1000000000);
Min1 = min(Min1,x+y);
Min2 = min(Min2,x-y);
Max1 = max(Max1,x+y);
Max2 = max(Max2,x-y);
}
ll ans = max(Max1-Min1,Max2-Min2);
printf("%I64d\n",ans);
}
return 0;
}
本文分享了在VJ竞赛中解决五道题目的过程与思路。涉及搜索、思维、贪心等算法,并通过具体代码示例展示了每种类型的解题方法。
137

被折叠的 条评论
为什么被折叠?



