Codeforces Round #229 (Div. 2)(C,E待續)

u010660276發表於2014-02-17
A. Inna and Alarm Clock

Inna loves sleeping very much, so she needs n alarm clocks in total to wake up. Let's suppose that Inna's room is a100 × 100 square with the lower left corner at point(0, 0) and with the upper right corner at point (100, 100). Then the alarm clocks are points with integer coordinates in this square.

The morning has come. All n alarm clocks in Inna's room are ringing, so Inna wants to turn them off. For that Inna has come up with an amusing game:

  • First Inna chooses a type of segments that she will use throughout the game. The segments can be either vertical or horizontal.
  • Then Inna makes multiple moves. In a single move, Inna can paint a segment of any length on the plane, she chooses its type at the beginning of the game (either vertical or horizontal), then all alarm clocks that are on this segment switch off. The game ends when all the alarm clocks are switched off.

Inna is very sleepy, so she wants to get through the alarm clocks as soon as possible. Help her, find the minimum number of moves in the game that she needs to turn off all the alarm clocks!

Input

The first line of the input contains integer n(1 ≤ n ≤ 105) — the number of the alarm clocks. The nextn lines describe the clocks: the i-th line contains two integersxi,yi — the coordinates of thei-th alarm clock(0 ≤ xi, yi ≤ 100).

Note that a single point in the room can contain any number of alarm clocks and the alarm clocks can lie on the sides of the square that represents the room.

Output

In a single line print a single integer — the minimum number of segments Inna will have to draw if she acts optimally.

Sample test(s)
Input
4
0 0
0 1
0 2
1 0
Output
2
Input
4
0 0
0 1
1 0
1 1
Output
2
Input
4
1 1
1 2
2 3
3 3
Output
3
Note

In the first sample, Inna first chooses type "vertical segments", and then she makes segments with ends at :(0, 0),(0, 2); and, for example,(1, 0),(1, 1). If she paints horizontal segments, she will need at least 3 segments.

In the third sample it is important to note that Inna doesn't have the right to change the type of the segments during the game. That's why she will need 3 horizontal or 3 vertical segments to end the game.

用set儲存有多少個不同的列和行,輸出小的。

#include<iostream>
#include<cstdio>
#include<map>
#include<set>
using namespace std;
int main()
{
    int n,x,y;
    set<int> r,l;
    cin>>n;
    while(n--)
    {
        cin>>x>>y;
        r.insert(x);
        l.insert(y);
    }
    cout<<((r.size()>l.size())?l.size():r.size())<<endl;
    return 0;
}

B. Inna, Dima and Song

Inna is a great piano player and Dima is a modest guitar player. Dima has recently written a song and they want to play it together. Of course, Sereja wants to listen to the song very much.

A song is a sequence of notes. Dima and Inna want to play each note at the same time. At that, they can play thei-th note at volumev (1 ≤ v ≤ ai;v is an integer) both on the piano and the guitar. They should retain harmony, so the total volume with which thei-th note was played on the guitar and the piano must equalbi. If Dima and Inna cannot play a note by the described rules, they skip it and Sereja's joy drops by 1. But if Inna and Dima play thei-th note at volumesxi andyi(xi + yi = bi) correspondingly, Sereja's joy rises byxi·yi.

Sereja has just returned home from the university and his current joy is 0. Help Dima and Inna play the song so as to maximize Sereja's total joy after listening to the whole song!

Input

The first line of the input contains integer n(1 ≤ n ≤ 105) — the number of notes in the song. The second line containsn integers ai(1 ≤ ai ≤ 106). The third line containsn integers bi(1 ≤ bi ≤ 106).

Output

In a single line print an integer — the maximum possible joy Sereja feels after he listens to a song.

Sample test(s)
Input
3
1 1 2
2 2 3
Output
4
Input
1
2
5
Output
-1
Note

In the first sample, Dima and Inna play the first two notes at volume 1 (1 + 1 = 2, the condition holds), they should play the last note at volumes1 and2. Sereja's total joy equals:1·1 + 1·1 + 1·2 = 4.

In the second sample, there is no such pair (x, y), that1 ≤ x, y ≤ 2,x + y = 5, so Dima and Inna skip a note. Sereja's total joy equals -1

題意:一首音樂有n段,每一段用兩種樂器演奏,要求兩種樂器音量都小於ai,和正好等於bi,使得它們的乘積最大,joy加上他們的乘積,入股不滿足要求,joy-1

思路:兩個數相差越小乘積越大,所以只要滿足要求下,joy+(bi/2)*(bi-bi/2);

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=100010;
typedef long long LL;
int D[maxn];
int main()
{
    LL n,ans=0,x;
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>D[i];
    for(int i=0;i<n;i++)
    {
        cin>>x;
        if(x<=1||2*D[i]<x){ans--;continue;}
        ans+=(x/2)*(x-x/2);
    }
    cout<<ans<<endl;
    return 0;
}

