很久没有更新blog了==
昨天做了cf,成绩不是很理想,这其中有外在因素,因为最近班里比较多活动,只好忍痛割爱搞其他DD。。。==|||
今天的这场cf比较水,很多大牛早早就AK了,tourist还是稳坐第一。。。orz!
ABCD都是水题,这里只贴代码。ps:这次不用standard input output,改用文件形式,搞得一开始返回Idleness limit exceeded on test 1这样奇怪的信息=。=
A
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 305
#define FRE freopen("input.txt","r",stdin)
int main(){freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
char str[10];
int n;
while(scanf("%s%d",str,&n)!=EOF){
if(str[0]=='f'){
if(n==1){
puts("L");
}
else
puts("R");
}
if(str[0]=='b'){
if(n==1){
puts("R");
}
else
puts("L");
}
}
return 0;
}
B
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 1005
#define FRE freopen("input.txt","r",stdin)
int a[N];
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i,j;
for(i=1;i<=n;i++)scanf("%d",&a[i]);
i=m;
while(1){
if(i==n+1)i=1;
if(a[i] == 1){
printf("%d\n",i);
break;
}
else
i++;
}
}
return 0;
}
C
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 104
#define FRE freopen("input.txt","r",stdin)
struct node{
int id;
int w;
};
priority_queue<node> pp;
int b[N];
bool operator<(node x,node y){
return x.w<y.w;
}
int main(){
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,k;
while(scanf("%d%d",&n,&k)!=EOF){
while(!pp.empty())pp.pop();
int i,j;
int sum = 0;
for(i=0;i<n;i++){
int a;
scanf("%d",&a);
sum += a;
node t;
t.w = a;
t.id = i;
b[i] = 0;
pp.push(t);
}
int ans = 0;
while(!pp.empty()){
node x = pp.top();
pp.pop();
if(b[x.id]<3){
if(x.w<k)continue;
else{
b[x.id]++;
x.w -= k;
ans += k;
pp.push(x);
}
}
}
printf("%d\n",sum-ans);
}
return 0;
}
D
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 54
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
int g[N][N];
int d[3];
int a[N];
int b[N];
int main(){
FRE;
FF;
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
int i,j;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(g,0,sizeof(g));
int sum = 0;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
scanf("%d",&g[i][j]);
sum += g[i][j];
a[i] += g[i][j];
}
}
// cout<<sum<<endl;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
b[i] += g[j][i];
}
}
// for(i=0;i<n;i++)cout<<a[i]<<" ";puts("");
// for(i=0;i<m;i++)cout<<b[i]<<" ";puts("");
scanf("%d%d%d",&d[0],&d[1],&d[2]);
sort(d,d+3);
int s[3];
s[0] = 0,s[1] = 0,s[2] = 0;
int ans = 0;
int x[3];
for(i=0;i<n-2;i++){
s[0] += a[i];
if(s[0]>d[2])break;
s[1] = 0;
for(j=i+1;j<n-1;j++){
s[1] += a[j];
s[2] = sum - s[0] - s[1];
x[0] = s[0],x[1] = s[1],x[2] = s[2];
sort(x,x+3);
if(x[0] == d[0] && x[1] == d[1] && x[2] == d[2])
ans++;
}
}
s[0] = 0,s[1] = 0,s[2] = 0;
for(i=0;i<m-2;i++){
s[0] += b[i];//cout<<s[0]<<" ";
if(s[0]>d[2])break;
s[1] = 0;
for(j=i+1;j<m-1;j++){
s[1] += b[j];
s[2] = sum - s[0] - s[1];
x[0] = s[0],x[1] = s[1],x[2] = s[2];
sort(x,x+3);
if(x[0] == d[0] && x[1] == d[1] && x[2] == d[2])
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}
E 博弈。当奇数时,先手必胜策略是先放中心点,然后中心对称着放。
偶数时,后手必胜策略是与先手中心对称着放。代码很短。
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 105
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
int main(){
FRE;
FF;
int n;
while(scanf("%d",&n)!=EOF){
while(n--){
int m;
scanf("%d",&m);
int i,j;
if(m&1)puts("0");
else
puts("1");
}
}
return 0;
}
F
问一棵树最多有几层,dfs即可
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 105
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
vector<int> v[N];
int du[N];
int maxm;
int vis[N];
void dfs(int u,int len){
int i,j;
int sz = v[u].size();
if(du[u] == 1 && !vis[u]){
vis[u] = 1;//cout<<len<<endl;
maxm = max(maxm,len);
return ;
}
vis[u] = 1;
for(i=0;i<sz;i++){
int x = v[u][i];
if(!vis[x])
dfs(x,len+1);
}
}
int main(){
FRE;
FF;
int n;
while(scanf("%d",&n)!=EOF){
int i,j;
int m;
for(i=0;i<=100;i++)v[i].clear();
int ans = 0;
while(n--){
scanf("%d",&m);
for(i=0;i<=100;i++){
v[i].clear();
du[i] = 0;
//vis[i] = 0;
}
for(i=1;i<m;i++){
int a,b;
scanf("%d%d",&a,&b);
v[a].push_back(b);
v[b].push_back(a);
du[a]++;
du[b]++;
}
maxm = 0;
for(j=1;j<=m;j++){
if(du[j] == 1){
memset(vis,0,sizeof(vis));
vis[j] = 1;
dfs(j,0);
//break;
}
}
ans += maxm;
}
printf("%d\n",ans);
}
return 0;
}
G
模拟,英文要过关,因为题目中貌似有些歧义(就我而言)。
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 105
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
int a[N][2];
int c[N];
int d[N][N];
string str[N];
vector<string> ans[N];
int main(){
FRE;
FF;
int n,t;
scanf("%d%d",&n,&t);
int i,j;
for(i = 0; i < n; i++){
int a1,b1,a2,b2;
scanf("%d%d%d%d",&a1,&b1,&a2,&b2);
a[i][0] = a1 + b2;
a[i][1] = a2 + b1;
}
int m;
scanf("%d",&m);
for(i = 0;i < m; i++){
cin>>str[i];
scanf("%d",&c[i]);
}
queue<int> qq;
for(i=0;i<m;i++)qq.push(i);
for(int who = 0,i = 0;!qq.empty(); ){
for(int tt = 0;!qq.empty(); ){
int x = qq.front();
qq.pop();
int tmp = max(1,c[x] - a[i][who] - d[i][x]);
if(tt+tmp > t){
qq.push(x);
d[i][x] += t - tt;
break;
}
else{
ans[i].push_back(str[x]);
tt += tmp;
}
if(tt == t)break;
}
i++;
if(i == n){
i = 0;
who ^= 1;
}
}
for(i=0;i<n;i++){
cout<<(int)ans[i].size();
for(j=0;j<ans[i].size();j++){
cout<<" "<<ans[i][j];
}
puts("");
}
return 0;
}
H
匹配。列出每个单词所有长度<=4的组合,然后匹配就ok。
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 205
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
map<string,int> mp;
string ss[80000];
int match[80000];
vector<int> v[N];
bool vis[80000];
int cnt;
int get(char *s){
if(mp.find(s) == mp.end()){
ss[cnt++] = s;
mp[s] = cnt - 1;
return cnt - 1;
}
return mp[s];
}
bool gao(int u){
int i,j;
for(i = 0; i<v[u].size(); i++){
int x = v[u][i];
if(!vis[x]){
vis[x] = 1;
if(match[x] == -1 || gao(match[x])){
match[x] = u;
return true;
}
}
}
return false;
}
int out[N];
int main(){
FRE;
FF;
int n;
scanf("%d",&n);
int a,b,c,d;
int i,j;
char s[5];
char str[15];
cnt = 0;
for(i=0;i<n;i++){
scanf("%s",str);
int len = strlen(str);
for(a = 0;a < len ;a++){
s[0] = str[a];s[1] = '\0';
v[i].push_back(get(s));
for(b = a+1; b < len; b++){
s[1] = str[b];s[2] = '\0';
v[i].push_back(get(s));
for(c = b+1; c < len; c++){
s[2] = str[c];s[3] = '\0';
v[i].push_back(get(s));
for(d = c+1; d < len; d++){
s[3] = str[d];s[4] = '\0';
v[i].push_back(get(s));
}
}
}
}
}
bool flag = 1;
memset(match,-1,sizeof(match));
for(i = 0; i < n; i++){
memset(vis,0,sizeof(vis));
if(!gao(i)){
flag = 0;
break;
}
}
if(flag){
for(i = 0; i < cnt; i++){
if(match[i] != -1){
int x = match[i];
out[x] = i;
}
}
for(i=0;i<n;i++)cout<<ss[out[i]]<<endl;
}
else
puts("-1");
return 0;
}
I
额。。。不会做>_<,没思路啊!!!
J
把所有点取绝对值,转化成求平面上最小点对,最后跟原数比较一下就知道是1、2、3、4哪种情况。注意先对点排序,否则会TLE
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define LL long long
#define MIN INT_MIN
#define MAX INT_MAX
#define PI acos(-1.0)
#define N 100005
#define FRE freopen("input.txt","r",stdin)
#define FF freopen("output.txt","w",stdout);
int sqr(int n){
return n*n;
}
int abs(int n){
return n > 0 ? n :-n;
}
struct node{
int x,y;
int id;
bool operator < (node b) const {
if(abs(x)!= abs(b.x))return abs(x) < abs(b.x);
return abs(y) < abs(b.y);
}
int getMod(int ca){
int ret;
if(x >= 0 && y >= 0)ret = 1;
else
if(x < 0 && y >= 0)ret = 2;
else
if(x > 0 && y < 0)ret = 3;
else
if(x < 0 && y < 0)ret = 4;
return (ca == 1) ? ret : (5 - ret);
}
}p[N];
int main(){
FRE;
FF;
int n;
while(scanf("%d",&n)!=EOF){
int i,j;
for(i=0; i<n; i++){
scanf("%d %d",&p[i].x,&p[i].y);
p[i].id = i+1;
}
sort(p,p+n);
int minm = MAX;
int a,b;
for(i=0;i<n;i++){
for(j = i+1; j < n; j++){
if(sqr(abs(p[i].x) - abs(p[j].x)) > minm )break;
int tmp = sqr(abs(p[i].x) - abs(p[j].x)) + sqr(abs(p[i].y) - abs(p[j].y));
if(tmp < minm ){
minm = tmp;
a = i;
b = j;
}
}
}
printf("%d %d %d %d\n",p[a].id,p[a].getMod(1),p[b].id,p[b].getMod(2));
}
return 0;
}