Problem A: 求和(caioj3000)
Time Limit: 1000 ms Memory Limit: 128 MB
Description
水?
#include <iostream>
#include <algorithm>
#define SIZE 10001
using namespace std;
int a[SIZE];
int main(int argc, char** argv)
{
int x, y, n, i, MAX = 0, MIN = 1000000000, res = 0;
cin >> x >> y >> n;
for (i = 1; i <= n; i++)
{
cin >> a[i];
}
sort(a + 1, a + 1 + n);
for (i = 1; i <= n; i++)
{
if (a[i] < x)
{
continue;
}
if (a[i] > y)
{
break;
}
if (MIN > a[i])
{
MIN = a[i];
}
if (MAX < a[i])
{
MAX = a[i];
}
}
for (i = 1; i <= n; i++)
{
if (a[i] < x)
{
continue;
}
if (a[i] > y)
{
break;
}
if ((a[i] != MIN) && (a[i] != MAX))
{
res += a[i];
}
}
cout << res << endl;
return 0;
}
好好好,下一题。
Problem B: 素数(caioj3001)
【后面的caioj以此类推,每个都是前一个+1】
Time Limit: 1000 ms Memory Limit: 128 MB
Description
依然水?
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
string s;
bool prime(long long a)
{
if (a < 2)
{
return false;
}
long long len = sqrt(a), i;
for (i = 2; i <= len; i++)
{
if (a % i == 0)
{
return false;
}
}
return true;
}
int main(int argc, char** argv)
{
int n, i, j, k, res = 0;
long long temp;
cin >> s;
n = s.size();
for (i = 0; i < n; i++)
{
for (j = i; j < n; j++)
{
temp = 0;
for (k = i; k <= j; k++)
{
temp *= 10;
temp += (s[k] - '0');
}
if (prime(temp))
{
res++;
}
}
}
cout << res << endl;
return 0;
}
Problem C: 疯狂阶乘
Time Limit: 1000 ms Memory Limit: 128 MB
Description
别忘了,只要乘到p······
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
long long n, p, i, temp = 1, res = 0;
cin >> n >> p;
n = min(n, p);
for (i = 1; i <= n; i++)
{
temp *= i;
temp %= p;
res += temp;
res %= p;
}
cout << res << endl;
return 0;
}
Problem D: 咒语
Time Limit: 1000 ms Memory Limit: 128 MB
Description
······
#include <iostream>
#include <cstring>
#define SIZE 4002
#define NUM 2001
using namespace std;
short n, m, b[SIZE][SIZE], r[SIZE], c[SIZE], a[NUM];
int main()
{
short i, j, k, x, y;
scanf("%hd", &n);
for (i = 1; i <= n; i++)
{
scanf("%hd%hd", &x, &y);
r[i] = x + NUM;
c[i] = y + NUM;
b[r[i]][c[i]] = i;
}
scanf("%hd", &m);
short t;
while (m--)
{
scanf("%hd", &k);
memset(a, 0, sizeof (a));
for (i = 1; i <= k; i++)
{
scanf("%hd%hd", &x, &y);
for (j = 1; j <= n; j++)
{
if ((r[j] + x < 0) || (r[j] + x > SIZE - 1) || (c[j] + y < 0) || (c[j] + y > SIZE - 1))
{
continue;
}
t = b[r[j]+x][c[j]+y];
a[t]++;
}
}
short count = 0;
short _max = 0;
short index;
for (i = 1; i <= n; i++)
{
//if (a[i])printf("a[%hd]=%hd max=%hd k =%hd\n", i, a[i], max, k);
if (a[i] > _max)
{
_max = a[i];
index = i;
count = 1;
}
else if (a[i] == _max)
{
count++;
}
}
if ((count == 1) && (_max == k))
{
printf("%hd\n", index);
}
else
{
printf("No solution.\n");
}
}
return 0;
}
Problem E: 合并线段
Time Limit: 1000 ms Memory Limit: 128 MB
Description
类似于最长不下降子序列
#include <iostream>
#include <algorithm>
#define SIZE 15001
using namespace std;
struct node
{
int x, y, cnt, up, inc;
};
node a[SIZE];
bool r_cmp(node a, node b)
{
if (a.y != b.y)
{
return a.y > b.y;
}
else return a.x > b.x;
}
bool cmp(node a, node b)
{
if ((a.y - a.x) != (b.y - b.x))
{
return a.y - a.x > b.y - b.x;
}
else return a.cnt > b.cnt;
}
int main(int argc, char** argv)
{
int n, i, j;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
a[i].cnt = 1;
a[i].up = a[i].y;
a[i].inc = 0;
}
sort(a + 1, a + 1 + n, r_cmp);
for (i = 1; i <= n; i++)
{
a[i].y = a[i].up;
a[i].cnt += a[i].inc;
a[i].inc = 0;
for (j = i + 1; j <= n; j++)
{
if (a[j].y != a[i].x)
{
continue;
}
if ((a[j].up < a[i].y) || ((a[j].up == a[i].y) && (a[j].inc < a[i].cnt)))
{
a[j].up = a[i].y;
a[j].inc = a[i].cnt;
}
}
}
for (i = 1; i <= n; i++)
{
a[i].y = a[i].up;
a[i].cnt += a[i].inc;
a[i].inc = 0;
}
sort(a + 1, a + 1 + n, cmp);
cout << a[1].y - a[1].x << " " << a[1].cnt << endl;
return 0;
}
这样也行
#include <iostream>
#include <algorithm>
#define SIZE 15001
using namespace std;
struct node
{
int x, y, count;
node *next;
};
node a[SIZE];
node *head, *inp, *outp, *p, *tmp;
bool comp0(node a, node b)
{
if (a.y != b.y)
{
return a.y > b.y;
}
else return a.x > b.x;
}
bool comp(node a, node b)
{
if ((a.y - a.x) != (b.y - b.x))
{
return a.y - a.x > b.y - b.x;
}
else return a.count > b.count;
}
int main(int argc, char** argv)
{
int n, i, j;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> a[i].x >> a[i].y;
a[i].count = 1;
}
sort(a + 1, a + 1 + n, comp0);
for (i = 1; i < n; i++)
{
a[i].next = a + 1 + i;
}
a[n].next = NULL;
head = a + 1;
while (head != NULL)
{
inp = head;
outp = head;
p = outp->next;
while (p != NULL)
{
if (p->y == head->x)
{
p->y = head->y;
p->count += head->count;
if (inp != outp)
{
outp->next = p->next;
tmp = inp->next;
inp->next = p;
p->next = tmp;
inp = inp->next;
}
}
else
{
outp = outp->next;
}
p = outp->next;
}
head = head->next;
}
sort(a + 1, a + 1 + n, comp);
cout << a[1].y - a[1].x << " " << a[1].count << endl;
return 0;
}
暴力能得九十分
#include <iostream>
#include <algorithm>
#define SIZE 15001
using namespace std;
struct line
{
int s, e, count;
};
line a[SIZE];
bool comp(line a, line b)
{
if (a.e - a.s - b.e + b.s)
{
return ((a.e - a.s) > b.e - b.s);
}
return a.count > b.count;
}
int main(int argc, char** argv)
{
int n, i, j;
cin >> n;
for (i = 1; i <= n; i++)
{
cin >> a[i].s >> a[i].e;
a[i].count = 1;
}
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (a[i].e == a[j].s)
{
a[++n].e = a[j].e;
a[n].s = a[i].s;
a[n].count = a[i].count + a[j].count;
}
}
}
sort(a + 1, a + 1 + n, comp);
cout << a[1].e - a[1].s << " " << a[1].count << endl;
return 0;
}