BestCoder Round #42(3/4)

Shaking hands

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 169    Accepted Submission(s): 147


Problem Description
Today is Gorwin’s birthday, so she holds a party and invites her friends to participate. She will invite n friends, for convenience, Gorwin numbers them from 1 to n. Some of them have known each other, But some of them have not. Those friends whose have known each other will shake hands with each other, and drink one cup of champagne. Gorwin wants to know how many cups of champagne she should prepare. Can you help her?
 

Input
Multiple test cases (about 30), the first line of each case contains an integer n which indicates Gorwin will invite n friends to her party. 

Next n lines will give a n*n matrix, if a[i][j] is 1, then friend i and friend j have known each other, otherwise they have not known each other.

Please process to the end of file.

[Technical Specification]

All input entries are integers.

1<=n<=30

0<=a[i][j]<=1

a[i][i]=0;

a[i][j]=a[j][i] for i!=j
 

Output
For each case, output an integer which denotes total cups of champagne Gorwin should prepare in a single line.
 

Sample Input
    
    
2 0 0 0 0 3 0 0 1 0 0 0 1 0 0
 

Sample Output
    
    
4 8
Hint
For the second case, Gorwin will shake hands with all her friends, then Gorwin drink three cups of champagne, each friends drink one cup. Friend 1 and friend 3 know each other,every of them drinks one cup again. So the total cups is 3+3+2=8.
 

