您的位置:首页 > 其它

2016弱校连萌十一专场10.3 部分题解

2016-10-07 18:57 351 查看
【比赛题目链接】https://www.bnuoj.com/v3/contest_show.php?cid=8504#info

【A】找两个数乘积是连续上升并且最大的。模拟即可。

【代码君】

//
//Created by just_sort 2016/10/3
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;

const int maxn = 1010;
int a[maxn];

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
int ans = -1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j < i; j++){
int tmp = a[i]*a[j];
int p = tmp%10; tmp /= 10;
while(tmp)
{
int q = tmp%10;
if(q + 1 != p) break;
tmp /= 10;
p = q;
}
if(tmp == 0) ans = max(ans,a[i]*a[j]);
}
}
printf("%d\n",ans);
}
return 0;
}
【B】从终点开始bfs,记录各点到终点最短路。

【代码君】

//
//Created by just_sort 2016/10/3
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 220;
const int inf = 0x3f3f3f3f;
int dir[4][2] = {{0,1},{0,-1},{-1,0},{1,0}};
struct node{int x,y;};
queue<node>q;
int dp[maxn][maxn];
char G[maxn][maxn];

int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int ans1,ans2;
for(int i = 0; i < n; i++) scanf("%s",G[i]);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(G[i][j] == '%'){
q.push(node{i,j});
}
}
}
ans1 = ans2 = inf;
memset(dp,-1,sizeof(dp));
while(q.size())
{
node now = q.front();
q.pop();
if(G[now.x][now.y] == '$') ans1 = min(ans1,dp[now.x][now.y]);
if(G[now.x][now.y] == '@') ans2 = min(ans2,dp[now.x][now.y]);
for(int i = 0; i < 4; i++)
{
int dx = now.x + dir[i][0];
int dy = now.y + dir[i][1];
if(dx >= 0 && dx < n && dy >= 0 && dy < m && G[dx][dy] != '#' && dp[dx][dy] == -1){
dp[dx][dy] = dp[now.x][now.y] + 1;
q.push(node{dx,dy});
}
}
}
if(ans2 < ans1) printf("Yes\n");
else printf("No\n");
}
return 0;
}


【C】n对括号最多需要交换n*(n+1)/2次()))...(((类似这种摆放情况),那么找最短的满足m*(m+1)/2>=n的,然后把额外需要交换的交换完毕就是结果

【代码君】

//
//Created by just_sort 2016/10/3
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int m = 1;
while(m * (m + 1) / 2 < n) m++;
string str;
str = string(m,')')+string(m,'(');
int now = m;
for(int i = 0; i < m * (m + 1) / 2 - n; i++){
swap(str[now-1],str[now]);
now--;
}
cout<<str<<endl;
}
return 0;
}


【D】ACM协会中有 n 个人, 每个人有个 motivation level , 将每个人按照
motivation level 从大到小排序, 相同的按照入会时间从小到大排序. 协会里面排名在前 20 的会努力工作, 剩下80 不工作. 现在有
m 个事件, 加入一个新的成员或者一个成员离开. 每次事件后输出工作状态变化的人. 保证同时只有一个人状态变化.

【解题方法】set大模拟,开两个集合,一个是 s1 装的是 20 的人的信息,另一个集合 s2 装的是剩下的信息,注意开一个结构体,这个结构体中也包括时间,然后s1 集合中是按照 那个
motivation level 从小到大排序的,然后 s2 是motivation level 从大到小排序的,然后就进行模拟就行了。

【代码君】

