比赛链接:http://codeforces.com/contest/1106
A:数据不大,暴力遍历一遍就行
#include<bits/stdc++.h>
using namespace std;
string str[505];
int n;
int access(int x,int y)
{
if((y+1)<n && (y+1)>=0 && (y-1)<n && (y-1)>=0&&(x+1)<n && (x+1)>=0 && (x-1)<n && (x-1)>=0&&x < n&&x>=0&&y<n && y>=0)
return 1;
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
int i,ans, j;
while(cin >> n)
{
for(i = 0; i < n; i++)
cin >> str[i];
ans = 0;
if(n<3)
{
cout << "0" << endl;
continue;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n; j++)
{
if(access(i,j))
if(str[i][j]=='X' && str[i-1][j-1] == 'X' && str[i-1][j+1]=='X'&& str[i+1][j-1]=='X'&& str[i+1][j+1]=='X')
ans++;
}
}
cout << ans << endl;
}
return 0;
}
B :直接模拟就行
不过每次找最便宜的菜进行补充时,不能每次都从头找起,不然会超时
#include<bits/stdc++.h>
using namespace std;
struct node
{
long long int num, value;
};
node mp[100005];
struct nn
{
int index;
long long int value;
}stand[100005];
bool cmp(nn a, nn b)
{
if(a.value == b.value)
return a.index < b.index;
return a.value < b.value;
}
int main()
{
// std::ios::sync_with_stdio(false);
int n, m, i, j, flag;
long long int ans,x,y;
node temp;
while(~scanf("%d %d", &n,&m))
{
memset(stand, 0, sizeof(stand));
memset(mp, 0, sizeof(mp));
for(i = 1; i <= n; i++)
{
scanf("%I64d", &mp[i].num);
}
for(i = 1; i <= n; i++)
{
scanf("%I64d", &mp[i].value);
stand[i].index = i;
stand[i].value = mp[i].value;
}
sort(stand+1, stand+n+1, cmp);
j = 1;//这里,j一开始为1 后面就直接++就行while循环不能一直从1开始找
for(i = 1; i <= m; i++)
{
flag = 0;
ans = 0;
scanf("%I64d %I64d", &x,&y) ;
if(mp[x].num>=y)
{
ans += y * mp[x].value;
mp[x].num -= y;
flag = 1;
y = 0;
}
else
{
ans += mp[x].value * mp[x].num;
y-=mp[x].num;
mp[x].num = 0;
while(y && j <= n)
{
if(!mp[stand[j].index].num)
j++;
if(mp[stand[j].index].num>=y)
{
ans += y*mp[stand[j].index].value;
mp[stand[j].index].num-=y;
y = 0;
flag = 1;
}
else
{
ans += mp[stand[j].index].num*mp[stand[j].index].value;
y-=mp[stand[j].index].num;
mp[stand[j].index].num = 0;j++;
}
}
}
if(flag)
printf("%I64d\n", ans);
else printf("0\n");
}
}
return 0;
}
C:平方和最小,就要使每个数尽可能小,平方之后就会小,所以排序一下,最大的和最小的相加就行
#include<bits/stdc++.h>
using namespace std;
long long int mp[300005];
bool cmp(int a, int b)
{
return a < b;
}
int main()
{
std::ios::sync_with_stdio(false);
int n,i;
long long int ans;
while(cin >> n)
{
ans = 0;
for(i = 0; i < n; i++)
cin >> mp[i];
sort(mp, mp+n, cmp);
for(i = 0; i < n / 2; i++)
{
ans += (mp[i]+mp[n-i-1])*(mp[i]+mp[n-i-1]);
}
cout << ans << endl;
}
return 0;
}
D: 根据所给的无向图,给出字典序最小的路径
用优先队列模拟
#include<bits/stdc++.h>
using namespace std;
vector<int>mp[100005], ans;
int num[100005], n, m, i, j, vis[100005];
void toop()
{
priority_queue<int,vector<int>, greater<int> >q;
q.push(1);
vis[1] = 1;
while(!q.empty())
{//cout << "qqq" << endl;
int ii = q.top();
q.pop();
ans.push_back(ii);
for(j = 0; j < mp[ii].size();j++)
{//cout << j << " llll" << endl;
if(!vis[mp[ii][j]])
{
q.push(mp[ii][j]);
vis[mp[ii][j]] = 1;
}
}
}
}
int main()
{
std::ios::sync_with_stdio(false);
// int n,m;
int u,v;
while(cin >> n >> m)
{
for(i = 0; i <= n; i++)
mp[i].clear();
ans.clear();
memset(num, 0, sizeof(num));
memset(vis, 0, sizeof(vis));
for(i = 0; i < m; i++)
{
cin >> u >> v;
mp[u].push_back(v);
mp[v].push_back(u);
}
toop();
cout << ans[0];
for(i = 1; i < ans.size(); i++)
cout << " " << ans[i];
cout << endl;
}
return 0;
}