您的位置:首页 > 其它

CSU-ACM2016暑假集训比赛3

2016-07-26 16:19 316 查看
A - A

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

HDU 1242

Description

Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.

Angel’s friends want to save Angel. Their task is: approach Angel. We assume that “approach Angel” is to get to the position where Angel stays. When there’s a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.

You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)

Input

First line contains two integers stand for N and M.

Then N lines follows, every line has M characters. “.” stands for road, “a” stands for Angel, and “r” stands for each of Angel’s friend.

Process to the end of the file.

Output

For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing “Poor ANGEL has to stay in the prison all his life.”

Sample Input

7 8

.#####.

.a#..r.

..#x…

..#..#.#

…##..

.#……

……..

Sample Output

13

每个图只有1个r

#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
using namespace std;
const int MAX=205;
int N,M,startN,startM,endN,endM;
const int dN[4]= {-1,0,0,1};
const int dM[4]= {0,1,-1,0};
char pic[MAX][MAX];
int ans[MAX][MAX];
struct node
{
int n,m,t;
node(int a,int b,int c):n(a),m(b),t(c) {};
};
bool check(int n,int m,int t)
{
if (n<0||n>=N||m<0||m>=M||pic
[m]=='#'||(ans
[m]<t&&ans
[m]!=-1))
return false;
return true;
}
int main()
{
while(cin>>N>>M)
{
memset(ans,-1,sizeof(ans));
for (int i=0; i<N; i++)
cin>>pic[i];
for (int i=0; i<N; i++)
for (int j=0; j<M; j++)
if (pic[i][j]=='r')
{
startN=i;
startM=j;
}
else if (pic[i][j]=='a')
{
endN=i;
endM=j;
}
queue<node> Q;
Q.push(node(startN,startM,0));
ans[startN][startM]=0;
while (!Q.empty())
{
node u=Q.front();
Q.pop();
int cn=u.n;
int cm=u.m;
int ct=u.t;
//cout<<cn<<" "<<cm<<" "<<ct<<endl;
if (ans[cn][cm]!=-1)
ans[cn][cm]=min(ans[cn][cm],ct);
for (int i=0; i<4; i++)
{
int nn=cn+dN[i];
int nm=cm+dM[i];
int nt=0;
if (pic[nn][nm]=='x')
nt=ct+2;
else
nt=ct+1;
if (check(nn,nm,nt))
{
ans[nn][nm]=nt;
Q.push(node(nn,nm,nt));
}
}
}
if (ans[endN][endM]==-1)
cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else
cout<<ans[endN][endM]<<endl;
}
return 0;
}


B - B

Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

CodeForces 681C

Description

Petya has recently learned data structure named “Binary heap”.

The heap he is now operating with allows the following operations:

put the given number into the heap;

get the value of the minimum element in the heap;

extract the minimum element from the heap;

Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.

In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format:

insertx — put the element with value x in the heap;

getMinx — the value of the minimum element contained in the heap was equal to x;

removeMin — the minimum element was extracted from the heap (only one instance, if there were many).

All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.

While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya’s log and used them to make paper boats.

Now Vova is worried, if he made Petya’s sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.

Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.

Input

The first line of the input contains the only integer n (1 ≤ n ≤ 100 000) — the number of the records left in Petya’s journal.

Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.

Output

The first line of the output should contain a single integer m — the minimum possible number of records in the modified sequence of operations.

Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.

Note that the input sequence of operations must be the subsequence of the output sequence.

It’s guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.

Sample Input

Input

2

insert 3

getMin 4

Output

4

insert 3

removeMin

insert 4

getMin 4

Input

4

insert 1

insert 1

removeMin

getMin 2

Output

6

insert 1

insert 1

removeMin

removeMin

insert 2

getMin 2

模拟+优先队列

没写出来 每次模拟的题都做不出..

C - C

Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

HDU 2614

Description