//
//Created by just_sort 2016/10/7
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5+6;
struct node{
string s;
int time;
int d;
};
bool cmp(node a,node b)
{
if(a.d == b.d) return a.time > b.time;
else return a.d>b.d;
}
struct cmp1{//从大到小排序
bool operator()(const node &a,const node &b)const{
if(a.d == b.d) return a.time > b.time;
else return a.d > b.d;
}
};
struct cmp2{//从小到大排序
bool operator()(const node &a,const node &b)const{
if(a.d == b.d) return a.time < b.time;
else return a.d < b.d;
}
};
set<node,cmp2>s1;
set<node,cmp1>s2;
map<string,node>mp;
node a[maxn];
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF)
{
s1.clear();
s2.clear();
mp.clear();
double tmp = 1.0*n*0.2;
int x = floor(tmp);
node xx;
for(int i = 1; i <= n; i++){
cin>>a[i].s>>a[i].d;
a[i].time = i;
mp[a[i].s] = a[i];
}
int curn = n;
sort(a+1,a+n+1,cmp);
for(int i = 1; i <= x; i++){
s1.insert(a[i]);
}
for(int i = x + 1; i <= n; i++){
s2.insert(a[i]);
}
cin>>m;
string s;
for(int i = curn+1; i <= curn + m; i++){
char op[5];
scanf("%s",op);
if(op[0] == '-')//'-'
{
cin>>s;
xx = mp[s];
if(s1.erase(xx)) x--;
s2.erase(xx);
if(x > (int)(1.0*(n-1)*0.2))
{
x--;
xx = *s1.begin();
s1.erase(xx);
s2.insert(xx);
cout<<xx.s;
printf(" is not working now.\n");
}
n--;
if(x < (int)(1.0*(n)*0.2))
{
x++;
xx = *s2.begin();
s1.insert(xx);
s2.erase(xx);
cout<<xx.s;
printf(" is working hard now.\n");
}
}
else if(op[0] == '+') //'+'
{
cin>>a[i].s>>a[i].d;
a[i].time = i;
mp[a[i].s] = a[i];
if(x < (int)(1.0*(n+1)*0.2))
{
if(a[i].d > (*s2.begin()).d || (a[i].d == (*s2.begin()).d && a[i].time > (*s2.begin()).time))
{
s1.insert(a[i]);
cout<<a[i].s;
printf(" is working hard now.\n");
}
else
{
xx = *s2.begin();
s2.erase(xx);
s1.insert(xx);
s2.insert(a[i]);
cout<<a[i].s;
printf(" is not working now.\n");
cout<<xx.s;
printf(" is working hard now.\n");
}
x++;
}
else
{
if(x != 0)
{
xx = *s1.begin();
if(a[i].d > xx.d || (a[i].d == xx.d && a[i].time > xx.time))
{
s1.erase(xx);
s1.insert(a[i]);
s2.insert(xx);
cout<<a[i].s;
printf(" is working hard now.\n");
cout<<xx.s;
printf(" is not working now.\n");
}
else{
s2.insert(a[i]);
cout<<a[i].s;
printf(" is not working now.\n");
}
}
else{
xx = *s2.begin();
if((int)(1.0*(n+1)*0.2) > 0)
{
if(a[i].d > xx.d || (a[i].d == xx.d && a[i].time > xx.time))
{
s1.insert(a[i]);
cout<<a[i].s;
printf(" is working hard now.\n");
}
else{
s2.erase(xx);
s2.insert(a[i]);
s1.insert(xx);
cout<<a[i].s;
printf(" is not working now.\n");
cout<<xx.s;
printf(" is working hard now.\n");
}
}
else{
s2.insert(a[i]);
cout<<a[i].s;
printf(" is not working now.\n");
}
}
}
n++;
}
}
}
return 0;
}


【E】贴个题解http://www.cnblogs.com/chen9510/p/5929542.html

【代码君】

//
//Created by just_sort 2016/10/3
//Copyright (c) 2016 just_sort.All Rights Reserved
//
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 111111 + 10;
int n;
vector<int> g[maxn];
map<ull, int> mp;
ull dfs(int u, int f, int p) {
ull res = 0;
for (int i = 0; i < g[u].size(); ++i) {
int v = g[u][i];
res += dfs(v, u, p);
}
res = res * p + 1;
mp[res]++;
return res;
}

int main() {
scanf("%d", &n);
for (int i = 0; i < n - 1; ++i) {
int u, v;
scanf("%d%d", &u, &v);
g[u].push_back(v);
}
mp.clear();
dfs(1, 0, 1e9 + 7);
ll ans = 0;
map<ull, int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++) {
ll x = it->second;
ans += x * (x - 1) / 2;
}
printf("%lld\n", ans);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: