http://codeforces.com/contest/1106
A. Lunar New Year and Cross Counting:
在n*n的矩阵里找一个固定的图案,n很小,暴力就行。
#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const int N = 500+2;
char mp[N][N];
int n;
bool checkx(int x,int y)
{
if(x<0||x>=n||y<0||y>=n) return false;
if(mp[x][y]=='X') return true;
return false;
}
int main()
{
int num=0;
cin>>n;
for(int i=0;i<n;i++)
cin>>mp[i];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++){
if(checkx(i,j)&&checkx(i-1,j-1)&&checkx(i-1,j+1)&&checkx(i+1,j+1)&&checkx(i+1,j-1))
num++;
}
cout<<num;
}
B. Lunar New Year and Food Ordering
模拟。
#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const LL N = 2e6+2;
LL to[N];
LL val[N];
LL num[N];
LL k[N],y[N];
bool cmp(LL x,LL y)
{
if(val[x]!=val[y])
return val[x]<val[y];
return x<y;
}
int main()
{
LL n,m;
scanf("%lld %lld",&n,&m);
for(LL i=1;i<=n;i++){
scanf("%lld",&num[i]);
to[i]=i;
}
for(LL i=1;i<=n;i++) scanf("%lld",&val[i]);
for(LL i=1;i<=m;i++){
scanf("%lld%lld",&k[i],&y[i]);
}
sort(to+1,to+1+n,cmp);
LL now=1;
for(LL i=1;i<=m;i++){
LL cost=0;
if(num[k[i]]>=y[i]){
num[k[i]]-=y[i];
cost=y[i]*val[k[i]];
y[i]=0;
}else{
cost=num[k[i]]*val[k[i]];
y[i]-=num[k[i]];
num[k[i]]=0;
}
while(y[i]&&now<=n){
if(num[to[now]]>=y[i]){
num[to[now]]-=y[i];
cost+=y[i]*val[to[now]];
y[i]=0;
}else{
cost+=num[to[now]]*val[to[now]];
y[i]-=num[to[now]];
num[to[now]]=0;
now++;
}
}
if(now>n) printf("0\n");
else
printf("%lld\n",cost);
}
}
C. Lunar New Year and Number Division:
把一些数组合起来。每个【组合数】的权值为他的平方。
组合数最少2个。
因为x^2随着x增大越来越大。
所以排序,最大和最小组合。
D. Lunar New Year and a Wander:
给你一张图,问你从1开始,经过的点形成的序列字典序最小是多少。可以重复走。
那么就很简单。把1能走到的点加入优先队列。
然后取top,把top能走到的点加入。重复即可。
#include<iostream>
#include<vector>
#include<string.h>
#include<unordered_set>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
const LL N = 1e5+2;
int flag[N];
vector<int>G[N];
vector<int> ans;
priority_queue<int,vector<int>,greater<int> > q;
int n,m;
void haha(){
q.push(1);
flag[1]=1;
while(!q.empty()){
int u = q.top();
q.pop();
ans.push_back(u);
for(int i=0;i<G[u].size();i++){
int v=G[u][i];
if(flag[v]==0){
q.push(v);
flag[v]=1;
}
}
}
for(int i=0;i<n;i++){
printf("%d%c",ans[i],i==n-1?'\n':' ');
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
haha();
}