Codeforces Round #417 (Div. 2)Sagheer, the Hausmeister 乱搞 Sagheer and Nubian Market 二分

来源:互联网 发布:哈工大 ltp 源码 编辑:程序博客网 时间:2024/05/29 18:35

B. Sagheer, the Hausmeister
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Some people leave the lights at their workplaces on when they leave that is a waste of resources. As a hausmeister of DHBW, Sagheer waits till all students and professors leave the university building, then goes and turns all the lights off.

The building consists of n floors with stairs at the left and the right sides. Each floor has m rooms on the same line with a corridor that connects the left and right stairs passing by all the rooms. In other words, the building can be represented as a rectangle with n rows and m + 2 columns, where the first and the last columns represent the stairs, and the m columns in the middle represent rooms.

Sagheer is standing at the ground floor at the left stairs. He wants to turn all the lights off in such a way that he will not go upstairs until all lights in the floor he is standing at are off. Of course, Sagheer must visit a room to turn the light there off. It takes one minute for Sagheer to go to the next floor using stairs or to move from the current room/stairs to a neighboring room/stairs on the same floor. It takes no time for him to switch the light off in the room he is currently standing in. Help Sagheer find the minimum total time to turn off all the lights.

Note that Sagheer does not have to go back to his starting position, and he does not have to visit rooms where the light is already switched off.

Input

The first line contains two integers n and m (1 ≤ n ≤ 15 and 1 ≤ m ≤ 100) — the number of floors and the number of rooms in each floor, respectively.

The next n lines contains the building description. Each line contains a binary string of length m + 2 representing a floor (the left stairs, then m rooms, then the right stairs) where 0 indicates that the light is off and 1 indicates that the light is on. The floors are listed from top to bottom, so that the last line represents the ground floor.

The first and last characters of each string represent the left and the right stairs, respectively, so they are always 0.

Output

Print a single integer — the minimum total time needed to turn off all the lights.

Examples
input
2 200100100
output
5
input
3 4001000000010000010
output
12
input
4 301110011100111001110
output
18
Note

In the first example, Sagheer will go to room 1 in the ground floor, then he will go to room 2 in the second floor using the left or right stairs.

In the second example, he will go to the fourth room in the ground floor, use right stairs, go to the fourth room in the second floor, use right stairs again, then go to the second room in the last floor.

In the third example, he will walk through the whole corridor alternating between the left and right stairs at each floor.

题意:一栋楼关灯,nm,n是多少层,m是一层多少间屋,其中给n行数据,每行数据m+2个点,1代表灯亮着,0代表灯灭着,其中第一个和最后一个点是楼梯,起点在左下角,走一个格时间是1,只能通过楼梯上楼,并且必须关掉当前层的所有灯之后才能上楼,问关掉所有的灯需要的最少时间

每一层的左楼梯和右楼梯都有两个选择策略,左楼梯可以是下面层的右楼梯走过来的,也可以是下面层的左楼梯先去关灯在走回来的,同理右楼梯。

在特殊考虑一下细节。

#include<bits/stdc++.h>#define eps 1e-9#define PI 3.141592653589793#define bs 1000000007#define bsize 256#define MEM(a) memset(a,0,sizeof(a))typedef long long ll;using namespace std;int dp[30][3];char ch[30][200];int n,m;int main(){int i,j;cin>>n>>m;int maxx=1;for(i=1;i<=n;i++){scanf("%s",ch[i]+1);}for(i=1;i<=n;i++){int flog=0;for(j=1;j<=m+2;j++){if(ch[i][j]=='1'){flog=1;break;}}if(!flog){maxx=i+1;}elsebreak;}if(maxx>n){cout<<0<<endl;return 0;}int le,ri=1;for(i=1;i<=m+2;i++){if(ch[n][i]=='1'){ri=i;}}if(maxx==n){cout<<ri-1<<endl;return 0;}dp[n][1]=ri*2-2;dp[n][2]=m+1;for(i=n-1;i>=maxx+1;i--){le=m+2,ri=1;for(j=1;j<=m+2;j++){if(ch[i][j]=='1'){le=min(le,j);ri=max(ri,j);}}dp[i][1]=min(dp[i+1][1]+ri*2-1,dp[i+1][2]+m+2);        dp[i][2]=min(dp[i+1][2]+(m+3-le)*2-1,dp[i+1][1]+m+2);} le=100000,ri=-1;for(i=1;i<=m+2;i++){if(ch[maxx][i]=='1'){le=min(le,i);ri=max(ri,i);}}cout<<min(dp[maxx+1][1]+ri,dp[maxx+1][2]+m+2-le+1)<<endl;return 0; }


C. Sagheer and Nubian Market
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

On his trip to Luxor and Aswan, Sagheer went to a Nubian market to buy some souvenirs for his friends and relatives. The market has some strange rules. It contains n different items numbered from 1 to n. The i-th item has base cost ai Egyptian pounds. If Sagheer buysk items with indices x1, x2, ..., xk, then the cost of item xj is axj + xj·k for 1 ≤ j ≤ k. In other words, the cost of an item is equal to its base cost in addition to its index multiplied by the factor k.

Sagheer wants to buy as many souvenirs as possible without paying more than S Egyptian pounds. Note that he cannot buy a souvenir more than once. If there are many ways to maximize the number of souvenirs, he will choose the way that will minimize the total cost. Can you help him with this task?

Input

The first line contains two integers n and S (1 ≤ n ≤ 105 and 1 ≤ S ≤ 109) — the number of souvenirs in the market and Sagheer's budget.

The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 105) — the base costs of the souvenirs.

Output

On a single line, print two integers kT — the maximum number of souvenirs Sagheer can buy and the minimum total cost to buy thesek souvenirs.

Examples
input
3 112 3 5
output
2 11
input
4 1001 2 5 6
output
4 54
input
1 77
output
0 0
Note

In the first example, he cannot take the three items because they will cost him [5, 9, 14] with total cost 28. If he decides to take only two items, then the costs will be [4, 7, 11]. So he can afford the first and second items.

In the second example, he can buy all items as they will cost him [5, 10, 17, 22].

In the third example, there is only one souvenir in the market which will cost him 8 pounds, so he cannot buy it.

二分,C比B好搞多了。。。

#include<bits/stdc++.h>#define eps 1e-9#define PI 3.141592653589793#define bs 1000000007#define bsize 256#define MEM(a) memset(a,0,sizeof(a))typedef long long ll;using namespace std;struct node{long long int a,index;}num[100005];long long n,S;long long b[100005];int cmp(long long u,long long v){return u<v;}int check(long long x){int i;for(i=1;i<=n;i++)b[i]=num[i].a+num[i].index*x;sort(b+1,b+1+n,cmp);long long now=0;for(i=1;i<=x;i++){now+=b[i];if(now>S)return 0;}if(now>S)return 0;return 1;}int main(){long long i;cin>>n>>S;for(i=1;i<=n;i++)scanf("%lld",&num[i].a),num[i].index=i;long long ans,le=0,ri=n,mid;while(le<=ri){mid=(le+ri)>>1;if(check(mid)){ans=mid;le=mid+1;}else{ri=mid-1;}}cout<<ans;long long ans_s=0;for(i=1;i<=n;i++)b[i]=num[i].a+num[i].index*ans;sort(b+1,b+1+n,cmp);for(i=1;i<=ans;i++){ans_s+=b[i];}cout<<" "<<ans_s<<endl; }




阅读全文
0 0