C. Inna and Candy Boxes

Inna loves sweets very much. She has n closed present boxes lines up in a row in front of her. Each of these boxes contains either a candy (Dima's work) or nothing (Sereja's work). Let's assume that the boxes are numbered from 1 to n, from left to right.

As the boxes are closed, Inna doesn't know which boxes contain candies and which boxes contain nothing. Inna chose numberk and askedw questions to Dima to find that out. Each question is characterised by two integersli, ri (1 ≤ li ≤ ri ≤ n;r - l + 1 is divisible byk), the i-th question is: "Dima, is that true that among the boxes with numbers fromli tori, inclusive, the candies lieonly in boxes with numbersli + k - 1,li + 2k - 1,li + 3k - 1, ...,ri?"

Dima hates to say "no" to Inna. That's why he wonders, what number of actions he will have to make for each question to make the answer to the question positive. In one action, Dima can either secretly take the candy from any box or put a candy to any box (Dima has infinitely many candies). Help Dima count the number of actions for each Inna's question.

Please note that Dima doesn't change the array during Inna's questions. That's why when you calculate the number of operations for the current question, please assume that the sequence of boxes didn't change.

Input

The first line of the input contains three integers n,k andw(1 ≤ k ≤ min(n, 10), 1 ≤ n, w ≤ 105). The second line containsn characters. If the i-th box contains a candy, the i-th character of the line equals 1, otherwise it equals 0.

Each of the following w lines contains two integersli andri(1 ≤ li ≤ ri ≤ n) — the description of thei-th question. It is guaranteed thatri - li + 1 is divisible byk.

Output

For each question, print a single number on a single line — the minimum number of operations Dima needs to make the answer to the question positive.

Sample test(s)
Input
10 3 3
1010100011
1 3
1 6
4 9
Output
1
3
2
Note

For the first question, you need to take a candy from the first box to make the answer positive. So the answer is 1.

For the second question, you need to take a candy from the first box, take a candy from the fifth box and put a candy to the sixth box. The answer is 3.

For the third question, you need to take a candy from the fifth box and put it to the sixth box. The answer is 2.

思路:預處理,求出從開頭到當前位置1的個數ans陣列,然後從後向前dp求出從當前位置每次加k直到最後的位置上的1的個數,然後對於每次詢問,進行簡單的計算就可以了。

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
using namespace std;
const int maxn=100010;
int n,k,w,l,r;
string a;
int ans[maxn],is_one[maxn];
void solve()
{
    memset(ans,0,sizeof(ans));
    memset(is_one,0,sizeof(is_one));
    int len=a.size();
    ans[0]=a[0]=='1'?1:0;
    for(int i=1;i<len;i++)
    ans[i]=ans[i-1]+(a[i]=='1'?1:0);
    for(int i=len-1;i>=len-k;i--)
    if(a[i]=='1')is_one[i]++;
    for(int i=len-k-1;i>=0;i--)
    {
        if(a[i]=='1')is_one[i]++;
        is_one[i]+=is_one[i+k];
    }
}
int main()
{
    cin>>n>>k>>w>>a;
    solve();
    while(w--)
    {
        cin>>l>>r;
        int q=(r+1-l)/k;
        l--,r--;
        int m=is_one[l+k-1]-is_one[r];
        if(a[r]=='1')m++;
        int p=l==0?ans[r]:ans[r]-ans[l-1];
        cout<<p-m+q-m<<endl;
    }
    return 0;
}

D. Inna and Sweet Matrix

Inna loves sweets very much. That's why she decided to play a game called "Sweet Matrix".

Inna sees an n × m matrix and k candies. We'll index the matrix rows from 1 to n and the matrix columns from 1 to m. We'll represent the cell in thei-th row and j-th column as(i, j). Two cells (i, j) and (p, q) of the matrix areadjacent if |i - p| + |j - q| = 1. Apath is a sequence of the matrix cells where each pair of neighbouring cells in the sequence is adjacent. We'll call the number of cells in the sequence the path's length.

Each cell of the matrix can have at most one candy. Initiallly, all the cells are empty. Inna is trying to place each of thek candies in the matrix one by one. For each candy Inna chooses cell(i, j) that will contains the candy, and also chooses the path that starts in cell(1, 1) and ends in cell (i, j) and doesn't contain any candies. After that Inna moves the candy along the path from cell(1, 1) to cell (i, j), where the candy stays forever. If at some moment Inna can't choose a path for the candy, she loses. If Inna can place all the candies in the matrix in the described manner, then her penalty equals the sum of lengths of all the paths she has used.

