T1:买铅笔
水题!
#include<cstdio>
#include<cmath>
//#define DEBUG
int n;
int a1, a2, a3;
int b1, b2, b3;
int t1, t2, t3;
int main(){
#ifndef DEBUG
freopen("pencil.in", "r", stdin);
freopen("pencil.out", "w", stdout);
#endif
scanf("%d", &n);
scanf("%d%d%d%d%d%d", &a1, &b1, &a2, &b2, &a3, &b3);
t1 = floor((double)(n) / a1 + 0.9999) * b1;
t2 = floor((double)(n) / a2 + 0.9999) * b2;
t3 = floor((double)(n) / a3 + 0.9999) * b3;
if(t1 < t2){
if(t1 < t3)printf("%d\n", t1);
else printf("%d\n", t3);
}else{
if(t2 < t3)printf("%d\n", t2);
else printf("%d\n", t3);
}
#ifndef DEBUG
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
第二题:回文日期
又一水题!但很坑。
我这代码有漏洞。但全A。
#include<cstdio>
//#define DEBUG
int y1, m1, d1;
int y2, m2, d2;
int total = 0;
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int gays[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool isSYear(int y){
return (y%400==0)||((y%4==0)&&(y%100!=0));
}
inline bool isOK(int y, int m, int d){
if(!(m>=1 && m<=12))return false;
if(isSYear(y)){
if(d>=0 && d<=gays[m])return true;
return false;
}else{
if(d>=0 && d<=days[m])return true;
return false;
}
}
int main(){
#ifndef DEBUG
freopen("date.in", "r", stdin);
freopen("date.out", "w", stdout);
#endif
scanf("%4d%2d%2d", &y1, &m1, &d1);
scanf("%4d%2d%2d", &y2, &m2, &d2);
for(int y = y1;y <= y2;y++){
char buf[5], src[5];
sprintf(src, "%d", y);
for(int i = 3;i >= 0;i--)buf[i] = src[3-i];
buf[4] = '\0';
int m, d;
sscanf(buf, "%2d%2d", &m, &d);
if(isOK(y, m, d))total++;
}
printf("%d\n", total);
#ifndef DEBUG
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
第三题:港口
难度一下就来了!但注意:暴力的话70分稳拿。
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<set>
using namespace std;
//#define DEBUG
#define MAXN 100000
int n, x;
int k[MAXN], t[MAXN], next[MAXN];
vector<int> from[MAXN];
set<int> conty;
int main(){
#ifndef DEBUG
freopen("port.in", "r", stdin);
freopen("port.out", "w", stdout);
#endif
memset(next, 0, sizeof(next));
scanf("%d", &n);
for(int i = 0;i < n;i++){
scanf("%d%d", &t[i], &k[i]);
for(int j = i;j >= 0;j--){
if(t[i]-86400 < t[j])next[i] = j;
else break;
}
for(int j = 0;j < k[i];j++){
scanf("%d", &x);
from[i].push_back(x);
}
}
for(int i = 0;i < n;i++){
conty.clear();
for(int j = i;j >= next[i];j--){
for(int k = 0;k < from[j].size();k++){
conty.insert(from[j][k]);
}
}
printf("%d\n", conty.size());
}
#ifndef DEBUG
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
正解是用队列。
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int n, s = 0;
struct node{
int pos, val;
node(int p, int v){
this->pos = p;
this->val = v;
}
};
int country[100005];
queue<node> Q;
int main(){
memset(country, 0, sizeof(country));
scanf("%d", &n);
for(int i = 0;i < n;i++){
int t, k, x;
scanf("%d%d", &t, &k);
for(int j = 0;j < k;j++){
scanf("%d", &x);
Q.push(node(t, x));
if(country[x] == 0)s++;
country[x]++;
}
for(;;){
node now = Q.front();
if(now.pos <= t - 86400){
country[now.val]--;
if(country[now.val] == 0)s--;
Q.pop();
}else break;
}
printf("%d\n", s);
}
return 0;
}
第四题:魔法阵
这™是什么破题!这除了暴搜还有其他方法吗?在考试时我没想出来。
暴搜45
#include<cstdio>
#include<cstring>
#include<algorithm>
//#define DEBUG
#define minutes(a, b) (a-b)
#define MAXN 40000
#define MAXM 15000
int n;
struct node{
int val, pos;
}a[MAXN];
int A[MAXN], B[MAXN], C[MAXN], D[MAXN];
inline bool isMatix(int a, int b, int c, int d){
if(a==b || b==c || c==d)return false;
if(minutes(b,a) != 2 * minutes(d,c))return false;
if((double)(minutes(b,a)) >= (double)(minutes(c,b)) / 3)return false;
return true;
}
inline int cmp(node a, node b){return a.val < b.val;}
int main(){
#ifndef DEBUG
freopen("magic.in", "r", stdin);
freopen("magic.out", "w", stdout);
#endif
scanf("%*d%d", &n);
for(int i = 0;i < n;i++){
scanf("%d", &a[i].val);
a[i].pos = i;
}
memset(A, 0, sizeof(A));
memset(B, 0, sizeof(B));
memset(C, 0, sizeof(C));
memset(D, 0, sizeof(D));
std::sort(a, a + n, cmp);
for(int i = 0;i < n;i++)
for(int j = i + 1;j < n;j++)
for(int k = j + 1;k < n;k++)
for(int h = k + 1;h < n;h++)
if(isMatix(a[i].val, a[j].val, a[k].val, a[h].val)){
A[a[i].pos]++;
B[a[j].pos]++;
C[a[k].pos]++;
D[a[h].pos]++;
}
for(int i = 0;i < n;i++)printf("%d %d %d %d\n", A[i], B[i], C[i], D[i]);
#ifndef DEBUG
fclose(stdin);
fclose(stdout);
#endif
return 0;
}