Zty is a man that always full of enthusiasm. He wants to solve every kind of difficulty ACM problem in the world. And he has a habit that he does not like to solve

a problem that is easy than problem he had solved. Now yifenfei give him n difficulty problems, and tell him their relative time to solve it after solving the other one.

You should help zty to find a order of solving problems to solve more difficulty problem.

You may sure zty first solve the problem 0 by costing 0 minute. Zty always choose cost more or equal time’s problem to solve.

Input

The input contains multiple test cases.

Each test case include, first one integer n ( 2< n < 15).express the number of problem.

Than n lines, each line include n integer Tij ( 0<=Tij<10), the i’s row and j’s col integer Tij express after solving the problem i, will cost Tij minute to solve the problem j.

Output

For each test case output the maximum number of problem zty can solved.

Sample Input

3

0 0 0

1 0 1

1 0 0

3

0 2 2

1 0 1

1 1 0

5

0 1 2 3 1

0 0 2 3 1

0 0 0 3 1

0 0 0 0 2

0 0 0 0 0

Sample Output

3

2

4

Hint

Hint: sample one, as we know zty always solve problem 0 by costing 0 minute. So after solving problem 0, he can choose problem 1 and problem 2, because T01 >=0 and T02>=0. But if zty chooses to solve problem 1, he can not solve problem 2, because T12 < T01. So zty can choose solve the problem 2 second, than solve the problem 1.

题有点难读 其实很水

#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
using namespace std;
const int MAX=17;
int T[MAX][MAX];
int solved[MAX];
int n,ans;
void dfs(int q,int num,int last)
{
ans=max(ans,num);
for (int j=0;j<n;j++)
{
if (!solved[j]&&T[q][j]>=last)
{
solved[j]=1;
dfs(j,num+1,T[q][j]);
solved[j]=0;
}
}
}
int main()
{
while (cin>>n)
{
ans=0;
memset(solved,0,sizeof(solved));
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
scanf("%d",&T[i][j]);
solved[0]=1;
dfs(0,1,0);
cout<<ans<<endl;
}
}


D - D

Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

CodeForces 687A

Description

Recently, Pari and Arya did some research about NP-Hard problems and they found the minimum vertex cover problem very interesting.

Suppose the graph G is given. Subset A of its vertices is called a vertex cover of this graph, if for each edge uv there is at least one endpoint of it in this set, i.e. or (or both).

Pari and Arya have won a great undirected graph as an award in a team contest. Now they have to split it in two parts, but both of them want their parts of the graph to be a vertex cover.

They have agreed to give you their graph and you need to find two disjoint subsets of its vertices A and B, such that both A and B are vertex cover or claim it’s impossible. Each vertex should be given to no more than one of the friends (or you can even keep it for yourself).

Input