Help Inna to minimize the penalty in the game.

Input

The first line of the input contains three integers n,m and k(1 ≤ n, m ≤ 50, 1 ≤ k ≤ n·m).

Output

In the first line print an integer — Inna's minimum penalty in the game.

In the next k lines print the description of the path for each candy. The description of the path of the candy that is placedi-th should follow on the i-th line. The description of a path is a sequence of cells. Each cell must be written in the format(i, j), where i is the number of the row and j is the number of the column. You are allowed to print extra whitespaces in the line. If there are multiple optimal solutions, print any of them.

Please follow the output format strictly! If your program passes the first pretest, then the output format is correct.

Sample test(s)
Input
4 4 4
Output
8
(1,1) (2,1) (2,2)
(1,1) (1,2)
(1,1) (2,1)
(1,1)
Note

Note to the sample. Initially the matrix is empty. Then Inna follows her first path, the path penalty equals the number of cells in it — 3. Note that now no path can go through cell (2, 2), as it now contains a candy. The next two candies go to cells (1, 2) and (2, 1). Inna simply leaves the last candy at cell (1, 1), the path contains only this cell. The total penalty is: 3 + 2 + 2 + 1 = 8.

Note that Inna couldn't use cell (1, 1) to place, for instance, the third candy as in this case she couldn't have made the path for the fourth candy.


題意:Inna有k個糖果,在一個n*m平面上,她要從(1,1)移動糖果,使得總移動步數最少,並且使得每個糖果都能移動到一個合適的位置。求步數並輸出移動路徑。
思路:先是貪心的思想,步數要儘量少,橫縱座標之和-1即為步數,那麼就按這排序,前k個就是我們要的目標位置,接著構造路徑,既然已知目標位置,那麼讓每個先一直往下在一直往右移動就可以了。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=55;
struct node
{
    int x,y;
}grid[maxn*maxn];
int n,m,k,num;
bool cmp(node a,node b)
{
    return a.x+a.y<b.x+b.y;
}
int main()
{
    num=0;
    cin>>n>>m>>k;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        grid[num].x=i,grid[num++].y=j;
    sort(grid,grid+n*m,cmp);
    int ans=0;
    for(int i=0;i<k;i++)
    ans+=grid[i].x+grid[i].y-1;
    cout<<ans<<endl;
    for(int i=k-1;i>=0;i--)
    {
        for(int j=1;j<=grid[i].x;j++)
        printf("(%d,%d) ",j,1);
        for(int j=2;j<=grid[i].y;j++)
        printf("(%d,%d) ",grid[i].x,j);
        cout<<endl;
    }
    return 0;
}

E(轉載)沒看懂程式碼,待續
E題:在一個n*m平面上,Inna進行w次操作,操作0表示在(x1,y1)到(x2,y2)的矩形中新增v個糖果,操作1表示詢問(x1,y1)到(x2,y2)的矩形中的糖果數與該矩形4個角上對應矩形中的總糖果數的差。
思路:看到新增,詢問自然就會想到樹狀陣列,線段樹,但是好像是二維的,實際上可以用一維的做法,把x和y分開來考慮,最後詢問計算個數的時候,計算方式就如下圖所示。下標還是有些細節要多注意。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>

using namespace std;
typedef long long ll;
const int N = 4 * 1000005;

int n[2], w;
ll bit1[2][N], bit2[2][N];

void insert(ll* b, int m, int x, ll value) {
	while (x <= m) {
		b[x] += value;
		x += x & (-x);
	}
}

ll get(ll* b, int x) {
	ll ans = 0;
	while (x > 0) {
		ans += b[x];
		x -= x & (-x);
	}
	return ans;
}

ll Get(int id, int r) {
	return get(bit1[id], r) * r + get(bit2[id], r);
}

void Insert(int id, int l, int r, ll value) {
	insert(bit1[id], n[id], l, value);
	insert(bit1[id], n[id], r+1, -value);
	insert(bit2[id], n[id], l, -value * (l-1));
	insert(bit2[id], n[id], r+1, value * r);
}

int main () {
	int x1, x2, y1, y2, v;
	scanf("%d%d%d", &n[0], &n[1], &w);
	while (w--) {
		scanf("%d%d%d%d%d", &v, &x1, &y1, &x2, &y2);
		if (v) {
			ll ans = Get(1, y2) - Get(1, y1-1) - (Get(0, n[0]) - Get(0, x2) + Get(0, x1-1));
			cout << ans << endl;
		} else {
			ll value;
			cin >> value;
			Insert(0, x1, x2, value * (y2-y1+1));
			Insert(1, y1, y2, value * (x2-x1+1));
		}
	}
	return 0;
}



相關文章