Source
/* ***********************************************
Author :
Created Time :2015/5/23 18:55:44
File Name :1.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const
double eps=1e-5;
using namespace
std;

bool
cmp(int a,int b){
return
a>b;
}

int
a[33][33];
int
main()
{

#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n;
while
(cin>>n){
int
num=0;
for
(int i=1;i<=n;i++)
for
(int j=1;j<=n;j++)
cin>>a[i][j];
for
(int i=1;i<=n;i++)
for
(int j=1;j<=i;j++){
if
(a[i][j]==1)
num++;
}

cout<<n*2+num*2<<endl;
}

return
0;
}

Gunner II

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 917    Accepted Submission(s): 372


Problem Description
Long long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There are n birds and n trees. The i-th bird stands on the top of the i-th tree. The trees stand in straight line from left to the right. Every tree has its height. Jack stands on the left side of the left most tree. When Jack shots a bullet in height H to the right, the nearest bird which stands in the tree with height H will falls.

Jack will shot many times, he wants to know which bird will fall during each shot.
 

Input
There are multiple test cases (about 5), every case gives n, m in the first line, n indicates there are n trees and n birds, m means Jack will shot m times. 

In the second line, there are n numbers h[1],h[2],h[3],…,h[n] which describes the height of the trees.

In the third line, there are m numbers q[1],q[2],q[3],…,q[m] which describes the height of the Jack’s shots.

Please process to the end of file.

[Technical Specification]

All input items are integers.

1<=n,m<=100000(10^5)

1<=h[i],q[i]<=1000000000(10^9)
 

Output
For each q[i], output an integer in a single line indicates the id of bird Jack shots down. If Jack can’t shot any bird, just output -1.

The id starts from 1.
 

Sample Input
    
    
5 5 1 2 3 4 1 1 3 1 4 2
 

Sample Output
    
    
1 3 5 4 2
Hint
Huge input, fast IO is recommended.
 

Source
 
题目大意:给定n棵树的高度,给定m个射击高度,每次射击输出对应的树的最小的下标。
该开始用队列模拟的,G++ MLE了,C++AC
/* ***********************************************
Author :
Created Time :2015/5/23 19:32:56
File Name :1.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const
double eps=1e-5;
using namespace
std;

bool
cmp(int a,int b){
return
a>b;
}


map<int,queue<int> >mp;
int
main()
{

#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,m;
int
a;
while
(cin>>n>>m){
mp.clear();
for
(int i=1;i<=n;i++){
scanf("%d",&a);
mp[a].push(i);
}

for
(int i=1;i<=m;i++){
scanf("%d",&a);
if
(mp[a].empty())printf("-1\n");
else
printf("%d\n",mp[a].front()),mp[a].pop();
}
}

return
0;
}
下面的是按照标程写的离散化+vector,其中离散化使用map做的。注意vector的erase函数是o(n)的时间复杂度的,所以用erase会超时,这里的删除操作用的数组模拟
/* ***********************************************
Author :
Created Time :2015/5/25 13:50:31
File Name :1.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 100000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const
double eps=1e-5;
using namespace
std;

vector<int>v[maxn];

map<int,int>mp;
int
mark[maxn];
int
main()
{

#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,m;
while
(cin>>n>>m){
mp.clear();
int
a;
int
cnt=0;
for
(int i=0;i<n;i++){
scanf("%d",&a);
if
(!mp[a]){
mp[a]=++cnt;//注意cnt最好不用0 会有冲突
v[mp[a]].clear();
v[mp[a]].push_back(i+1);
mark[mp[a]]=0;
}

else
v[mp[a]].push_back(i+1);
}

for
(int i=0;i<m;i++){
scanf("%d",&a);
if
(!mp[a]||v[mp[a]].size()<=mark[mp[a]])puts("-1");
else
{
printf("%d\n",v[mp[a]][mark[mp[a]]++]);
//v[mp[a]].erase(v[mp[a]].begin());
}
}
}

return
0;
}

Happy birthday

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 296    Accepted Submission(s): 153


Problem Description
Today is Gorwin’s birthday. So her mother want to realize her a wish. Gorwin says that she wants to eat many cakes. Thus, her mother takes her to a cake garden. 

The garden is splited into n*m grids. In each grids, there is a cake. The weight of cake in the i-th row j-th column is  {w_{ij}} kilos, Gorwin starts from the top-left(1,1) grid of the garden and walk to the bottom-right(n,m) grid. In each step Gorwin can go to right or down, i.e when Gorwin stands in (i,j), then she can go to (i+1,j) or (i,j+1) (However, she can not go out of the garden). 

When Gorwin reachs a grid, she can eat up the cake in that grid or just leave it alone. However she can’t eat part of the cake. But Gorwin’s belly is not very large, so she can eat at most K kilos cake. Now, Gorwin has stood in the top-left grid and look at the map of the garden, she want to find a route which can lead her to eat most cake. But the map is so complicated. So she wants you to help her.
 

Input
Multiple test cases (about 15), every case gives n, m, K in a single line.

In the next n lines, the i-th line contains m integers  {w_{i1}},{w_{i{\rm{2}}}},{w_{i3}}, \cdots {w_{im}} which describes the weight of cakes in the i-th row

Please process to the end of file.

[Technical Specification]

All inputs are integers.

1<=n,m,K<=100

1<= {w_{ij}}<=100
 

Output
For each case, output an integer in an single line indicates the maximum weight of cake Gorwin can eat.
 

Sample Input
    
    
1 1 2 3 2 3 100 1 2 3 4 5 6
 

Sample Output
    
    
0 16
Hint
In the first case, Gorwin can’t eat part of cake, so she can’t eat any cake. In the second case, Gorwin walks though below route (1,1)->(2,1)->(2,2)->(2,3). When she passes a grid, she eats up the cake in that grid. Thus the total amount cake she eats is 1+4+5+6=16.
 

Source
 
题目大意转换:从1,1点走到n,m点,每个点(x,y)有一个wi价值,求在总价值不超过k时能得到的最大价值。
思路:对于某个点,我们可以选择要和不要,也就是dfs(x,y,v+a[i][j])和dfs(x,y,v)  并用dp[x][y][v]记录这个状态是否被搜索过。
代码如下:
/* ***********************************************
Author :
Created Time :2015/5/25 16:28:12
File Name :1.cpp
************************************************ */

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 1<<30
#define maxn 10000+10
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << 61;
const
double eps=1e-5;
using namespace
std;

bool
cmp(int a,int b){
return
a>b;
}

int
a[110][110];
int
dir[2][2]={1,0,0,1};
int
dp[110][110][102];//状态
int n,m,k;
int
Max;
void
dfs(int x,int y,int v){
if
(x==n&&m==y){
Max=max(v,Max);
}

if
(dp[x][y][v])return ;
dp[x][y][v]=1;

for
(int i=0;i<2;i++){
int
nx=x+dir[i][0];
int
ny=y+dir[i][1];
if
(nx<=n&&nx>=1&&ny<=m&&ny>=1){
if
(v+a[nx][ny]<=k)dfs(nx,ny,v+a[nx][ny]);
dfs(nx,ny,v);
}
}
}

int
main()
{

#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
while(cin>>n>>m>>k){
for
(int i=1;i<=n;i++)
for
(int j=1;j<=m;j++)
cin>>a[i][j];
cle(dp);
Max=0;
dfs(1,1,0);
if
(a[1][1]<=k)
dfs(1,1,a[1][1]);
cout<<Max<<endl;
}

return
0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值