第二天补的
并不都是自己写的
参考了别人的题解
感觉这次cf题意好懂
就是做起来嘛。。
毕竟我还太弱。。
只为落实
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.
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 ≤ 100, 1 ≤ 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.
In a single line print the sum of all values in the cells of the table.
2 1 1 2 3 2 2 3 3
10
2 1 1 3 3 1 1 3 3
18
Note to the first sample test:
Values of the table in the first three rows and columns will be as follows:
121
121
110
So, the sum of values will be equal to 10.
Note to the second sample test:
Values of the table in the first three rows and columns will be as follows:
222
222
222
So, the sum of values will be equal to 18.
题意:给两个点坐标算矩形面积之和
#include "iostream"
#include "cstdio"
#include "cmath"
#include "algorithm"
using namespace std;
int main(int argc, char const *argv[])
{
int n, a, b, c, d;
cin >> n;
int ans = 0;
for(int i = 0; i < n; ++i) {
cin >> a >> b >> c >> d;
ans += abs(c - a + 1) * abs(d - b + 1);
}
cout << ans << endl;
return 0;
}
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.
The first line contains integer n (1 ≤ n ≤ 109) — the number of books in the library.
Print the number of digits needed to number all the books.
13
17
4
4
Note to the first test. The books get numbers 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, which totals to 17 digits.
Note to the second sample. The books get numbers 1, 2, 3, 4, which totals to 4 digits.
题意:给一个n判断1~n有多少个数 eg: 13有17个数 分别是:1,2,3,4,5,6,7,8,9,1,0,1,1,1,2,1,3
思路:一个数组保存个数 另外加一下零头多少个就好了
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "cmath"
using namespace std;
int n;
long long f[12];
long long t[12];
int main() {
cin >> n;
t[1] = 10;
for (int i = 2; i <= 10; i++)
t[i] = t[i - 1] * 10;
f[1] = 11;
for (int i = 2; i <= 10; i++)
f[i] = f[i - 1] + (t[i] - t[i - 1]) * i + 1;
long long ans;
long long base;
for (int i = 0; i <= n; i++) {
if (t[i] > n) {
base = i - 1;
break;
}
}
ans = f[base] + (n - t[base]) * (base + 1);
cout << ans << endl;
return 0;
}<span style="color:#33cc00;">
</span>
我的代码():
判错是test5 可是我本地和oj上结果不一样 本地上的结果就是对的。。 奇怪。。
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "queue"
#include "cmath"
using namespace std;
long long num[12] = {0};
void init()
{
for(int i = 1; i <= 11; ++i)
num[i] = num[i - 1] + 9 * pow(10, i - 1) * i;
}
int main(int argc, char const *argv[])
{
int n;
init();
while(cin >> n) {
int x = n, n_num = 0;
while(x / 10) {
n_num++;
x /= 10;
}
if(n_num == 0) {
printf("%d\n", n);
return 0;
}
long long ans = num[n_num];
ans += (n - pow(10, n_num) + 1) * (n_num + 1);
cout << ans << endl;
}
return 0;
}
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.
The first line contains two integers w, m (2 ≤ w ≤ 109, 1 ≤ m ≤ 109) — the number defining the masses of the weights and the mass of the item.
Print word 'YES' if the item can be weighted and 'NO' if it cannot.
3 7
YES
100 99
YES
100 50
NO
Note to the first sample test. One pan can have an item of mass 7 and a weight of mass 3, and the second pan can have two weights of masses 9 and 1, correspondingly. Then 7 + 3 = 9 + 1.
Note to the second sample test. One pan of the scales can have an item of mass 99 and the weight of mass 1, and the second pan can have the weight of mass 100.
Note to the third sample test. It is impossible to measure the weight of the item in the manner described in the input.
嗯。。 并不会做。。看题解。。
题意:用质量为w0,w1....w100的砝码乘出质量m,砝码可放天平左可放天平右
思路:假设可以称出,用w进制表示m,每一位上一定是0, 1, w - 1, 否则就是NO,如果某一位是w-1则说明这个砝码跟物品摆在同一边,
对m的每一位模拟这个过程
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
using namespace std;
int main(int argc, char const *argv[])
{
int n, m;
cin >> n >> m;
if(n == 3) {
cout << "YES" << endl;
return 0;
}
while(m) {
int z = m % n;
if(z <= 1)
m /= n;
else if(z == n - 1)
m = m / n + 1;
else {
cout << "NO" << endl;
return 0;
}
}
cout << "YES" << endl;
return 0;
}
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.
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.
In the first line print an integer — the number of triangles with the non-zero area among the painted points.
4 0 0 1 1 2 0 2 2
3
3 0 0 1 1 2 0
1
1 1 1
0
Note to the first sample test. There are 3 triangles formed: (0, 0) - (1, 1) - (2, 0); (0, 0) - (2, 2) - (2, 0); (1, 1) - (2, 2) - (2, 0).
Note to the second sample test. There is 1 triangle formed: (0, 0) - (1, 1) - (2, 0).
Note to the third sample test. A single point doesn't form a single triangle.
题意:给出n个坐标,问能构成几个三角形
嗯。。还是看题解。。
思路:总方案数为n^3之和(数学公式),枚举每一个点,计算这个点与其他所有点的斜率,三点共线则斜率会相等,减去相等斜率m个点m^2之和
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "vector"
#include "cmath"
using namespace std;
const double inf = 1e10;
const double eps = 1e-8;
typedef long long ll;
struct point
{
/* data */
int x, y;
point() {};
point(int a, int b): x(a), y(b) {}
};
int n;
point p[2005];
int main(int argc, char const *argv[])
{
cin >> n;
for(int i = 0; i < n; ++i)
cin >> p[i].x >> p[i].y;
if(n < 3) {
cout << 0 << endl;
return 0;
}
ll tot = (ll)n * (n - 1) * (n - 2) / 6;
ll sum = 0;
vector<double> ans;
for(int i = 0; i < n; ++i) {
ans.clear();
for(int j = i + 1; j < n; ++j) {
int dx = p[j].x - p[i].x;
int dy = p[j].y - p[i].y;
double k;
if(dx == 0) k = inf;
else if(dy == 0) k = 0.0;
else k = (double)dy / (double)dx;
ans.push_back(k);
}
sort(ans.begin(), ans.end());
double num = ans[0];
int cnt = 0;
for(int j = 0; j < ans.size(); ++j)
if(fabs(ans[j] - num) < eps) cnt++;
else {
sum += (ll)cnt * ((ll)cnt - 1ll) / 2;
cnt = 1;
num = ans[j];
}
sum += (ll)cnt * ((ll)cnt - 1ll) / 2;
}
cout << tot - sum << endl;
return 0;
}
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.
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.
In the first line print the maximum possible value of an expression.
3+5*7+8*4
303
2+3*5
25
3*4*5
60
Note to the first sample test. 3 + 5 * (7 + 8) * 4 = 303.
Note to the second sample test. (2 + 3) * 5 = 25.
Note to the third sample test. (3 * 4) * 5 = 60 (also many other variants are valid, for instance, (3) * 4 * 5 = 60).
题意:给出一个表达式只含有加号+和乘号*,数字是1~9,让你加一个括号,使得表达式值最大,看这个就知道肯定用栈(说的好像我会一样)
思路:左括号在乘号右边,右括号在乘号左边保证最大。枚举括号位置并计算。
AC代码:
#include "iostream"
#include "cstdio"
#include "cstring"
#include "algorithm"
#include "stack"
#include "vector"
#include "string"
using namespace std;
typedef long long ll;
int n;
string str;
vector<int> pos;
stack<char> sc;
stack<ll> sn;
ll tworesult(ll a, ll b, char c) {
return (c == '*') ? a * b : a + b;
}
void cal() {
char t = sc.top();
sc.pop();
ll a = sn.top();
sn.pop();
ll b = sn.top();
sn.pop();
sn.push(tworesult(a, b, t));
}
ll expression(string s) {
for(int i = 0; i < s.length(); ++i) {
char c = s[i];
if(isdigit(c)) sn.push(c - '0');
else if(c == '(') sc.push(c);
else if(c == ')') {
while(sc.top() != '(') cal();
sc.pop();
}else {
if(c == '+') {
while(!sc.empty() && sc.top() == '*') cal();
sc.push(c);
}else sc.push(c);
}
}
while(!sc.empty()) cal();
return sn.top();
}
int main(int argc, char const *argv[])
{
cin >> str;
n = str.size();
pos.push_back(-1);
for(int i = 1; i < n; i += 2)
if(str[i] == '*') pos.push_back(i);
pos.push_back(n);
int len = pos.size();
ll ans = 0;
for(int i = 0; i < len - 1; ++i)
for(int j = i + 1; j < len; ++j) {
string s = str;
s.insert(pos[i] + 1, 1, '(');
s.insert(pos[j] + 1, 1, ')');
ans = max(ans, expression(s));
}
cout << ans << endl;
return 0;
}