Codeforces Round #308 (Div. 2)

A. Vanya and Table
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya has a table consisting of 100 rows, each row contains 100 cells. The rows are numbered by integers from 1 to 100 from bottom to top, the columns are numbered from 1 to 100 from left to right.

In this table, Vanya chose n rectangles with sides that go along borders of squares (some rectangles probably occur multiple times). After that for each cell of the table he counted the number of rectangles it belongs to and wrote this number into it. Now he wants to find the sum of values in all cells of the table and as the table is too large, he asks you to help him find the result.

Input

The first line contains integer n (1 ≤ n ≤ 100) — the number of rectangles.

Each of the following n lines contains four integers x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ 1001 ≤ y1 ≤ y2 ≤ 100), where x1 and y1 are the number of the column and row of the lower left cell and x2 and y2 are the number of the column and row of the upper right cell of a rectangle.

Output

In a single line print the sum of all values in the cells of the table.

Sample test(s)
input
2
1 1 2 3
2 2 3 3
output
10
题意:给出n矩形的左下角点和右上角点,每个点若被一个矩形覆盖则+1,求所有点的权值和
解法:最简单的是直接把每个矩形面积算出来累加,不想想的话就直接模拟对每个点进行累加
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
int n;
int mp[105][105];
int main(void){
    scanf("%d",&n);
    while(n--){
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        for(int i=x1;i<=x2;i++)
            for(int j=y1;j<=y2;j++)
                mp[i][j]++;
        
    }
    int sum=0;
        for(int i=1;i<=100;i++)
            for(int j=1;j<=100;j++)
                sum+=mp[i][j];
    printf("%d\n",sum);
    
    return 0;
}



B. Vanya and Books
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya got an important task — he should enumerate books in the library and label each book with its number. Each of the n books should be assigned with a number from 1 to n. Naturally, distinct books should be assigned distinct numbers.

Vanya wants to know how many digits he will have to write down as he labels the books.

Input

The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.

Output

Print the number of digits needed to number all the books.

Sample test(s)
input
13
output
17
题意:求数字1到n,所有数的位数和
解法:每10的次方-1算1组,这样只要记录当前组的位数,和上一组与这一组的最后一个数的位数相减乘位数,就是当前组的位数和
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long

int main(void){
    int n;
    int now=1;
    int ten=10;
    int last=0;
    scanf("%d",&n);
    LL sum=0;
    while(n>=ten){
        sum += 1LL*(ten-1-last)*now;
        last = ten-1;
        ten*=10;
        now++;
    }
    sum += 1LL*(n-last)*now;
    printf("%I64d\n",sum);
    return 0;
}



C. Vanya and Scales
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya has a scales for weighing loads and weights of masses w0, w1, w2, ..., w100 grams where w is some integer not less than 2(exactly one weight of each nominal value). Vanya wonders whether he can weight an item with mass m using the given weights, if the weights can be put on both pans of the scales. Formally speaking, your task is to determine whether it is possible to place an item of mass m and some weights on the left pan of the scales, and some weights on the right pan of the scales so that the pans of the scales were in balance.

Input

