1、HDU 5670 Machine
解题思路:
变化的规律为红 -> 绿 -> 蓝 -> 红
将红、绿、蓝分别表示0、1、2
原问题就转化为求 n 的三进制表示的最低的 m 位,即求n mod 3^m的三进制表示
复杂度 O(m)
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int maxn = 30 + 5;
char s[maxn];
int m;
LL n;
int main()
{
// freopen("in.txt", "r", stdin);
int T;
while (scanf("%d", &T) != EOF) {
while (T--) {
cin>>m>>n;
for (int i=0; i<m; ++i) {
s[i] = 'R';
}
LL x = n%3;
LL y = n/3;
for (int i=m-1; i>=0; --i) {
if (x == 1) {
s[i] = 'G';
} else if (x == 2) {
s[i] = 'B';
}
if (y == 0) {
break;
}
x = y%3;
y /= 3;
}
for (int i=0; i<m; ++i) {
cout<<s[i];
}
cout<<endl;
}
}
return 0;
}
2、HDU 5671 Matrix
解题思路:
暴力肯定是不可行的
可以用两个数组记录行和列交换的操作
再用两个数组记录行和列加一个数的操作
复杂度 O(mn + q)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1000 + 10;
int Matrix[maxn][maxn];
int row[maxn], row_add[maxn]; //行
int column[maxn], column_add[maxn]; //列
int main()
{
// freopen("in.txt", "r", stdin);
int T;
while (scanf("%d", &T) != EOF) {
while (T--) {
int n, m, q;
scanf("%d%d%d", &n, &m, &q);
for (int i=1; i<=n; ++i) {
for (int j=1; j<=m; ++j) {
scanf("%d", &Matrix[i][j]);
}
}
int a, x, y;
memset(row_add, 0, sizeof(row_add));
memset(column_add, 0, sizeof(column_add));
for (int i=1; i<=n; ++i) {
row[i] = i;
}
for (int i=1; i<=m; ++i) {
column[i] = i;
}
for (int i=0; i<q; ++i) {
scanf("%d%d%d", &a, &x, &y);
if (a == 1) {
int t = row[x];
row[x] = row[y];
row[y] = t;
} else if (a == 2) {
int t = column[x];
column[x] = column[y];
column[y] = t;
} else if (a == 3) {
row_add[row[x]] += y;
} else {
column_add[column[x]] += y;
}
}
int t;
for (int i=1; i<=n; ++i) {
for (int j=1; j<m; ++j) {
t = Matrix[row[i]][column[j]] + row_add[row[i]] + column_add[column[j]];
printf("%d ", t);
}
t = Matrix[row[i]][column[m]] + row_add[row[i]] + column_add[column[m]];
printf("%d\n", t);
}
}
}
return 0;
}
3、HDU 5675 ztr loves math
题意:
是否存在正整数 x 和 y,满足 n = x^2 - y^2 (n 为正整数)
解题思路:
据说这次 BC 出现很多问题(导致unrated)所以肯定不知道哪里有毒
一:
n = x^2 - y^2 = (x+y) * (x-y)
令 a = x+y; b = x-y;
那么 n = a*b; x = (a+b) / 2; y = (a-b) / 2;
所以 (a-b) != 0 && (a-b) %2 == 0 时有解
复杂度O(sqrt(n))
以为会TLE,没想到358MS过了
二:
假设存在解时 y = x+i; n = (x+i)^2 - x^2;
那么 n = 2xi + i^2;
i = 1时,n = 2x + 1;
i = 2时,n = 4x + 4;
容易得出结论,当 n 不等于 1 && n 不等于 4 && n 为奇数或者 4 的倍数时,方程一定有正整数解
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
int main()
{
// freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
LL n;
scanf("%I64d", &n);
bool flag = false;
int t = (int)sqrt(n);
for (int i=1; i<=t; ++i) {
if (n%i == 0) {
LL a = i;
LL b = n / i;
if ((a-b) != 0 && (a-b)%2 == 0) {
// cout<<a<<" "<<b<<endl;
flag = true;
break;
}
}
}
if (flag) {
printf("True\n");
} else {
printf("False\n");
}
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long LL;
int main()
{
// freopen("in.txt", "r", stdin);
int T;
scanf("%d", &T);
while (T--) {
LL n;
scanf("%I64d", &n);
if (n != 1 && n != 4 && (n%2 != 0 || n%4 == 0)) {
printf("True\n");
} else {
printf("False\n");
}
}
return 0;
}
4、CodeForces_673A Bear and Game
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 100;
int t[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int n;
scanf("%d", &n);
t[0] = 0;
for (int i = 1; i <= n; ++i) {
scanf("%d", &t[i]);
}
++n;
t[n] = 90;
bool flag = false;
for (int i = 1; i <= n; ++i) {
if (t[i] - t[i-1] > 15) {
flag = true;
printf("%d\n", t[i-1] + 15);
break;
}
}
if (!flag) {
printf("90\n");
}
return 0;
}
5、CodeForces_673B Problems for Round
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
vector<int> Div1, Div2;
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int u, v;
for (int i = 0; i < m; ++i) {
scanf("%d%d", &u, &v);
if (u < v) {
swap(u, v);
}
Div1.push_back(u);
Div2.push_back(v);
}
if (m == 0) {
printf("%d\n", n-1);
} else {
int a = *max_element(Div2.begin(), Div2.end());
int b = *min_element(Div1.begin(), Div1.end());
printf("%d\n", max(0, b-a));
}
return 0;
}
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int maxn = 1e5 + 10;
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int n, m;
scanf("%d%d", &n, &m);
int u, v;
int div2_Max = 1, div1_Min = n;
for (int i = 0; i < m; ++i) {
scanf("%d%d", &u, &v);
if (u < v) {
swap(u, v);
}
div1_Min = min(div1_Min, u);
div2_Max = max(div2_Max, v);
}
if (m == 0) {
printf("%d\n", n-1);
} else {
printf("%d\n", max(0, div1_Min - div2_Max));
}
return 0;
}
6、HDU 2054 A == B ?
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e5 + 10;
char A[maxn], B[maxn];
void Init(char s[]);
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
while (scanf("%s%s", A, B) != EOF) {
Init(A);
Init(B);
if (strcmp(A, B) == 0) {
printf("YES\n");
} else {
printf("NO\n");
}
}
return 0;
}
void Init(char s[])
{
int len = strlen(s);
//strstr(str1,str2)
//若str2是str1的子串,则返回str2在str1的首次出现的地址
//如果str2不是str1的子串,则返回NULL
if (strstr(s, ".")) {
while (s[len-1] == '0') {
s[len-1] = '\0';
--len;
}
if (s[len-1] == '.') {
s[len-1] = '\0';
}
}
}
7、Codeforces_5A Chat Server's Outgoing Traffic
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
char s[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int Count = 0;
int ans = 0;
while (gets(s)) {
if (s[0] == '+') {
++Count;
} else if (s[0] == '-') {
--Count;
} else {
int len = strlen(s);
int pos = 0;
while (s[pos] != ':') {
++pos;
}
ans += Count * (len - (pos+1));
}
}
printf("%d\n", ans);
return 0;
}
8、Codeforces_686A Free Ice Cream
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
char cmd[2];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
ull n, x;
ull d;
scanf("%I64d%I64d", &n, &x);
ull Count = 0;
for (ull i = 0; i < n; ++i) {
scanf("%s%I64d", cmd, &d);
if (cmd[0] == '+') {
x += d;
} else {
if (x < d) {
++Count;
} else {
x -= d;
}
}
}
printf("%I64d %I64d\n", x, Count);
return 0;
}
9、Codeforces_686B Little Robber Girl's Zoo
题目要求操作次数不能超过 20000 ,n^2 的循环肯定会 wa,但是不确定 n^3 循环操作次数会不会超 20000
不过最后 AC 了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int a[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
}
int s = 0, e = 0;
for (int i = 1; i <= n*n; ++i) {
bool flag = false;
for (int j = n; j > 0; --j) {
if (a[j] < a[j-1]) {
if (!flag) {
flag = true;
e = j;
}
--j;
s = j;
} else {
if (flag) {
printf("%d %d\n", s, e);
flag = false;
for (int k = s; k <= e; k+=2) {
swap(a[k], a[k+1]);
}
}
}
}
if (flag) {
printf("%d %d\n", s, e);
for (int j = s; j <= e; j+=2) {
swap(a[j], a[j+1]);
}
}
}
return 0;
}
10、HDU 1008 Elevator
解题思路:
注意有可能在同一层停留
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 100 + 10;
int num[maxn];
int main()
{
#ifdef __AiR_H
freopen("in.txt", "r", stdin);
#endif // __AiR_H
int N;
while (scanf("%d", &N) != EOF && N != 0) {
int ans = 0;
num[0] = 0;
for (int i = 1; i <= N; ++i) {
scanf("%d", &num[i]);
if (num[i-1] < num[i]) {
ans += (num[i] - num[i-1]) * 6 + 5;
} else {
ans += (num[i-1] - num[i]) * 4 + 5;
}
}
printf("%d\n", ans);
}
return 0;
}