Gym 100694 解题报告

来源:互联网 发布:软件接口测试方案 编辑:程序博客网 时间:2024/06/16 16:45

B

Far Manager

B. Far Manager

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49742

Input/Output: standard input/output

Pavel loves Far Manager very much. By default, Far shows files in a certain number of columns each of which holds p files. Besides, there is also a «zero file» — the link to the parent directory, after which the files in the current directory are shown.

Pavel is viewing a directory with n files numbered from 1 to n. He needs to select the file x. To do that, he has to place the cursor onto this file. He can press any of the four arrow keys: up / down arrows move the cursor by 1 file, and left / right arrows move it byp files (or place the cursor onto the «zero file» or the last file if less than p files remains in the cursor's movement direction).

Your task is to help Pavel to determine the minimum number of key presses to select the file x. At the beginning the cursor points to the link to the parent directory.

Input

The only line contains 3 integers pn and x (1 ≤ p ≤ 1091 ≤ n ≤ 1091 ≤ x ≤ n) — the maximum number of files in a column, the total number of files in the directory viewed by Pavel and the file he wants to select.

Output

Write one integer — the minimum number of key presses Pavel needs to select the file x.

Sample Input

Input
5 8 3
Output
3
Input
6 20 14
Output
4
Input
8 33 33
Output
5

/*    题意:你有n个文件编号分别是1-n,刚开始光标在0的位置,你可以向左或者向右移动光标,          你可以一次移动1个单位,也可以一次移动p个单位,但是跳跃的位置不能超过n,          问你移动到x文件的最小的移动次数是多少    类型:水题    分析:1.一次跳一格一定能到达文件的位置;          2.每次跳p格,当光标处于x的左右两边时,再使用一次跳一格,更新最小值;          3.跳到边界n,再往回跳的情况          更新最小值,输出答案即可*/#include<cstdio>#include<iostream>#include<algorithm>using namespace std;int main(){    int p,n,x;    while(~scanf("%d%d%d",&p,&n,&x)){        int ans=x;            //一次跳一格        ans=min(ans,x/p+x%p); //每次跳p格,当光标处于x的左边时        ans=min(ans,x/p*p+p-x+x/p+1); //每次跳p格,当光标处于x的右边时        int t1=n/p+(n%p==0?0:1);      //跳跃到边界的次数        ans=min(ans,t1+n-x);          //从边界每次一格往回走        ans=min(ans,t1+(n-x)/p+(n-(n-x)/p*p-x));  //从边界每次往回跳p格,到x右边        int t2=(n-x)/p+((n-x)%p==0?0:1);  //从边界跳跃到x左边的次数        ans=min(ans,t1+t2+x-(n-t2*p));    //从边界每次往回跳p格,到x左边        printf("%d\n",ans);    }    return 0;}


E

SuperHyperMarket

E. SuperHyperMarket

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49745

Input/Output: standard input/output

The day of opening of the greatest shop in the world is coming. To show the full potential of their shop, in particular, the unbelievable speed of the cash desks, the owners of the shop came up with the following move: n most average customers will be let into the shop, and the cash desks will be opened only after all customers will choose their purchases and take their places in the queues. The problem is that if the customers walk along the cash desks and choose the optimal queue, the process can last for several hours. That's why you are asked to choose queues for all customers automatically.

The sociologists told you how the average customers choose the queue: they look at the number of people in the queue and at the number of the purchases of the last two people in it. Formally, the customer chooses the cash desk i for which the value xi = ci·p is minimal. Here, ci is the number of people in the i-th queue, and p — is the average (by the average customer's definition) number of purchases of one person in the queue. The value of p is calculated by two last people in the queue: p = (p1 + p2) / 2, if there are two or more people in the queue (p1 and p2 are the numbers of purchases of the last two people), p = p1, if there is only one customer in the queue, and p = 0 if the queue is empty. If there are several cash desks with minimal xi, the customer chooses the one with the minimal number i.

After the customers choose their queue, they don't go anywhere out of it.

Input

The first line contains two space-separated integers n and k (1 ≤ n, k ≤ 105) — the number of customers and the number of cash desks in the shop.

The next line contains n space-separated integers pj (1 ≤ pj ≤ 1000) — the numbers of purchases of the j-th customer in the chronological order. Every customer chooses his queue before the next customer starts doing it.

Output

Write n space-separated integers in one line. The i-th number should be equal to the number of cash desk where the i-th customer will go using the criterion described in the problem.

Sample Input

Input
5 2
1 3 1 2 3
Output
1 2 1 1 2 
Input
5 7
1 3 1 2 3
Output
1 2 3 4 5 

/*    题意:有n个顾客排队买单,k个收银台,每个顾客选择收银台的规则是:所有"收银台最后两个顾客购买东西数量          的平均值乘以该收银台的顾客总数"的最小值,给你每个顾客购买东西的数量,输出每个顾客选择的收银台          的编号    类型:优先队列    分析:维护最后两个人的购买数量和每个收银台的值,每次从优先队列弹出最小值即可*/#include<cstdio>#include<iostream>#include<queue>#include<algorithm>using namespace std;typedef long long ll;struct cashDesk{    double sum,one,tow;    int id,num;    friend bool operator < (cashDesk a, cashDesk b)    {        if(a.sum==b.sum)return a.id>b.id;        return a.sum>b.sum;    }};priority_queue<cashDesk>que;int main(){    int n,m;    double k;    cashDesk tmp;    cin>>n>>m;    for(int i=1;i<=m;i++){        tmp.id=i;        tmp.sum=0;        tmp.num=0;        que.push(tmp);    }    for(int i=1;i<=n;i++){        scanf("%lf",&k);        tmp=que.top();        que.pop();        printf("%d%c",tmp.id,i==n?'\n':' ');        if(tmp.num==0){            tmp.sum=k;            tmp.one=k;            tmp.num=1;        }        else if(tmp.num==1){            tmp.sum=k+tmp.one;            tmp.tow=k;            tmp.num=2;        }        else if(tmp.num>=2){            tmp.num++;            tmp.sum=(k+tmp.tow)*1.0/2*tmp.num;            tmp.tow=k;        }        que.push(tmp);    }    return 0;}


G

The Lost Graph


G. The Lost Graph

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49747

Input/Output: standard input/output

Student Vladislav is taking programming exam. He hasn't prepared properly and had a bad luck to get a question about some complicated graph algorithm that will be definitely useless for him for the rest of his life. He quickly asked his groupmate a cheatsheet and discovered the following pseudocode there:


function dfs(v)
begin
visited[v] := true
print("in " + v)
for k := 1 : n do
begin
if adjacent(v, k) and not visited[k] do
begin
dfs(k)
end
end
print("out " + v)
end

Vladislav manually executed the algorithm on the connected undirected graph just drawn by him. Unfortunately, he was very nervous and ate the piece of paper where this graph was drawn, but he has to show the graph to his teacher. Help Vladislav to restore the graph. It must be done as soon as possible, so the restored graph should contain the minimal possible number of edges.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of vertices in the graph.

It's followed by the execution result of the algorithm completely useless for Vladislav. It consists of 2n lines «in x» or «out x» wherex is the number of vertex (1 ≤ x ≤ n).

It's guaranteed that all vertices from 1 to n exist in the graph, and that the input really specifies the execution result of the algorithm on some existing graph.

Output

Output (n - 1) lines, two numbers on each line — the numbers of vertices connected by edges. You can output the pairs of vertices and the vertices in pairs in any order.

Sample Input

Input
2
in 1
in 2
out 2
out 1
Output
1 2
Input
4
in 4
in 2
out 2
in 3
in 1
out 1
out 3
out 4
Output
1 3
2 4
3 4


/*    题意:有n个点和2n行in/out串,根据图中伪代码,输出图的连线关系    类型:栈    分析:直接模拟即可*/#include<cstdio>#include<iostream>#include<stack>#include<queue>#include<algorithm>using namespace std;typedef long long ll;char s[5];int d;int main(){    int n;cin>>n;    stack <int>t;    for(int i=0;i<2*n;i++){        scanf("%s%d",s,&d);        if(s[0]=='i'){            t.push(d);        }        else{            if(!t.empty()){                int u=t.top();                t.pop();                if(!t.empty())                    printf("%d %d\n",u,t.top());            }        }    }    return 0;}


H

Noisy Lecture

H. Noisy Lecture

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49748

Input/Output: standard input/output

Teacher N/A is delivering a lecture. The lecture consists of n sentences and goes this way: each sentence should be firstly remembered by the teacher from her copy-book (remembering the i-th sentence takes ai time) and then written on the blackboard (it takes bi time). If there is a noise in the lecture hall, N/A gets distracted: if she is remembering the sentence at this moment, she forgots it and will have to remember it from the beginning after the noise ends. And if she is writing the sentence on the blackboard, she will have to remember the remaining part of the sentence (she has to spend the corresponding part of ai for that) and write that part on the blackboard.

There are k students in the lecture hall. Each of these students can initiate the noise of duration cj exactly once. What is the maximal amount of time students can slow down the lecture?

Input

The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of sentences in the lecture.

The second line contains n space-separated integers ai (1 ≤ ai ≤ 1000) — how much time it takes to remember the i-th sentence.

The third line contains n space-separated integers bi (1 ≤ bi ≤ 1000) — how much time it takes to write the i-th sentence on the blackboard.

The fourth line contains a single integer k (1 ≤ k ≤ 1000) — the number of students in the lecture hall.

The fifth line contains k space-separated integers cj (1 ≤ cj ≤ 1000) — the duration of noise which can be initiated by the j-th student.

Output

Output one integer — the maximal amount of time for which the lecture can be slowed down.

Sample Input

Input
3
3 2 1
1 5 3
2
1 2
Output
9
Input
4
2 1 2 1
1 2 3 4
3
2 3 1
Output
12

/*    题意:给你n个句子,以及每个句子需要记忆的时间和每个句子需要抄写的时间,          然后有k个学生,每个学生可以打断老师正在进行的操作,并且使老师不能工作bi时间,          当老师在记忆句子的时候被打断需要重新记忆这个句子;当老师在抄写句子的时候被打断,          老师需要重新记忆当前抄写的句子,不过不用重头开始抄这个句子,只需抄剩下部分的句子即可,          问你学生最多能拖延老师的课堂多少时间    类型:水题    分析:题意理解清楚,就可以随便切了.要求拖延最多的时间,只要每个学生在老师记忆完最长的句子的时          候进行打断即可*/#include<cstdio>#include<iostream>#include<stack>#include<queue>#include<algorithm>using namespace std;typedef long long ll;int main(){    int n,k,Max=-0x7fffffff,t,res=0;    cin>>n;    for(int i=0;i<n;i++){        scanf("%d",&t);        if(t>Max)Max=t;    }    for(int i=0;i<n;i++){        scanf("%d",&t);    }    cin>>k;    for(int i=0;i<k;i++){        scanf("%d",&t);        res+=t;    }    cout<<res+Max*k<<endl;    return 0;}


I

Goat in the Field

I. Goat in the Field

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49749

Input/Output: standard input/output

Goat Tanya accidentally got out of the garden and found herself in the field. The goat got confused since she haven't been here before so she decided to walk straight in the certain direction. The field consists of the infinite number of 1 × 1 cells, and each of these cells is connected with four other cells by their sides. In a single turn the goat moves to the adjacent cell in her movement direction.

Shepherds have noticed the disappearence and try to catch the goat. Every shepherd in his turn can move in any direction by one or two cells, but he can't change the direction during the turn. They also can just stand in their cells instead of moving. The goat is catched if some shepherd at the end of his turn is located in the goat's cell. At the beginning no shepherd is located in the same cell as the goat.

Tanya and shepherds move in rotation — the goat moves first, and then the shepherds move in the same order as in the input. Your task is to find which shepherd will be the first to catch the goat.

Input

The first line contains two integers x, y (|x|, |y| ≤ 1000) — the initial coordinates of the goat.

The second line contains a string s which determines the goat's movement direction. s can be one of the four following strings (the quotes are for clarity):

  • «LEFT» — the goat moves to the left, x coordinate decreases by 1,
  • «RIGHT» — the goat moves to the right, x coordinate increases by 1,
  • «UP» — the goat moves to the top, y coordinate increases by 1,
  • «DOWN» — the goat moves to the bottom, y coordinate decreases by 1.

The third line contains one integer n (1 ≤ n ≤ 1000) — the number of shepherds.

The i-th of the n following lines contains a string  () of lowercase Latin letters, and two integers xi, yi (|xi|, |yi| ≤ 1000) — the name of the i-th shepherd and his initial coordinates. It's guaranteed that all names are distinct.

Output

Output a single string — the name of the shepherd which will catch the goat first.

Sample Input

Input
0 0
LEFT
3
andrew -10 0
denis 10 0
ilia 0 10
Output
andrew
Input
10 2
UP
3
danila 5 4
sashac 11 1
sashab 12 10
Output
sashac
Input
0 0
UP
2
mike 2 1
constantine 0 1
Output
mike

/*    题意:有一只羊迷路了,现在给你羊的坐标,以及羊移动的方向,羊每回合能移动1个单位长度,          有n个人去抓羊,给你每个人的名字和初始坐标,人每回合能移动1、2单位长度,但是不能边移动边转向,          羊和人轮流进行各自的回合,羊先执行回合,问你谁最先抓到羊    类型:模拟    分析:分情况讨论,模拟即可*/#include<cstdio>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;char s[30],who[30],flag[30];int main(){    int x,y;    scanf("%d%d%s",&x,&y,flag);    int m,ans=0x7fffffff;    cin>>m;    while(m--){        int xx,yy;        scanf("%s%d%d",s,&xx,&yy);        int t;        if(flag[0]=='U'){            int t1=(abs(x-xx)+1)/2;            int newy=y+t1;            if(newy>yy) t=(newy-yy)+t1;            else t=(yy-newy+2)/3+t1;        }        else if(flag[0]=='D'){            int t1=(abs(x-xx)+1)/2;            int newy=y-t1;            if(newy<yy) t=(yy-newy)+t1;            else t=(newy-yy+2)/3+t1;        }        else if(flag[0]=='L'){            int t1=(abs(y-yy)+1)/2;            int newx=x-t1;            if(xx>newx) t=(xx-newx)+t1;            else t=(newx-xx+2)/3+t1;        }        else{            int t1=(abs(y-yy)+1)/2;            int newx=x+t1;            if(newx>xx) t=(newx-xx)+t1;            else t=(xx-newx+2)/3+t1;        }        if(t<ans) ans=t,strcpy(who,s);    }    printf("%s\n",who);    return 0;}


J

Ticket Booking

J. Ticket Booking

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49750

Input/Output: standard input/output

The biggest student ACM (Anarchy Code Modification) contest will be held in N-sk city soon. Slava is the leader of the group of students who will take part in this competition, so he must buy the train tickets for all members of the group. Each student told him his favourite seat number where this student wants to travel (all numbers are distinct).

Ticket window workers have gone on strike, so Slava has to use the terminal. The process of buying tickets in the terminal goes this way:

  1. The personal data of the passengers is entered. The maximum number of people whose information can be entered at one terminal usage is k.
  2. The lower and upper bounds of the seat numbers are entered. This segment can contain the seats that have been already bought. First c free seats in this segment are booked, where c is the number of people whose information has been filled at the previous step. If the segment doesn't contain enough free seats, the booking isn't performed.

Every terminal usage is accompanied by the entering of the great amount of information, so Slava wants to minimize the number of usages. He can't determine the optimal order by himself — the last bus to his home is about to leave — so you should help him.

Input

The first line contains three space-separated integers nm and k (1 ≤ n, m, k ≤ 105n ≤ m) — the number of students in the group, the number of free seats and the maximal number of people at one terminal usage.

The second line contains n distinct integers wi (1 ≤ wi ≤ 109) — the seat number preferred by the i-th student. They are sorted in ascending order.

The third line contains m distinct integers fj (1 ≤ fj ≤ 109) — the numbers of free seats. They are also sorted in ascending order.

It's guaranteed that every student wants to travel at a free seat. That is, for each 1 ≤ i ≤ n there exists such 1 ≤ j ≤ m that wi = fj.

Output

In the first line write a single integer s — the minimal number of terminal usages.

In each of the next s lines write the information about terminal usages — the number of people and their numbers, separated by spaces. Each student should be presented in only one terminal usage. The tickets should be bought for all students in the group.

Sample Input

Input
4 6 2
1 4 5 6
1 2 4 5 6 8
Output
3
1 1
2 2 3
1 4
Input
12 21 4
2 6 8 10 12 28 40 44 46 48 50 52
2 4 6 8 10 12 24 26 28 30 32 33 34 35 36 40 44 46 48 50 52
Output
5
1 1
4 2 3 4 5
1 6
4 7 8 9 10
2 11 12

/*    题意:有n个人,m个座位,每次在购票系统最大可以连续购票k张,          给你n个人要买的座位,和购票系统中m张票,问你最少几次购买可以          买好全部人票,输出怎么买    类型:模拟    分析:题目说排好序了,所以可以用二分得到位置,模拟一下就好了*/#include<cstdio>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;int t,n,m,k;int a[100010],b[100010];vector<int>vt[100010];int main(){    scanf("%d%d%d",&n,&m,&k);    for(int i=0;i<n;i++)        scanf("%d",&a[i]);    for(int i=0;i<m;i++)        scanf("%d",&b[i]);    int ans=0;    int pre=lower_bound(b,b+m,a[0])-b;    vt[0].push_back(1);    int num=1;    for(int i=1;i<n;i++){        int x=lower_bound(b,b+m,a[i])-b;        if(x!=pre+1||num==k){            num=1;            ans++;        }        else num++;        pre=x;        vt[ans].push_back(i+1);    }    printf("%d\n",ans+1);    for(int i=0;i<=ans;i++){        int l=vt[i].size();        printf("%d",l);        for(int j=0;j<l;j++)            printf(" %d",vt[i][j]);        printf("\n");    }    return 0;}



M
The Fifth Season

M. The Fifth Season

Time Limit: 2000ms
Memory Limit: 262144KB
64-bit integer IO format: %I64d      Java class name: (Any)
Submit Status PID: 49753

Input/Output: standard input/output

Cersei and Jaime has stolen n gold coins from the Casterly Rock reserves. They decided to play a game while the disappearence is not detected.

The game consists of q rounds. At the beginning of each round all n coins lie in a heap. The players move in rotation, taking from the heap no less than ki and no more than li coins (ki ≤ lii is the number of round). The player who can't do the move loses.

Cersei and Jaime have determined the pairs of numbers (ki, li) for each round of the game. They don't see any point in finding out who wins in what round as they choose who moves the first by flipping a coin. They are interested in the maximal length of each round, provided that they play optimally (intend to win). The length of the round is the total number of moves made by both Cersei and Jaime.

Your task is to find the maximal possible length of each round of the game.

Input

The first line contains two integers n and q — the number of coins and the number of rounds (1 ≤ n ≤ 106, 1 ≤ q ≤ 105).

Each of the next q lines contains two integers ki and li   — the minimal and maximal number of coins that could be taken at one turn in the i-th round.

Output

Write q space-separated integers. The i-th number should be equal to the maximal possible length of the i-th round.

Sample Input

Input
10 2
1 1
5 6
Output
10 1 

/*    题意:有n枚硬币,两个人轮流抓硬币,谁没得抓谁就输,两人都想赢          给你q次询问,每次询问给你a,b,表示最少抓a枚硬币,最多抓b枚硬币          问你两人一共能抓多少次    类型:博弈    分析:1. n%(a+b)>=a时 ans = 2 * (n/(a+b));          2. n%(a+b)< a时 ans = 2 * (n/(a+b)) + 1;*/#include<cstdio>#include<iostream>#include<stack>#include<queue>#include<algorithm>#include<cstring>using namespace std;typedef long long ll;int main(){    int a,b,n,q,ans;    cin>>n>>q;    for(int i=0;i<q;i++){        scanf("%d%d",&a,&b);        if(n%(a+b)<a) ans = 2 * (n/(a+b));        else ans = 2 * (n/(a+b)) + 1;        printf("%d%c",ans,i==q-1?'\n':' ');    }    return 0;}











1 0
原创粉丝点击