The first line contains two integers w, m (2 ≤ w ≤ 1091 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.

Output

Print word 'YES' if the item can be weighted and 'NO' if it cannot.

Sample test(s)
input
3 7
output
YES
题意:你有一些砝码,质量为w^0,w^1,……w^100各一个,现有一个东西质量为m,你需要把东西放天平一边,并往里面放砝码,使得天平两边平衡,如果能平衡输入YES,没有平衡的方案输出no
思路:把m换算成w进制,从低位往高位枚举,当当前位等于w-1则改为-1,然后下一位+1,若大于等于w,则取余,下一位为当前位整除w,这样最后判断所有位是否大于等于-1小于等于1即可。w等于2的时候要特判
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
int f[10000];
int main(void){
    int n,m;
    int now=0;
    scanf("%d%d",&n,&m);
    while(m>0){
        f[now++]=m%n;
        m/=n;
    }
    for(int i=0;i<now;i++)
        if(f[i]==n-1&&n!=2){
            f[i]=-1;
            f[i+1]++;
            now = max(now,i+2);
        }
        else if(f[i]>=n){
            f[i+1]+=f[i]/n;
            f[i]%=n;
            now = max(now,i+2);
        }
    for(int i=0;i<now;i++)
        if(!(f[i]>=-1&&f[i]<=1)){
            printf("NO\n");
            return 0;
        }
    printf("YES\n");
    return 0;
}



D. Vanya and Triangles
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Vanya got bored and he painted n distinct points on the plane. After that he connected all the points pairwise and saw that as a result many triangles were formed with vertices in the painted points. He asks you to count the number of the formed triangles with the non-zero area.

Input

The first line contains integer n (1 ≤ n ≤ 2000) — the number of the points painted on the plane.

Next n lines contain two integers each xi, yi ( - 100 ≤ xi, yi ≤ 100) — the coordinates of the i-th point. It is guaranteed that no two given points coincide.

Output

In the first line print an integer — the number of triangles with the non-zero area among the painted points.

Sample test(s)
input
4
0 0
1 1
2 0
2 2
output
3
题意:给出n个点坐标,要你求出能构成多少个不同的三角形
思路:其实就是求三点不共线的方案,这里我转成求三点共线的方案,两者求法差不多,不过前者麻烦一点点。唯一的难点就是排除重复的共线方案。首先我们需要找到一个点作为原点建立xy轴,然后算出这个点后面所有点相对于该点的角度,把相同角度跟当前角度+π的当成一组,找出每一组的个数,从中任选两个的排列组合就是这个角度的共线方案。最后找到所有共线方案后,用C(3,n)-方案数就是不共线的方案数
注意:在判断角度是否相同的时候需要用到精度,以及二分查找角度+π的时候也要用到精度
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<math.h>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
const double eps = 1e-10;
const double pi = acos(-1.0);
int n;
struct po{
    int x,y;
    void in(){
        scanf("%d%d",&x,&y);
    }
}node[2005],base;
struct sw{
    double du;
}dian[2005];
int cou;
int vis[2005];
double dis(po x,po y){
    return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
int cmp(const void *a,const void *b){
    return ((sw *)a)->du-((sw *)b)->du>0?1:-1;
}
int found(double du,int l,int r){
    int mid ;
    while(l<=r){
        mid = (l+r)>>1;
        if(dian[mid].du>=du||fabs(dian[mid].du-du)<=eps)
            r=mid-1;
        else l=mid+1;
    }
    return l;
}
int main(void){
    scanf("%d",&n);
    for(int i=0;i<n;i++) node[i].in();
    if(n<=2){
        printf("0\n");
        return 0;
    }
    LL sum = 0;
    for(int i=0;i<n-2;i++){
        base = node[i];
        cou=0;
        for(int j=i+1;j<n;j++){
            dian[cou].du = acos((node[j].x-base.x)/dis(node[j],base));
            if(node[j].y<base.y)
                dian[cou].du = 2*pi-dian[cou].du;
            cou++;
        }
        qsort(dian,cou,sizeof(dian[0]),cmp);
        memset(vis,0,sizeof(vis));
        int j,k,tk,res;
        for(j=0;j<cou;j++)
            if(!vis[j]){
                vis[j]=1;
                res=1;
                for(k=j+1;k<cou;k++)
                    if(fabs(dian[k].du-dian[j].du)<eps){
                        res++;
                        vis[k]=1;
                    }
                    else break;
                k = found(dian[j].du+pi,k,cou-1);
                if(k>j)
                for(;k<cou;k++)
                    if(fabs(dian[k].du-dian[j].du)<eps+pi){
                        res++;
                        vis[k]=1;
                    }
                sum += 1LL*res*(res-1)/2;
            }
    }
    LL ans = 1LL *n*(n-1)*(n-2)/6-sum;
    printf("%I64d\n",ans);
    return 0;
}



E. Vanya and Brackets
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Vanya is doing his maths homework. He has an expression of form , where x1, x2, ..., xn are digits from 1 to 9, and sign  represents either a plus '+' or the multiplication sign '*'. Vanya needs to add one pair of brackets in this expression so that to maximize the value of the resulting expression.

Input

The first line contains expression s (1 ≤ |s| ≤ 5001|s| is odd), its odd positions only contain digits from 1 to 9, and even positions only contain signs  +  and  * .

The number of signs  *  doesn't exceed 15.

Output

In the first line print the maximum possible value of an expression.

Sample test(s)
input
3+5*7+8*4
output
303
题意:给出一个表达式,添加一个括号使得最后值最大
思路:很容易想到直接枚举括号的方案,相当于在i前加左括号,j后加右括号,这样复杂度就是n^2,时间复杂度够了,这样我们只需要模拟这个过程就行了,我们需要用各种前缀跟后缀,用f数组保存从1到当前某一位表达式的值,b数组保存从后面到当前位表达式的值,乘法处理的时候因为要连续几个数相乘,所以我们需要保存前面几个数相乘,并记录最后一个加法位置
 
   
#pragma comment(linker, "/STACK:1024000000,1024000000") 
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<string>
using namespace std;

#define LL long long
#define ULL unsigned long long
char str[6000];
int num[3000];
char q[3000],h[3000];
int all;
LL f[3000],b[3000],t[3000];
int flast[3000],blast[3000];
LL fmut[3000],bmut[3000];
int main(void){
    scanf("%s",str);
    all = (strlen(str)+1)/2;
    int now=1;
    q[1]='+';
    for(int i=0;str[i];i++){
        if(str[i]>='1'&&str[i]<='9'){
            num[now]=str[i]-'0';
            now++;
        }
        else {
            h[now-1] = str[i];
            q[now] = str[i];
        }
    }
    h[now-1]='+';
    
    int flag=0,last=0;
    LL mut;
    for(int i=1;i<=all;i++)
        if(q[i]=='+')
            f[i] = f[i-1]+num[i],flag=0;
        else {
            if(!flag) flag=1,mut=num[i-1],last=i-2;
            flast[i]=last;
            fmut[i]=mut;
            mut*=num[i];
            f[i] = f[last]+mut;
        }
    flag=0,last=all;
    for(int i=all;i>=1;i--)
        if(h[i]=='+')
            b[i] = b[i+1]+num[i],flag=0;
        else {
            if(!flag) flag=1,mut=num[i+1],last=i+2;
            blast[i]=last;
            bmut[i]=mut;
            mut*=num[i];
            b[i]= b[last]+mut;
        }
    LL ans = f[all];
    LL res = 0;
    for(int i=all;i>=1;i--){
        memset(t,0,sizeof(t));
        for(int j=i;j>=1;j--){
            if(h[j]=='+'||j==i){
                t[j]=t[j+1]+num[j];
                flag=0;
            }
            else {
                if(!flag) flag=1,mut=num[j+1],last=j+2;
                mut*=num[j];
                t[j]=t[last]+mut;
            }
            if(h[i]=='+'){
                if(q[j]=='+')
                    ans = max(ans,res=0LL+f[j-1]+t[j]+b[i+1]);
                else ans = max(ans,res=0LL+f[flast[j]]+fmut[j]*t[j]+b[i+1]);
            }
            else {
                if(q[j]=='+')
                    ans = max(ans,res=0LL+f[j-1]+t[j]*bmut[i]+b[blast[i]]);
                else ans = max(ans,res=0LL+f[flast[j]]+fmut[j]*t[j]*bmut[i]+b[blast[i]]);
            }
        //  printf("%I64d\n",res);
        }
    }
    printf("%I64d\n",ans);

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值