The first line of the input contains two integers n and m (2 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of vertices and the number of edges in the prize graph, respectively.

Each of the next m lines contains a pair of integers ui and vi (1  ≤  ui,  vi  ≤  n), denoting an undirected edge between ui and vi. It’s guaranteed the graph won’t contain any self-loops or multiple edges.

Output

If it’s impossible to split the graph between Pari and Arya as they expect, print “-1” (without quotes).

If there are two disjoint sets of vertices, such that both sets are vertex cover, print their descriptions. Each description must contain two lines. The first line contains a single integer k denoting the number of vertices in that vertex cover, and the second line contains k integers — the indices of vertices. Note that because of m ≥ 1, vertex cover cannot be empty.

Sample Input

Input

4 2

1 2

2 3

Output

1

2

2

1 3

Input

3 3

1 2

2 3

1 3

Output

-1

二分图 染色法判断

#include<stdio.h>
#include<string>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
#include<sstream>
#include<set>
using namespace std;
const int maxn = 100000 + 10;
vector<int> g[maxn],v[2];
bool ok = true;
int color[maxn];
void dfs(int k,int c)
{
if(!ok)return;
if (color[k] != -1)
{
if (color[k] != c)ok=false;
return;
}
color[k] = c;
v[c].push_back(k);
int len = g[k].size();
for (int i = 0; i < len; ++i)dfs(g[k][i],c^1); // 0 -> 1,, 1 -> 0;
}
void print(vector<int> & t)
{
int len = t.size();
printf("%d\n",len);
for (int i = 0; i < len; ++i)
{
if (i)printf(" ");
printf("%d",t[i]);
}
printf("\n");
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for (int i = 0; i < m; ++i)
{
int u,w;
scanf("%d%d",&u,&w);
g[u].push_back(w);
g[w].push_back(u);
}
memset(color,-1,sizeof(color));
for (int i = 1; i <= n; ++i)
{
if (color[i] == -1)dfs(i,0);
}
if (!ok)printf("-1\n");
else
{
for (int i = 0; i < 2; ++i)print(v[i]);
}
return 0;
}


E - E

Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

HDU 1597

Description

假设:

S1 = 1

S2 = 12

S3 = 123

S4 = 1234

………

S9 = 123456789

S10 = 1234567891

S11 = 12345678912

…………

S18 = 123456789123456789

………………

现在我们把所有的串连接起来

S = 1121231234…….123456789123456789112345678912………

那么你能告诉我在S串中的第N个数字是多少吗?

Input

输入首先是一个数字K,代表有K次询问。

接下来的K行每行有一个整数N(1 <= N < 2^31)。

Output

对于每个N,输出S中第N个对应的数字.

Sample Input

6

1

2

3

4

5

10

Sample Output

1

1

2

1

2

4

水题

#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
using namespace std;
typedef long long ll;
int main()
{
ll N;
ll T;
ll i;
cin>>T;
while (T--)
{
cin>>N;
i=1;
while (N>i)
{
N-=i;
i++;
}
//cout<<N<<" "<<i<<endl;
if (N<10)
cout<<N<<endl;
else
{
N=N%9;
if (N==0)
N=9;
cout<<N<<endl;
}
}
}


F - F

Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u

Submit

Status

Practice

HDU 4355

Description

In the Dark forest, there is a Fairy kingdom where all the spirits will go together and Celebrate the harvest every year. But there is one thing you may not know that they hate walking so much that they would prefer to stay at home if they need to walk a long way.According to our observation,a spirit weighing W will increase its unhappyness for S 3*W units if it walks a distance of S kilometers.

Now give you every spirit’s weight and location,find the best place to celebrate the harvest which make the sum of unhappyness of every spirit the least.

Input

The first line of the input is the number T(T<=20), which is the number of cases followed. The first line of each case consists of one integer N(1<=N<=50000), indicating the number of spirits. Then comes N lines in the order that x [i]<=x [i+1] for all i(1<=i

#include<stdio.h>
#include<cstring>
#include<queue>
#include<algorithm>
#include<functional>
#include<vector>
#include<iomanip>
#include<math.h>
#include<iostream>
using namespace std;
const int MAX=50005;
const double MIN=0.00001;
double X[MAX],W[MAX];
int T,N;
double ABS(double x)
{
if (x<0)
return -x;
else
return x;
}
double F(double loca)
{
double ans=0;
double temp;
for (int i=0;i<N;i++)
{
temp=ABS(loca-X[i]);
ans+=temp*temp*temp*W[i];
}
return ans;
}
int main()
{
cout<<fixed<<setprecision(0);
cin>>T;
for (int a=1;a<=T;a++)
{
cin>>N;
for (int i=0;i<N;i++)
{
scanf("%lf%lf",&X[i],&W[i]);
}
double mid,midd,low,high;
low=X[0];
high=X[N-1];
while(low<high)
{
mid=(low+high)/2;
midd=(mid+high)/2;
if (F(mid)<F(midd))
high=midd-MIN;
else
low=mid+MIN;
}
cout<<"Case #"<<a<<": "<<F(low)<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm