//贪心+DP F. Wi-Fi Codeforces Round #587 (Div. 3)

CF

F. Wi-Fi
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You work as a system administrator in a dormitory, which has n
rooms one after another along a straight hallway. Rooms are numbered from 1 to n

.

You have to connect all n

rooms to the Internet.

You can connect each room to the Internet directly, the cost of such connection for the i
-th room is i

coins.

Some rooms also have a spot for a router. The cost of placing a router in the i
-th room is also i coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room i, you connect all rooms with the numbers from max(1, i−k) to min(n, i+k) inclusive to the Internet, where k is the range of router. The value of k

is the same for all routers.

Calculate the minimum total cost of connecting all n

rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have.
Input

The first line of the input contains two integers n
and k (1≤n,k≤2⋅105

) — the number of rooms and the range of each router.

The second line of the input contains one string s
of length n, consisting only of zeros and ones. If the i-th character of the string equals to '1' then there is a spot for a router in the i-th room. If the i-th character of the string equals to '0' then you cannot place a router in the i

-th room.
Output

Print one integer — the minimum total cost of connecting all n

rooms to the Internet.
Examples
Input
Copy

5 2
00100

Output
Copy

3

Input
Copy

6 1
000000

Output
Copy

21

Input
Copy

4 1
0011

Output
Copy

4

Input
Copy

12 6
000010000100

Output
Copy

15

Note

In the first example it is enough to place the router in the room 3
, then all rooms will be connected to the Internet. The total cost of connection is 3

.

In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is 1+2+3+4+5+6=21

.

In the third example you need to connect the room 1
directly and place the router in the room 3. Thus, the total cost of connection of all rooms is 1+3=4

.

In the fourth example you need to place routers in rooms 5
and 10. Then all rooms will be connected to the Internet. The total cost of connection is 5+10=15

.


  • 先放ACcode ~
  • 题解在下

    #include
    using namespace std;
    typedef long long ll;
    char s[200010];
    int f[200010];
    ll dp[200010];
    int main()
    {
    	int n,k;scanf("%d%d",&n,&k);
    	scanf("%s",s+1);f[n+1]=2*n+k;
    	for(int i=n;i>=1;i--) f[i]=s[i]=='1'?i:f[i+1];
    	for(int i=1;i<=n;i++)
    	{
    		dp[i]=dp[i-1]+i;
    		int q=f[max(1,i-k)];
    		if(q<=i+k)
    		{
    			dp[i]=min(dp[i],dp[max(1,q-k)-1]+q);
    		}
    	} 
    	printf("%lld\n",dp[n]);
    }
  • f[i]表示 与i最近的右端(更正了QAQ)的路由器 2*n+k 表示不存在这个路由器
  • 首先这里的dp [i] 表示前面的1—i 都连接好所需要的最少花费
  • 对于每个点连接的情况有两种:
  • 这个点直接连接互联网 开销就是 dp[i-1]+i
  • 这个点连接路由器 要保证dp最小那么也就是路由器的序号最小(f[i-k]就是这个最小的路由器的编号 ,你可以想象 ~对于f[i] ,i越小,f[i]越小,而且f[i-k-1]这个路由器有可能就是它自己 那么就可能无法被i连接 【f[i-k-1]>=i-k-1 ;f[i-k]>=i-k;很显然后者才是我们需要的】)如果这个最小序号的路由器大于i+k 那么就说明还是无法连接 那么就可以抛弃了这种情况了
  • 看代码应该好懂哒~~~

你可能感兴趣的:(DP,codeforce)